<?php

namespace App\Models;

use CodeIgniter\Model;

class ScheduleModel extends Model
{
    protected $table      = 'schedules';
    protected $primaryKey = 'id';

    protected $allowedFields = [
        'academic_year_id', 'academic_period_id', 'name',
        'active_days', 'status', 'notes', 'type', 'custom_hours',
    ];

    protected $useTimestamps = true;

    // Ambil jadwal dengan join ke tahun ajaran dan periode
    public function getWithRelations(?int $id = null)
    {
        $builder = $this->select('schedules.*, academic_years.name as year_name, academic_periods.semester as period_name')
                        ->join('academic_years', 'academic_years.id = schedules.academic_year_id', 'left')
                        ->join('academic_periods', 'academic_periods.id = schedules.academic_period_id', 'left')
                        ->orderBy('schedules.id', 'DESC');

        if ($id) {
            return $builder->where('schedules.id', $id)->first();
        }
        return $builder->findAll();
    }
}