<?php
declare(strict_types=1);
namespace App\Controllers;

use App\Core\Auth;
use App\Core\Controller;
use App\Core\Permission;
use App\Models\Professor;
use App\Models\ProfessorAgenda;

final class ProfessorAgendaController extends Controller
{
    public function index(): void
    {
        Auth::requireLogin();Permission::require('professeurs.view');
        $id=max(0,(int)($_GET['professeur']??0));$professor=Professor::find($id);
        if(!$professor){http_response_code(404);exit('Professeur introuvable.');}
        $mode=in_array($_GET['vue']??'week',['day','week','month'],true)?(string)$_GET['vue']:'week';
        $anchor=(string)($_GET['date']??date('Y-m-d'));[$from,$to]=ProfessorAgenda::range($mode,$anchor);
        $this->view('professors/agenda',['pageTitle'=>'Agenda professeur','professor'=>$professor,'mode'=>$mode,'anchor'=>$anchor,'from'=>$from,'to'=>$to,'events'=>ProfessorAgenda::events($id,$from,$to)]);
    }
}
