<?php
require_once __DIR__.'/../../_bootstrap.php';$u=apiUsuario($pdo);$uid=(int)$u['id'];
if($_SERVER['REQUEST_METHOD']==='GET')jsonResponse(['ok'=>true,'types'=>afPrivacyRequestTypes(),'statuses'=>afPrivacyRequestStatuses(),'subjects'=>afPrivacySubjects($pdo,$uid),'requests'=>afPrivacyRequestsForUser($pdo,$uid)]);
$d=requestJson();
if($_SERVER['REQUEST_METHOD']==='POST'){
 try{$result=afCreatePrivacyRequest($pdo,$uid,(int)($d['subject_user_id']??$uid),(string)($d['request_type']??''),(string)($d['description']??''));jsonResponse(['ok'=>true,'request'=>$result],201);}catch(Throwable $e){jsonResponse(['ok'=>false,'erro'=>$e->getMessage()],422);}
}
if(in_array($_SERVER['REQUEST_METHOD'],['PATCH','DELETE'],true)){
 try{if(!afCancelPrivacyRequest($pdo,$uid,(int)($d['request_id']??0)))throw new RuntimeException('A solicitação não pode ser cancelada.');jsonResponse(['ok'=>true]);}catch(Throwable $e){jsonResponse(['ok'=>false,'erro'=>$e->getMessage()],422);}
}
jsonResponse(['ok'=>false,'erro'=>'Método não permitido.'],405);
