<?php
session_start();
header('Content-Type: application/json; charset=utf-8');
require_once(__DIR__ . '/../db/conexion.php');

$response = ['success' => false, 'data' => []];

// Solo admins pueden acceder
if (!isset($_SESSION['rol']) || !in_array($_SESSION['rol'], ['admin', 'adminTi'])) {
    echo json_encode($response);
    exit;
}

$attuid = $_GET['attuid'] ?? '';
if (empty($attuid)) {
    echo json_encode($response);
    exit;
}

$sql = "SELECT id, nombre, apellido_paterno, apellido_materno, puesto, estatus_actual 
        FROM aspirantes 
        WHERE attuid = ?
          AND estatus_actual NOT IN ('rechazado', 'contratado')
        ORDER BY fecha_registro DESC";

$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $attuid);
$stmt->execute();
$result = $stmt->get_result();

$aspirantes = [];
while ($row = $result->fetch_assoc()) {
    $aspirantes[] = [
        'id' => $row['id'],
        'nombre_completo' => $row['nombre'] . ' ' . $row['apellido_paterno'] . ' ' . $row['apellido_materno'],
        'puesto' => $row['puesto'],
        'estatus' => $row['estatus_actual']
    ];
}

$response['success'] = true;
$response['aspirantes'] = $aspirantes;

echo json_encode($response);
