<?php
/**
 * SAC 2026 - API: Obtener Hist¨®rico de Recaudaci¨®n Mensual
 * Retorna el listado de facturaci¨®n vs recaudaci¨®n mes a mes.
 */
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');
header('Access-Control-Allow-Headers: Content-Type');

require_once 'config.php';

try {
    // Usar la conexi¨®n global $pdo ya inicializada en config.php
    $stmt = $pdo->query("SELECT id, month, year, amount_billed as amountBilled, amount_collected as amountCollected, created_at as createdAt FROM monthly_collection ORDER BY year DESC, month DESC");
    $data = $stmt->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode($data);
} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode(['error' => 'Database error: ' . $e->getMessage()]);
}
?>
