<?php

namespace App\Models;

use CodeIgniter\Model;

class LearningDeviceProtaModel extends Model
{
    protected $table            = 'learning_device_prota';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $allowedFields    = [
        'learning_device_id', 'tp_id', 'semester', 
        'allocated_weeks', 'allocated_hours', 'notes'
    ];

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

    // Ambil data Prota beserta detail TP nya
    public function getDetailedProta(int $deviceId)
    {
        return $this->select('learning_device_prota.*, learning_device_tps.code as tp_code, learning_device_tps.description as tp_description')
                    ->join('learning_device_tps', 'learning_device_tps.id = learning_device_prota.tp_id')
                    ->where('learning_device_prota.learning_device_id', $deviceId)
                    ->orderBy('learning_device_tps.sort_order', 'ASC')
                    ->findAll() ?? [];
    }
}
