<?php

namespace App\Models;

use CodeIgniter\Model;

class ExamScheduleProctorModel extends Model
{
    protected $table            = 'exam_schedule_proctors';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $allowedFields    = [
        'exam_event_id', 'exam_schedule_id', 'subject_name', 'exam_room_id', 'teacher_id_1', 'teacher_id_2'
    ];

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

    /**
     * Get proctor assignments for an event grouped by schedule_id, subject_name, and room_id
     */
    public function getEventAssignmentsGrouped($eventId)
    {
        $rows = $this->where('exam_event_id', $eventId)->findAll();
        $assignments = [];
        foreach ($rows as $row) {
            $schId = $row['exam_schedule_id'];
            $subject = $row['subject_name'];
            $roomId = $row['exam_room_id'];
            
            if (!isset($assignments[$schId])) {
                $assignments[$schId] = [];
            }
            if (!isset($assignments[$schId][$subject])) {
                $assignments[$schId][$subject] = [];
            }
            
            $assignments[$schId][$subject][$roomId] = [
                'teacher_id_1' => $row['teacher_id_1'],
                'teacher_id_2' => $row['teacher_id_2'],
            ];
        }
        return $assignments;
    }
}
