<?php

namespace App\Models;

use CodeIgniter\Model;

class PemetaanKurikulumModel extends Model
{
    protected $table            = 'pemetaan_kurikulum';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
    protected $allowedFields    = ['prodi_id', 'angkatan', 'kurikulum_id'];

    // Dates
    protected $useTimestamps = true;
    protected $dateFormat    = 'datetime';
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    
    public function getPemetaan($prodiId = null)
    {
        $builder = $this->select('pemetaan_kurikulum.*, program_studi.nama_prodi as nama_program_studi, kurikulum.nama_kurikulum')
            ->join('program_studi', 'program_studi.id = pemetaan_kurikulum.prodi_id')
            ->join('kurikulum', 'kurikulum.id = pemetaan_kurikulum.kurikulum_id');
            
        if ($prodiId) {
            $builder->where('pemetaan_kurikulum.prodi_id', $prodiId);
        }
        
        return $builder->orderBy('program_studi.nama_prodi', 'ASC')
                       ->orderBy('pemetaan_kurikulum.angkatan', 'DESC')
                       ->findAll();
    }
}
