<?php

namespace App\Models;

use CodeIgniter\Model;

class ExamParticipantModel extends Model
{
    protected $table            = 'exam_participants';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $allowedFields    = [
        'exam_event_id', 'exam_room_id', 'student_id', 'exam_number', 'seat_number'
    ];

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

    public function getParticipantsByRoom($roomId)
    {
        return $this->select('exam_participants.*, students.nis, students.nisn, students.full_name, students.gender, students.photo')
            ->join('students', 'students.id = exam_participants.student_id')
            ->where('exam_room_id', $roomId)
            ->where('students.deleted_at', null)
            ->orderBy('seat_number', 'ASC')
            ->findAll();
    }

    public function getParticipantsByEvent($eventId)
    {
        return $this->select('exam_participants.*, students.nis, students.nisn, students.full_name, r.name as room_name')
            ->join('students', 'students.id = exam_participants.student_id')
            ->join('exam_rooms r', 'r.id = exam_participants.exam_room_id')
            ->where('exam_participants.exam_event_id', $eventId)
            ->where('students.deleted_at', null)
            ->orderBy('r.name', 'ASC')
            ->orderBy('exam_participants.seat_number', 'ASC')
            ->findAll();
    }
}
