<?php
declare(strict_types=1);
namespace App\Controllers;
use App\Core\Auth;
use App\Core\Controller;
use App\Core\Csrf;
use App\Core\Permission;
use App\Identity\IdentityRepository;
final class IdentityController extends Controller
{
    private function repo():IdentityRepository{return new IdentityRepository();}
    public function index():void{Auth::requireLogin();Permission::require('axiom.meta.view');$q=trim((string)($_GET['q']??''));$type=trim((string)($_GET['type']??''));$this->view('identity/index',['pageTitle'=>'AXIOM Identity Engine','stats'=>$this->repo()->stats(),'identities'=>$this->repo()->search($q,$type),'q'=>$q,'type'=>$type]);}
    public function create():void{Auth::requireLogin();Permission::require('axiom.meta.manage');$this->view('identity/form',['pageTitle'=>'Nouvelle identité']);}
    public function store():void{Auth::requireLogin();Permission::require('axiom.meta.manage');Csrf::verify($_POST['_csrf']??null);$type=in_array($_POST['type_identite']??'',['personne','organisation'],true)?$_POST['type_identite']:'personne';$nom=mb_strtoupper(trim((string)($_POST['nom']??'')),'UTF-8');$raison=trim((string)($_POST['raison_sociale']??''));if($type==='personne'&&$nom===''){$_SESSION['error']='Le nom est obligatoire.';$this->redirect('/administration/axiom/identites/nouveau');}if($type==='organisation'&&$raison===''){$_SESSION['error']='La raison sociale est obligatoire.';$this->redirect('/administration/axiom/identites/nouveau');}
        $d=['organisation_id'=>(int)(Auth::user()['organisation_id']??1),'type_identite'=>$type,'civilite'=>trim((string)($_POST['civilite']??''))?:null,'nom'=>$type==='organisation'?mb_strtoupper($raison,'UTF-8'):$nom,'prenom'=>trim((string)($_POST['prenom']??''))?:null,'raison_sociale'=>$type==='organisation'?$raison:null,'date_naissance'=>trim((string)($_POST['date_naissance']??''))?:null,'identifiant_externe'=>trim((string)($_POST['identifiant_externe']??''))?:null,'notes'=>trim((string)($_POST['notes']??''))?:null,'roles'=>array_values(array_filter((array)($_POST['roles']??[]))),'email'=>trim((string)($_POST['email']??'')),'telephone'=>trim((string)($_POST['telephone']??'')),'ligne1'=>trim((string)($_POST['ligne1']??'')),'ligne2'=>trim((string)($_POST['ligne2']??'')),'code_postal'=>trim((string)($_POST['code_postal']??'')),'ville'=>trim((string)($_POST['ville']??'')),'pays'=>trim((string)($_POST['pays']??'France'))?:'France'];
        $id=$this->repo()->create($d,(int)Auth::id());$_SESSION['success']='Identité créée.';$this->redirect('/administration/axiom/identites/voir?id='.$id);
    }
    public function show():void{Auth::requireLogin();Permission::require('axiom.meta.view');$i=$this->repo()->find((int)($_GET['id']??0));if(!$i){http_response_code(404);exit('Identité introuvable.');}$this->view('identity/show',['pageTitle'=>'Fiche identité','identity'=>$i]);}
    public function archive():void{Auth::requireLogin();Permission::require('axiom.meta.manage');Csrf::verify($_POST['_csrf']??null);$this->repo()->archive((int)($_POST['id']??0),(int)Auth::id());$_SESSION['success']='Identité archivée.';$this->redirect('/administration/axiom/identites');}
}
