<?php
// =============================================================
//  POST /api/v1/admin_allocations/unit_permanent_delete
//
//  Wraps app/trash.php's 'permanent_delete_unit' action —
//  permanentDeleteUnitAssignment() unmodified: the DELETE itself is
//  scoped to `id = ? AND is_active = 'Inactive'`, so this can only
//  ever remove an already-trashed row.
//
//  Body: { "id": 5 }
// =============================================================

require_once __DIR__ . '/../_bootstrap.php';
require_once __DIR__ . '/../../../include/db_functions.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 assignment');

$result = permanentDeleteUnitAssignment($conn, $id);
if (!$result['success']) {
    Api::error(500, 'ServerError', $result['message']);
}

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

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