<?php
require_once 'config/init.php';
require_once 'includes/billing.php';

$db = getDB();
$billing = new BillingCalculator($db);

// Get test consumption records
$stmt = $db->prepare('SELECT id FROM consumption WHERE subscriber_id = 1 ORDER BY id');
$result = $stmt->execute();

while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
    $invoice = $billing->calculateInvoice($row['id']);
    
    // Insert invoice
    $stmt2 = $db->prepare('INSERT INTO invoices (consumption_id, subscriber_id, fixed_fees, collective_entitlement_amount, consumption_amount, penalties, total_amount) VALUES (:consumption_id, :subscriber_id, :fixed_fees, :collective_entitlement_amount, :consumption_amount, :penalties, :total_amount)');
    $stmt2->bindValue(':consumption_id', $invoice['consumption_id'], SQLITE3_INTEGER);
    $stmt2->bindValue(':subscriber_id', $invoice['subscriber_id'], SQLITE3_INTEGER);
    $stmt2->bindValue(':fixed_fees', $invoice['fixed_fees'], SQLITE3_FLOAT);
    $stmt2->bindValue(':collective_entitlement_amount', $invoice['collective_entitlement_amount'], SQLITE3_FLOAT);
    $stmt2->bindValue(':consumption_amount', $invoice['consumption_amount'], SQLITE3_FLOAT);
    $stmt2->bindValue(':penalties', $invoice['penalties'], SQLITE3_FLOAT);
    $stmt2->bindValue(':total_amount', $invoice['total_amount'], SQLITE3_FLOAT);
    $stmt2->execute();
    
    echo "Invoice created for consumption ID: " . $row['id'] . "\n";
    echo "Fixed Fees: " . $invoice['fixed_fees'] . "\n";
    echo "Collective Entitlement: " . $invoice['collective_entitlement_amount'] . "\n";
    echo "Consumption Amount: " . $invoice['consumption_amount'] . "\n";
    echo "Total Amount: " . $invoice['total_amount'] . "\n\n";
}

echo "Done!\n";
