<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Core\Auth;
use App\Core\Controller;
use App\Core\Csrf;
use App\Core\Response;
use App\Core\Session;
use App\Services\TherapistService;
use InvalidArgumentException;

final class TherapistController extends Controller
{
    public function __construct(private ?TherapistService $service = null) { $this->service ??= new TherapistService(); }

    public function index(): Response
    {
        $result = $this->service->list($_GET);
        return $this->view('therapists/index', ['pageTitle'=>'Therapists','pageDescription'=>'Manage therapist profiles and professional details.','therapists'=>$result['items'],'pagination'=>$result,'filters'=>['search'=>(string)($_GET['search']??''),'status'=>(string)($_GET['status']??'')],'message'=>Session::pull('therapist_message'),'error'=>Session::pull('therapist_error')]);
    }

    public function createForm(): Response
    {
        return $this->view('therapists/form', ['pageTitle'=>'Create Therapist','therapist'=>null,'users'=>$this->service->availableUsers(),'csrf'=>Csrf::token(),'error'=>Session::pull('therapist_error'),'form'=>Session::pull('therapist_form')]);
    }

    public function create(): Response
    {
        Csrf::verify((string)($_POST['_csrf']??''));
        try { $id=$this->service->create((int)($_POST['user_id']??0),trim((string)($_POST['professional_title']??'')),trim((string)($_POST['registration_number']??'')),trim((string)($_POST['status']??'active')),trim((string)($_POST['notes']??'')),Auth::id()??0); Session::put('therapist_message','Therapist profile created successfully.'); return Response::redirect('/crm/public/therapists/'.$id.'/edit'); }
        catch (InvalidArgumentException $e) { Session::put('therapist_error',$e->getMessage()); Session::put('therapist_form',$_POST); return Response::redirect('/crm/public/therapists/create'); }
    }

    public function editForm(int $id): Response
    {
        $therapist=$this->service->find($id);
        return $this->view('therapists/form', ['pageTitle'=>'Edit Therapist','therapist'=>$therapist,'users'=>[],'csrf'=>Csrf::token(),'error'=>Session::pull('therapist_error'),'message'=>Session::pull('therapist_message'),'form'=>Session::pull('therapist_form')]);
    }

    public function delete(int $id): Response
    {
        Csrf::verify((string)($_POST['_csrf']??''));
        try {
            $this->service->delete($id, Auth::id()??0);
            Session::put('therapist_message','Therapist profile archived successfully.');
        } catch (InvalidArgumentException $e) {
            Session::put('therapist_error',$e->getMessage());
        }
        return Response::redirect('/crm/public/therapists');
    }

    public function update(int $id): Response
    {
        Csrf::verify((string)($_POST['_csrf']??''));
        try { $this->service->update($id,trim((string)($_POST['professional_title']??'')),trim((string)($_POST['registration_number']??'')),trim((string)($_POST['status']??'active')),trim((string)($_POST['notes']??'')),Auth::id()??0); Session::put('therapist_message','Therapist profile updated successfully.'); }
        catch (InvalidArgumentException $e) { Session::put('therapist_error',$e->getMessage()); Session::put('therapist_form',$_POST); }
        return Response::redirect('/crm/public/therapists/'.$id.'/edit');
    }
}
