<?php
declare(strict_types=1);
function administrator_details(array $data,bool $allCampuses=false): array {
 $name=trim((string)($data['full_name']??''));$email=strtolower(trim((string)($data['email']??'')));$password=(string)($data['password']??'');$confirm=(string)($data['confirm_password']??'');$campus=filter_var($data['campus_id']??null,FILTER_VALIDATE_INT);
 if($name===''||strlen($name)>150||in_array(strtolower($name),['null','undefined'],true))throw new InvalidArgumentException('Enter the account holder’s full name (up to 150 bytes).');
 if(strlen($email)>150||!filter_var($email,FILTER_VALIDATE_EMAIL))throw new InvalidArgumentException('Enter a valid email address up to 150 characters.');
 if(strlen($password)<12||strlen($password)>72)throw new InvalidArgumentException('Use a password between 12 and 72 bytes.');
 if(!hash_equals($password,$confirm))throw new InvalidArgumentException('The password entries do not match.');
 if(!$allCampuses&&(!$campus||$campus<1))throw new InvalidArgumentException('Choose an active campus.');
 return [$name,$email,$password,$allCampuses?null:(int)$campus];
}
function create_administrator_account($database,array $data): int {
 return create_management_account($database,$data,'admin');
}
function create_management_account($database,array $data,string $role): int {
 if(!in_array($role,['admin','staff'],true))throw new InvalidArgumentException('Choose administrator or staff.');
 [$name,$email,$password,$campus]=administrator_details($data,$role==='staff');
 $number=trim((string)($data['staff_number']??''));$designation=trim((string)($data['designation']??''));
 if($role==='staff'&&($number===''||strlen($number)>40||strlen($designation)>150))throw new InvalidArgumentException('Enter a staff number (up to 40 characters), a designation up to 150 characters.');
 $database->beginTransaction();
 try{
  if($role==='admin'){$q=$database->prepare('SELECT id FROM campuses WHERE id=? AND is_active=1');$q->execute([$campus]);if(!$q->fetch())throw new InvalidArgumentException('Choose an active campus.');}
  $q=$database->prepare('SELECT id FROM users WHERE email=? OR username=?');$q->execute([$email,$email]);if($q->fetch())throw new DomainException('An account already uses this email address.');
  $departmentName=null;
  if($role==='staff'){
   $q=$database->prepare('SELECT user_id FROM staff WHERE staff_number=?');$q->execute([$number]);if($q->fetch())throw new DomainException('This staff number is already in use.');
  }
  $hash=password_hash($password,PASSWORD_DEFAULT);
  $database->prepare("INSERT INTO users(username,full_name,email,password_hash,role,is_active) VALUES(?,?,?,?,?,1)")->execute([$email,$name,$email,$hash,$role]);$uid=(int)$database->lastInsertId();
  if($role==='admin')$database->prepare('INSERT INTO admins(user_id,campus_id) VALUES(?,?)')->execute([$uid,$campus]);
  else $database->prepare('INSERT INTO staff(user_id,staff_number,department,designation,campus_id) VALUES(?,?,?,?,?)')->execute([$uid,$number,$departmentName,$designation?:null,$campus]);
  $database->commit();return $uid;
 }catch(Throwable $e){if($database->inTransaction())$database->rollBack();if((int)$e->getCode()===1062)throw new DomainException('This email address or staff number is already in use.');throw $e;}
}
