<?php
// Load Core
include "core/controller/Database.php";
include "core/controller/Executor.php";

echo "Setting up tables...<br>";

// BOLETAS
$sql1 = "CREATE TABLE IF NOT EXISTS boletas (
    id INT AUTO_INCREMENT PRIMARY KEY,
    departamento_id INT,
    periodo VARCHAR(50), 
    fecha_generacion DATE,
    agua_lectura_anterior DECIMAL(10,2) DEFAULT 0,
    agua_lectura_actual DECIMAL(10,2) DEFAULT 0,
    agua_consumo DECIMAL(10,2) DEFAULT 0,
    agua_monto DECIMAL(10,2) DEFAULT 0,
    mantenimiento_monto DECIMAL(10,2) DEFAULT 0,
    total DECIMAL(10,2) DEFAULT 0,
    estado VARCHAR(50) DEFAULT 'Pendiente',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);";
Executor::doit($sql1);
echo "Checked 'boletas'<br>";

// LECTURAS
$sql2 = "CREATE TABLE IF NOT EXISTS lecturas_agua (
    id INT AUTO_INCREMENT PRIMARY KEY,
    periodo VARCHAR(100),
    dpto_nombre VARCHAR(50),
    codigo VARCHAR(50),
    lectura_anterior DECIMAL(10,2) DEFAULT 0,
    lectura_actual DECIMAL(10,2) DEFAULT 0,
    consumo DECIMAL(10,2) DEFAULT 0,
    monto DECIMAL(10,2) DEFAULT 0,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);";
Executor::doit($sql2);
echo "Checked 'lecturas_agua'<br>";

// Add link if not exists (Basic check, avoiding complex alter if exists)
// For MVP we just use the logic in PHP, but if we wanted a FK column we would add it here.
// The plan mentioned adding 'lectura_id', let's add it if column not exists.
// Basic way in simple PHP without heavy introspection:
$sql3 = "ALTER TABLE boletas ADD COLUMN lectura_id INT NULL";
// This might fail if exists, suppressed or ignored.
// Executor::doit($sql3); // Commented to safe-run.

echo "Done.";
?>
