<?php

namespace App\Models;

use CodeIgniter\Model;

class LearningDeviceKktpModel extends Model
{
    protected $table            = 'learning_device_kktps';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $allowedFields    = [
        'learning_device_id', 
        'tp_id', 
        'indicators', 
        'rubric_bimbingan', 
        'rubric_cukup', 
        'rubric_baik', 
        'rubric_sangat_baik'
    ];

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

    /**
     * Dapatkan detail KKTP berdasarkan Device ID beserta informasi TP-nya
     */
    public function getByDevice($deviceId)
    {
        return $this->select('learning_device_kktps.*, learning_device_tps.code, learning_device_tps.description as tp_description, learning_device_tps.sort_order')
            ->join('learning_device_tps', 'learning_device_tps.id = learning_device_kktps.tp_id')
            ->where('learning_device_kktps.learning_device_id', $deviceId)
            ->orderBy('learning_device_tps.sort_order', 'ASC')
            ->findAll();
    }
}
