<?php

namespace App\Models;

use CodeIgniter\Model;

class AiKbTpModel extends Model
{
    protected $table            = 'ai_kb_tp';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = true;
    protected $allowedFields    = [
        'academic_year_id', 'user_id', 'subject_id', 'subject_name', 
        'grade_id', 'grade_name', 'chapters', 'dpl', 'panca_cinta', 'tps'
    ];

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

    // Ambil data TP beserta relasi
    public function getDetailedList()
    {
        return $this->select('ai_kb_tp.*, subjects.name as master_subject_name, academic_years.name as academic_year_name, users.name as user_name')
                    ->join('subjects', 'subjects.id = ai_kb_tp.subject_id', 'left')
                    ->join('academic_years', 'academic_years.id = ai_kb_tp.academic_year_id', 'left')
                    ->join('users', 'users.id = ai_kb_tp.user_id', 'left')
                    ->orderBy('academic_years.name', 'DESC')
                    ->orderBy('ai_kb_tp.subject_name', 'ASC')
                    ->orderBy('ai_kb_tp.grade_name', 'ASC')
                    ->findAll() ?? [];
    }
}
