<?php
require_once __DIR__ . '/../../config/init.php';

$db = getDB();

try {
    // Create subscriber_debts table
    $db->exec("CREATE TABLE IF NOT EXISTS subscriber_debts (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        subscriber_id INTEGER NOT NULL,
        meter_number TEXT NOT NULL,
        subscription_fee DECIMAL(10,2) NOT NULL,
        deposit_amount DECIMAL(10,2) NOT NULL,
        subscription_date DATE NOT NULL,
        total_debt DECIMAL(10,2) NOT NULL,
        remaining_debt DECIMAL(10,2) NOT NULL,
        created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
        FOREIGN KEY (subscriber_id) REFERENCES subscribers(id) ON DELETE CASCADE
    )");

    // Create debt_payments table
    $db->exec("CREATE TABLE IF NOT EXISTS debt_payments (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        debt_id INTEGER NOT NULL,
        payment_amount DECIMAL(10,2) NOT NULL,
        payment_date DATE NOT NULL,
        created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
        FOREIGN KEY (debt_id) REFERENCES subscriber_debts(id) ON DELETE CASCADE
    )");

    echo "Tables created successfully\n";
} catch(PDOException $e) {
    echo "Error creating tables: " . $e->getMessage() . "\n";
}
?>
