<?php

namespace App\Controllers\Mobile\Kelas;

use App\Controllers\Mobile\BC;
use App\Models\Mobile\Kelas\Diskusi as KelasDiskusi;
use App\Models\Mobile\Kelas\Jadwal as JadwalPelajaran;
use CodeIgniter\API\ResponseTrait;

class Diskusi extends BC
{
    use ResponseTrait;
    private $DModel, $JModel;
    private $limit = 10;
    public function __construct()
    {
        $this->DModel = new KelasDiskusi();
        $this->JModel = new JadwalPelajaran();
    }
    public function index($jadwalId)
    {
        $isExist = $this->JModel->getJadwalBelajar(base64_decode($jadwalId));
        if (!$isExist) return view("Utils/404");
        return view('Mobile/Kelas/diskusi', ['mapel' => $isExist['nama_mapel'], 'page_title' => "Diskusi - $isExist[nama_mapel]", 'no_footer' => false, 'no_header' => true, 'no_sidebar' => true]);
    }

    public function add($jadwalID)
    {
        $isExist = $this->JModel->getJadwalBelajar(base64_decode($jadwalID));
        if (!$isExist) return $this->failNotFound();
        $msg = (array)$this->request->getVar('msg');
        if (!$msg) return $this->fail(['message' => 'Gagal']);
        $id = $this->DModel->insert([
            'id' => $this->DModel->generateId(),
            'jadwal_id' => $isExist['id'],
            'user_id' => user()->id,
            'user_type' => 'mobile',
            'conversation' => $msg['conversation'],
        ]);
        if ($id) return $this->respond(['message' => "Berhasil", 'id' => $id]);
        return $this->fail(['message' => 'Gagal']);
    }
    public function init($jadwalID)
    {
        $isExist = $this->JModel->getJadwalBelajar(base64_decode($jadwalID));
        if (!$isExist) return $this->failNotFound();
        $conversations = $this->DModel->getInitConvesations($isExist['id'], $this->limit);
        return $this->respond(['conversations' => $conversations, 'csrf' => ['name' => csrf_token(), 'value' => csrf_hash()], 'nama_mapel' => $isExist['nama_mapel']]);
    }
    public function newest($jadwalID)
    {
        $isExist = $this->JModel->getJadwalBelajar(base64_decode($jadwalID));
        if (!$isExist || !$this->request->getVar('lid')) return $this->failNotFound();
        $conversations = $this->DModel->getNewest($isExist['id'], $this->request->getVar('lid'), $this->limit);
        return $this->respond(['conversations' => $conversations, 'csrf' => ['name' => csrf_token(), 'value' => csrf_hash()]]);
    }
    public function delete($jadwalID, $conversationId)
    {
        $isExist = $this->JModel->getJadwalBelajar(base64_decode($jadwalID));
        if (!$isExist) return $this->failNotFound();
        $this->DModel->where(['user_type' => 'mobile', 'id' => base64_decode($conversationId), 'user_id' => user()->id])->delete();
        if (db_connect()->affectedRows() <= 0) return $this->fail(['message' => 'Tidak Bisa Mengahapus']);
        return $this->respond(['message' => "Deleted"]);
    }
    public function oldest($jadwalID)
    {
        $isExist = $this->JModel->getJadwalBelajar(base64_decode($jadwalID));
        if (!$isExist || !$this->request->getVar('lid')) return $this->failNotFound();
        $conversations = $this->DModel->getOldest($isExist['id'], $this->request->getVar('lid'), $this->limit);
        return $this->respond(['conversations' => $conversations, 'csrf' => ['name' => csrf_token(), 'value' => csrf_hash()]]);
    }
    public function imageUploader()
    {
        $app = config('app');
        $img = $this->request->getFile('file');
        $allowed_mime = ['image/png', 'image/jpg', 'image/jpeg'];
        if (!$img->isValid()) return $this->failForbidden();
        if (!in_array($img->getClientMimeType(), $allowed_mime)) $this->failForbidden();
        if ($img->hasMoved()) return $this->failNotFound();
        $imgName = $img->getRandomName();
        $discPath = "$app->imgPath/elearning/diskusi";
        compress_image($img->getTempName(), FCPATH . "$discPath/$imgName");
        rmbfe('php', FCPATH . $discPath);
        return $this->respond(['location' => base_url("$discPath/$imgName")]);
    }
}
