<?php
declare(strict_types=1);
require_once __DIR__.'/../shared/lib/admin_accounts.php';
function check($condition){if(!$condition)throw new RuntimeException('Administrator account assertion failed');}
class AccountDb {
 public bool $active=false,$duplicate=false,$failProfile=false,$transaction=false,$committed=false,$rolled=false;public bool $departmentActive=true,$staffDuplicate=false;public array $writes=[];
 function beginTransaction(){$this->transaction=true;}function inTransaction(){return $this->transaction;}function commit(){$this->committed=true;$this->transaction=false;}function rollBack(){$this->rolled=true;$this->transaction=false;$this->writes=[];}function lastInsertId(){return 45;}function prepare($sql){return new AccountQuery($this,$sql);}
}
class AccountQuery {
 function __construct(public AccountDb $db,public string $sql){}function execute($args){if((str_starts_with($this->sql,'INSERT INTO admins')||str_starts_with($this->sql,'INSERT INTO staff'))&&$this->db->failProfile)throw new RuntimeException('Profile write failure');if(str_starts_with($this->sql,'INSERT'))$this->db->writes[]=[$this->sql,$args];}function fetch(){if(str_contains($this->sql,'FROM departments'))return $this->db->departmentActive?['department_name'=>'ICT']:false;if(str_contains($this->sql,'FROM staff'))return $this->db->staffDuplicate?['user_id'=>2]:false;return str_contains($this->sql,'FROM campuses')?($this->db->active?['id'=>2]:false):($this->db->duplicate?['id'=>1]:false);}
}
$data=['full_name'=>'New Administrator','email'=>'ADMIN@example.org','campus_id'=>2,'password'=>'Unique admin 123!','confirm_password'=>'Unique admin 123!'];
$db=new AccountDb();$db->active=true;check(create_administrator_account($db,$data)===45);check($db->committed&&count($db->writes)===2);check($db->writes[0][1][0]==='admin@example.org');check(password_verify($data['password'],$db->writes[0][1][3]));check($db->writes[1][1]===[45,2]);
foreach(['full_name'=>'','email'=>'invalid','password'=>'short','confirm_password'=>'mismatch','campus_id'=>0] as $field=>$value){$bad=$data;$bad[$field]=$value;try{administrator_details($bad);throw new RuntimeException('Invalid input accepted');}catch(InvalidArgumentException $e){}}
foreach(['inactive','duplicate','profile_failure'] as $case){$db=new AccountDb();$db->active=$case!=='inactive';$db->duplicate=$case==='duplicate';$db->failProfile=$case==='profile_failure';try{create_administrator_account($db,$data);throw new LogicException('Expected failure');}catch(InvalidArgumentException|DomainException $e){}catch(RuntimeException $e){check($case==='profile_failure'&&$e->getMessage()==='Profile write failure');}check($db->rolled&&!$db->committed&&!$db->writes);}
echo "PASS: validation, password hashing, campus selection, duplicate rejection and transaction rollback.\n";

$staff=$data+['staff_number'=>'STAFF-12','department_id'=>3,'designation'=>'Lecturer'];
$db=new AccountDb();$db->active=true;check(create_management_account($db,$staff,'staff')===45);check($db->writes[0][1][4]==='staff');check($db->writes[1][1]===[45,'STAFF-12',null,'Lecturer',null]);
foreach(['number','write'] as $case){$db=new AccountDb();$db->active=true;$db->departmentActive=$case!=='department';$db->staffDuplicate=$case==='number';$db->failProfile=$case==='write';try{create_management_account($db,$staff,'staff');throw new LogicException('Unexpected success');}catch(InvalidArgumentException|DomainException $e){}catch(RuntimeException $e){check($case==='write'&&$e->getMessage()==='Profile write failure');}check($db->rolled&&!$db->committed&&!$db->writes);}
try{create_management_account(new AccountDb(),$data,'student');throw new RuntimeException('Invalid role accepted');}catch(InvalidArgumentException $e){}
echo "PASS: staff role, department mapping, staff numbers, duplicate rejection and rollback.\n";

$staff["campus_id"]="all";$staff["department_id"]="all";$db=new AccountDb();check(create_management_account($db,$staff,"staff")===45);check($db->writes[1][1][2]===null&&$db->writes[1][1][4]===null);echo "PASS: all campuses and departments saved without fictitious foreign keys.\n";
