<?php
// =============================================================
//  GET /api/v1/admin_allocations/unit_edit_context?id=5
//
//  Wraps app/manage_unit_assignment.php's own 'get_units_for_edit'
//  action — getUnitsForAssignment() unmodified: every Active unit not
//  yet assigned under this assignment's specialty+department, PLUS the
//  currently-assigned unit itself (flagged is_current), so the edit
//  form's unit picker can show it pre-selected even though it would
//  otherwise be excluded as "already assigned".
// =============================================================

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

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

api_authenticate();
api_require_permission('manage_assignments');

$id = (int)($_GET['id'] ?? 0);
if (!$id) Api::error(422, 'ValidationError', 'id is required');

$result = getUnitsForAssignment($conn, $id);
if (!$result['success']) {
    Api::error(404, 'NotFound', $result['message']);
}

Api::success(['units' => $result['units'], 'current_unit_id' => $result['current_unit_id']]);
