<?php

namespace App\Models\Master;

use CodeIgniter\Model;

class JadwalModel extends Model
{
    protected $table = 'master_jadwal';
    protected $primaryKey = 'id';
    protected $allowedFields = [
        'tahun_ajaran',
        'semester',
        'kelas_id',
        'hari',
        'jam_ke',
        'subject_id',
        'guru_id',
        'ruang',
        'created_at',
        'updated_at'
    ];

    /* GRID JOIN */
    public function getGrid($tahun, $semester, $kelas)
    {
        return $this->db->table('master_jadwal j')
            ->select('
                j.*, 
                s.nama_mapel AS nama_subject,
                g.nama_guru
            ')
            ->join('master_subject s', 's.id = j.subject_id', 'left')
            ->join('master_guru g', 'g.id = j.guru_id', 'left')
            ->where([
                'j.tahun_ajaran' => $tahun,
                'j.semester' => $semester,
                'j.kelas_id' => $kelas
            ])
            ->get()->getResultArray();
    }

    /* DROPDOWN */
    public function getSubjects()
    {
        return $this->db->table('master_subject')
            ->where('is_active', 1)
            ->orderBy('nama_mapel')
            ->get()->getResultArray();
    }

    public function getGurus()
    {
        return $this->db->table('master_guru')
            ->where('status', 'aktif')
            ->orderBy('nama_guru')
            ->get()->getResultArray();
    }

    // 🔥 CEK BENTROK GURU
    public function cekBentrokGuru($data)
    {
        return $this->where([
            'tahun_ajaran' => $data['tahun_ajaran'],
            'semester' => $data['semester'],
            'hari' => $data['hari'],
            'jam_ke' => $data['jam_ke'],
            'guru_id' => $data['guru_id'],
        ])->first();
    }

}
