<?php
/**
 * NIS Asset Management System
 * API: Get Formations by Zone ID
 */

require_once __DIR__ . '/../config/init.php';

header('Content-Type: application/json');

// Check if user is logged in using your framework's Auth class
if (!Auth::check()) {
    http_response_code(401);
    echo json_encode(['error' => 'Unauthorized']);
    exit;
}

$zoneId = isset($_GET['zone_id']) ? (int)$_GET['zone_id'] : 0;

if (!$zoneId) {
    echo json_encode([]);
    exit;
}

// Use your framework's Database class to fetch formations
$formations = Database::fetchAll(
    "SELECT id, formation_name, formation_code FROM formations WHERE zone_id = ? ORDER BY formation_name",
    [$zoneId]
);

if ($formations === false) {
    echo json_encode([]);
    exit;
}

echo json_encode($formations);