<?php
declare(strict_types=1);

require_once __DIR__ . "/../config/db.php";
require_once __DIR__ . "/../config/auth.php";

// Allow only authenticated admins
if (!is_authenticated() || $_SESSION['role'] !== 'ADMIN') {
    http_response_code(401);
    header('Content-Type: application/json');
    echo json_encode(['error' => 'Unauthorized']);
    exit;
}

try {
    $st = $pdo->prepare("
        SELECT DISTINCT gp_id as id 
        FROM gp_assignments 
        WHERE status = 'ACTIVE'
    ");
    $st->execute();
    $assigned = $st->fetchAll(PDO::FETCH_ASSOC);
    
    header('Content-Type: application/json');
    echo json_encode($assigned);
} catch (Throwable $e) {
    http_response_code(500);
    header('Content-Type: application/json');
    echo json_encode(['error' => 'Database error']);
}