<?php
namespace App\Controllers;
use App\Core\{Auth,Controller,Csrf,Permission};
use App\Models\{ProfessorContract,ProfessorContractType};
final class ProfessorContractController extends Controller{
 public function index():void{Auth::requireLogin();Permission::require('professeurs.contrats.view');$id=(int)($_GET['professeur_id']??0);$p=ProfessorContract::professor($id);if(!$p){http_response_code(404);exit('Professeur introuvable.');}$canViewArchived=ProfessorContract::canViewArchived((int)Auth::id());$this->view('professors/contracts/index',['pageTitle'=>'Contrats','professor'=>$p,'contracts'=>ProfessorContract::forProfessor($id,$canViewArchived),'canViewArchived'=>$canViewArchived]);}
 public function create():void{Auth::requireLogin();Permission::require('professeurs.contrats.create');$id=(int)($_GET['professeur_id']??0);$this->view('professors/contracts/form',['pageTitle'=>'Nouveau contrat','professor'=>ProfessorContract::professor($id),'contract'=>null,'types'=>ProfessorContractType::all()]);}
 public function store():void{Auth::requireLogin();Permission::require('professeurs.contrats.create');Csrf::verify($_POST['_csrf']??null);$d=$this->payload($_POST);$d['created_by']=(int)(Auth::user()['id']??0)?:null;ProfessorContract::save($d);$_SESSION['success']='Contrat ajouté.';$this->redirect('/professeurs/contrats?professeur_id='.(int)$_POST['professeur_id']);}
 public function edit():void{Auth::requireLogin();Permission::require('professeurs.contrats.edit');$c=ProfessorContract::find((int)($_GET['id']??0));if(!$c){http_response_code(404);exit('Contrat introuvable.');}$this->view('professors/contracts/form',['pageTitle'=>'Modifier contrat','professor'=>ProfessorContract::professor((int)$c['professeur_id']),'contract'=>$c,'types'=>ProfessorContractType::all()]);}
 public function update():void{Auth::requireLogin();Permission::require('professeurs.contrats.edit');Csrf::verify($_POST['_csrf']??null);ProfessorContract::save($this->payload($_POST),(int)$_POST['id']);$_SESSION['success']='Contrat modifié.';$this->redirect('/professeurs/contrats?professeur_id='.(int)$_POST['professeur_id']);}
 public function archive():void{Auth::requireLogin();Permission::require('professeurs.contrats.archive');Csrf::verify($_POST['_csrf']??null);ProfessorContract::archive((int)$_POST['id']);$_SESSION['success']='Contrat archivé.';$this->redirect('/professeurs/contrats?professeur_id='.(int)$_POST['professeur_id']);}
 private function payload(array $i):array{$mode=$i['anciennete_mode']??'type';return ['professeur_id'=>(int)$i['professeur_id'],'type_contrat_id'=>(int)$i['type_contrat_id'],'numero_contrat'=>trim($i['numero_contrat']??'')?:null,'date_debut'=>$i['date_debut'],'date_fin'=>$i['date_fin']?:null,'fonction'=>trim($i['fonction']??'')?:null,'volume_heures'=>$i['volume_heures']!==''?(float)$i['volume_heures']:null,'unite_heures'=>$i['unite_heures']??'periode','contrat_principal'=>isset($i['contrat_principal'])?1:0,'compte_anciennete_override'=>$mode==='type'?null:($mode==='oui'?1:0),'statut'=>$i['statut']??'actif','observations'=>trim($i['observations']??'')?:null];}
}
