<?php
/**
 * SAC 2026 - GET SYSTEM BANK MOVEMENTS v1.0
 * Fetches bank transactions for reconciliation.
 */
require_once 'config.php';

if (!isset($_SESSION['user_id'])) response(["error" => "No autorizado"], 401);

$month = isset($_GET['month']) ? (int)$_GET['month'] : null;
$year = isset($_GET['year']) ? (int)$_GET['year'] : null;

if (!$month || !$year) {
    response(["error" => "Mes y año requeridos"], 400);
}

try {
    // Filter by month and year in the 'date' column (YYYY-MM-DD)
    $startDate = sprintf("%04d-%02d-01", $year, $month);
    $endDate = date("Y-m-t", strtotime($startDate));

    $stmt = $pdo->prepare("SELECT reference, amount_bs as amount FROM bank_transactions WHERE date BETWEEN ? AND ?");
    $stmt->execute([$startDate, $endDate]);
    $movements = $stmt->fetchAll();

    response([
        "success" => true,
        "movements" => $movements
    ]);
} catch (Exception $e) {
    response(["error" => "Error al obtener movimientos: " . $e->getMessage()], 500);
}
?>