<?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\BillingRateService;use InvalidArgumentException;
final class BillingRateController extends Controller {
 public function __construct(private ?BillingRateService $service=null){$this->service??=new BillingRateService();}
 public function index():Response{$p=(int)($_GET['patient_id']??0);$d=$this->service->data($p);return $this->view('billing-rates/index',['pageTitle'=>'Patient Billing Rates','pageDescription'=>'Set patient-specific prices for clinical services.','patientId'=>$p,'csrf'=>Csrf::token(),'error'=>Session::pull('billing_rate_error'),'message'=>Session::pull('billing_rate_message'),...$d]);}
 public function save():Response{Csrf::verify((string)($_POST['_csrf']??''));$p=(int)($_POST['patient_id']??0);try{$this->service->save($_POST);Session::put('billing_rate_message','Patient billing rate saved.');}catch(InvalidArgumentException $e){Session::put('billing_rate_error',$e->getMessage());}return Response::redirect('/crm/public/billing-rates?patient_id='.$p);}
 public function delete(int $id):Response{Csrf::verify((string)($_POST['_csrf']??''));$this->service->delete($id);Session::put('billing_rate_message','Billing rate removed.');return Response::redirect('/crm/public/billing-rates?patient_id='.(int)($_POST['patient_id']??0));}
}
