<?php
// =============================================================
//  POST /api/v1/admin_allocations/specialty_department_restore
//
//  Wraps app/trash.php's 'restore_specialty_dept' action —
//  restoreSpecialtyDepartment() unmodified: refuses the restore (with
//  a clear message) if an Active allocation for this same
//  specialty-department pair already exists (created independently
//  while this one was in the trash), otherwise sets is_active='Active'
//  and clears deleted_by/deleted_at.
//
//  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_allocations');

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

$result = restoreSpecialtyDepartment($conn, $id, $account->getFullName());
if (!$result['success']) {
    Api::error(409, 'RestoreFailed', $result['message']);
}

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

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