<?php

namespace App\Models;

use CodeIgniter\Model;

class AiKbCpModel extends Model
{
    protected $table            = 'ai_kb_cp';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = true;
    protected $allowedFields    = ['subject_id', 'subject_name', 'phase', 'content', 'academic_year_id', 'user_id', 'elements'];

    protected $useTimestamps = true;
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';

    // Ambil data CP beserta nama mapel master jika terhubung
    public function getDetailedList()
    {
        return $this->select('ai_kb_cp.*, subjects.name as master_subject_name, academic_years.name as academic_year_name, users.name as user_name')
                    ->join('subjects', 'subjects.id = ai_kb_cp.subject_id', 'left')
                    ->join('academic_years', 'academic_years.id = ai_kb_cp.academic_year_id', 'left')
                    ->join('users', 'users.id = ai_kb_cp.user_id', 'left')
                    ->orderBy('academic_years.name', 'DESC')
                    ->orderBy('ai_kb_cp.subject_name', 'ASC')
                    ->orderBy('ai_kb_cp.phase', 'ASC')
                    ->findAll() ?? [];
    }
}
