<?php
header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-cache, no-store, must-revalidate');

require_once __DIR__ . '/env_loader.php';
require_once __DIR__ . '/app/models/conexion.php';
require_once __DIR__ . '/app/models/votos.php';

use app\models\votos;

try {
    $votosModel = new votos();
    $quorum = $votosModel->datosQuorumConsolidado();

    $presentes = [];
    if (isset($_GET['lista']) && $_GET['lista'] === '1') {
        $presentes = $votosModel->listaPresentes();
    }

    echo json_encode([
        'success'           => true,
        'presentes'         => $quorum->presentes,
        'poder_presentes'   => $quorum->poder_presentes,
        'registrados'       => $quorum->registrados,
        'poder_registrados' => $quorum->poder_registrados,
        'porcentaje'        => $quorum->porcentaje,
        'quorum_alcanzado'  => $quorum->quorum_alcanzado,
        'lista_presentes'   => $presentes,
        'timestamp'         => date('Y-m-d H:i:s'),
    ], JSON_UNESCAPED_UNICODE);
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}
