<?php
chdir(dirname(__DIR__));
require_once 'includes/db.php';

echo "=== invoice_payments table structure ===\n";
try {
    $rows = fetchAll('DESCRIBE invoice_payments');
    foreach ($rows as $row) {
        echo $row['Field'] . ' | ' . $row['Type'] . ' | ' . $row['Null'] . ' | ' . $row['Key'] . "\n";
    }
} catch (Exception $e) {
    echo "ERROR: " . $e->getMessage() . "\n";
}

echo "\n=== Sample payments ===\n";
try {
    $payments = fetchAll('SELECT * FROM invoice_payments LIMIT 5');
    if (empty($payments)) {
        echo "(no records yet)\n";
    } else {
        foreach ($payments as $p) {
            echo json_encode($p) . "\n";
        }
    }
} catch (Exception $e) {
    echo "ERROR: " . $e->getMessage() . "\n";
}

echo "\n=== Invoice statuses ===\n";
try {
    $statuses = fetchAll("SELECT status, COUNT(*) as cnt FROM production_invoices GROUP BY status");
    foreach ($statuses as $s) {
        echo $s['status'] . ' => ' . $s['cnt'] . "\n";
    }
} catch (Exception $e) {
    echo "ERROR: " . $e->getMessage() . "\n";
}
