<?php
// =============================================================
//  POST /api/v1/admin_allocations/unit_delete
//
//  Wraps app/manage_unit_assignment.php's own 'soft_delete_unit' action
//  — softDeleteUnitAssignment() unmodified: an unconditional soft
//  delete (unlike specialty_department_delete.php's own
//  softDeleteSpecialtyDepartment(), this one has no "still in use"
//  guard — there is nothing downstream of a unit assignment for it to
//  protect, matching the desktop page's own plain confirm dialog with
//  no reason collected either).
//
//  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 = softDeleteUnitAssignment($conn, $id, $account->getFullName());
if (!$result['success']) {
    Api::error(409, 'DeleteFailed', $result['message']);
}

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

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