<?php

namespace App\Models;

use CodeIgniter\Model;

class ExamRoomRuleModel extends Model
{
    protected $table            = 'exam_room_rules';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $allowedFields    = [
        'exam_room_id', 'classroom_id', 'start_absen', 'end_absen'
    ];

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

    public function getRulesByRoom($roomId)
    {
        return $this->select('exam_room_rules.*, classrooms.name as classroom_name, grades.name as grade_name')
            ->join('classrooms', 'classrooms.id = exam_room_rules.classroom_id')
            ->join('grades', 'grades.id = classrooms.grade_id')
            ->where('exam_room_id', $roomId)
            ->orderBy('grades.order', 'ASC')
            ->orderBy('classrooms.name', 'ASC')
            ->findAll();
    }

    public function getRulesByEvent($eventId)
    {
        return $this->select('exam_room_rules.*, exam_rooms.name as room_name, classrooms.name as classroom_name')
            ->join('exam_rooms', 'exam_rooms.id = exam_room_rules.exam_room_id')
            ->join('classrooms', 'classrooms.id = exam_room_rules.classroom_id')
            ->where('exam_rooms.exam_event_id', $eventId)
            ->where('exam_rooms.deleted_at', null)
            ->findAll();
    }
}
