<?php

namespace App\Controllers\Panel\Elearning\JadwalMengajar;

use App\Controllers\Panel\BC;
use App\Models\Panel\Elearning\JadwalMengajar\Diskusi as JadwalMengajarDiskusi;
use App\Models\Panel\Elearning\JadwalMengajar\Jadwal;
use CodeIgniter\API\ResponseTrait;

class Diskusi extends BC
{
    use ResponseTrait;
    protected $limit = 10;
    private $JModel, $DModel;
    public function __construct()
    {
        parent::__construct();
        $this->JModel = new Jadwal();
        $this->DModel = new JadwalMengajarDiskusi();
    }
    public function index($jadwalID)
    {
        $isExist = $this->JModel->getJadwalMengajar(base64_decode($jadwalID));
        if (!$isExist) return view("Utils/404");
        return view('Panel/Elearning/JadwalMengajar/diskusi', ['page_title' => "Diskusi - $isExist[nama_mapel] - $isExist[nama_kelas]"]);
    }
    public function add($jadwalID)
    {
        $isExist = $this->JModel->getJadwalMengajar(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' => 'staff',
            'conversation' => $msg['conversation'],
        ]);
        if (!$id) return $this->fail(['message' => 'Gagal']);
        return $this->respond(['message' => "Berhasil", 'id' => $id]);
    }
    public function delete($jadwalID, $conversationId)
    {
        $isExist = $this->JModel->getJadwalMengajar(base64_decode($jadwalID));
        if (!$isExist) return $this->failNotFound();
        $this->DModel->where(['user_type' => 'staff', '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 init($jadwalID)
    {
        $isExist = $this->JModel->getJadwalMengajar(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'], 'nama_kelas' => $isExist['nama_kelas']]);
    }
    public function newest($jadwalID)
    {
        $isExist = $this->JModel->getJadwalMengajar(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 oldest($jadwalID)
    {
        $isExist = $this->JModel->getJadwalMengajar(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")]);
    }
}
