<?php
// =============================================================
//  POST /api/v1/admin_allocations/school_unit_permanent_delete
//
//  Wraps app/manage_clinical_unit_assignment.php's own
//  'permanent_delete_school_unit' action — a real, unrecoverable
//  DELETE, only ever allowed on a row already in trash
//  (is_active='Inactive'), matching the desktop page's own two-step
//  confirmation ("Move to Trash" then "Delete Forever").
//
//  Body: { "id": 5 }
// =============================================================

require_once __DIR__ . '/../_bootstrap.php';

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    Api::error(405, 'MethodNotAllowed', 'Use POST');
}

$account = api_authenticate();
api_require_permission('manage_assignments');

$body = Api::body();
$id = (int)($body['id'] ?? 0);
if (!$id) Api::error(422, 'ValidationError', 'Invalid ID');

$permManager = new PermissionManager($conn);
$permManager->logAudit($account->getId(), 'permanent_delete_school_unit', 'school_unit_assignments', $id);

$stmt = $conn->prepare("DELETE FROM school_unit_assignments WHERE id = ? AND is_active = 'Inactive'");
$stmt->bind_param('i', $id);
if (!$stmt->execute() || $stmt->affected_rows === 0) {
    $stmt->close();
    Api::error(404, 'NotFound', 'Assignment not found in trash.');
}
$stmt->close();

Api::success(['id' => $id]);
