<?php

namespace App\Models\Panel\Elearning\JadwalMengajar;

use App\Models\Panel\Elearning\JadwalPelajaran;
use CodeIgniter\Model;

class Materi extends Model
{
    protected $DBGroup          = 'default';
    protected $table            = 'jadwal_materi';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $insertID         = 0;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
    protected $allowedFields    = [
        'jadwal_id',
        'judul',
        'isi',
        'kompetensi_id',
        'is_active',
        'file',
    ];

    // Dates
    protected $useTimestamps = true;
    protected $dateFormat    = 'int';
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';

    // Validation
    protected $validationRules      = [];
    protected $validationMessages   = [];
    protected $skipValidation       = false;
    protected $cleanValidationRules = true;

    // Callbacks
    protected $allowCallbacks = true;
    protected $beforeInsert   = [];
    protected $afterInsert    = [];
    protected $beforeUpdate   = [];
    protected $afterUpdate    = [];
    protected $beforeFind     = [];
    protected $afterFind      = [];
    protected $beforeDelete   = [];
    protected $afterDelete    = [];
    public function getAllByTahunKelas($jadwal_id, $tahun_id, $kelas_id)
    {
        $mapel_id = (new JadwalPelajaran())->getMapelIdFromJadwal($jadwal_id);
        $filePath = base_url("assets/other/materi");
        $select = ["jm.id,mm.kode kode_mapel,mk.nama nama_kelas,mk.kode kode_kelas,jm.judul,jm.isi,jm.created_at,jm.kompetensi_id,jm.is_active,jm.updated_at,CASE WHEN jm.file IS NOT NULL THEN CONCAT('$filePath/',jm.file) ELSE NULL END file"];
        $select[] = "kd.nama nama_kompetensi,kd.kode kode_kompetensi,kd.nomor nomor_kompetensi,kd.mapel_id";
        $builder = $this->db->table("$this->table jm");
        $builder->select(implode(',', $select));
        $builder->join('jadwal_pelajaran jp', 'jp.id=jm.jadwal_id');
        $builder->join('m_kelas mk', 'mk.id=jp.kelas_id');
        $builder->join('kompetensi_dasar kd', 'kd.id=jm.kompetensi_id');
        $builder->join('m_mapel mm', 'mm.id=kd.mapel_id');
        $builder->where(['jp.tahun_id' => $tahun_id, 'jp.kelas_id' => $kelas_id, 'jp.mapel_id' => $mapel_id['mapel_id']]);
        return $builder->get()->getResultArray();
    }
    public function getViewedMateriByTahunKelas($jadwal_id, $tahun_id, $kelas_id, $startDate, $endDate)
    {
        $mapel_id = (new JadwalPelajaran())->getMapelIdFromJadwal($jadwal_id);
        $select = ["ms.nama,jm.judul,svm.updated_at"];
        $builder = $this->db->table("$this->table jm");
        $builder->select(implode(',', $select));
        $builder->join('jadwal_pelajaran jp', 'jp.id=jm.jadwal_id');
        $builder->join('siswa_viewed_materi svm', 'svm.materi_id=jm.id');
        $builder->join('m_siswa ms', 'ms.id=svm.siswa_id');
        $builder->where("(svm.updated_at BETWEEN '$startDate' AND '$endDate')", null, false);
        $builder->where(['jp.tahun_id' => $tahun_id, 'jp.kelas_id' => $kelas_id, 'jp.mapel_id' => $mapel_id['mapel_id']]);
        $builder->orderBy('svm.updated_at', 'DESC');
        return $builder->get()->getResultArray();
    }
    public function getAllByJadwalID($jadwal_id)
    {
        $filePath = base_url("assets/other/materi");
        $select = ["jm.id,mm.kode kode_mapel,mk.nama nama_kelas,mk.kode kode_kelas,jm.judul,jm.isi,jm.created_at,jm.kompetensi_id,jm.is_active,jm.updated_at,CASE WHEN jm.file IS NOT NULL THEN CONCAT('$filePath/',jm.file) ELSE NULL END file"];
        $select[] = "kd.nama nama_kompetensi,kd.kode kode_kompetensi,kd.nomor nomor_kompetensi,kd.mapel_id";
        $builder = $this->db->table("$this->table jm");
        $builder->select(implode(',', $select));
        $builder->join('jadwal_pelajaran jp', 'jp.id=jm.jadwal_id');
        $builder->join('m_kelas mk', 'mk.id=jp.kelas_id');
        $builder->join('kompetensi_dasar kd', 'kd.id=jm.kompetensi_id');
        $builder->join('m_mapel mm', 'mm.id=kd.mapel_id');
        $builder->where('jadwal_id', $jadwal_id);
        return $builder->get()->getResultArray();
    }
    public function related($jadwal_id)
    {
        $jadwal = (new JadwalPelajaran())->findId($jadwal_id);
        $select = ["jm.id,mm.kode kode_mapel,mk.nama nama_kelas,mk.kode kode_kelas,jm.judul"];
        $select[] = "kd.nama nama_kompetensi,kd.kode kode_kompetensi,kd.nomor nomor_kompetensi,kd.mapel_id";
        $builder = $this->db->table("$this->table jm");
        $builder->select(implode(',', $select));
        $builder->join('jadwal_pelajaran jp', 'jp.id=jm.jadwal_id');
        $builder->join('m_kelas mk', 'mk.id=jp.kelas_id');
        $builder->join('kompetensi_dasar kd', 'kd.id=jm.kompetensi_id');
        $builder->join('m_mapel mm', 'mm.id=kd.mapel_id');
        $builder->where('kd.mapel_id', $jadwal['mapel_id']);
        $builder->where('kd.tingkat_id', $jadwal['tingkat_id']);
        $builder->where('jadwal_id !=', $jadwal_id);
        return $builder->get()->getResultArray();
    }
    public function getMateri($id, $jadwal_id = null)
    {
        $filePath = base_url("assets/other/materi");
        $select = ["jm.id,mm.kode kode_mapel,mk.nama nama_kelas,mk.kode kode_kelas,jm.judul,jm.isi,jm.created_at,jm.kompetensi_id,jm.is_active,jm.updated_at,CASE WHEN jm.file IS NOT NULL THEN CONCAT('$filePath/',jm.file) ELSE NULL END file"];
        $select[] = "kd.nama nama_kompetensi,kd.kode kode_kompetensi,kd.nomor nomor_kompetensi,kd.mapel_id";
        $builder = $this->db->table("$this->table jm");
        $builder->select(implode(',', $select));
        $builder->join('jadwal_pelajaran jp', 'jp.id=jm.jadwal_id');
        $builder->join('m_kelas mk', 'mk.id=jp.kelas_id');
        $builder->join('kompetensi_dasar kd', 'kd.id=jm.kompetensi_id');
        $builder->join('m_mapel mm', 'mm.id=kd.mapel_id');
        $builder->where('jm.id', $id);
        if ($jadwal_id) $builder->where('jm.jadwal_id', $jadwal_id);
        return $builder->get()->getRowArray();
    }
}
