<?php
// Standalone database diagnostic tool
define('LARAVEL_START', microtime(true));

try {
    require __DIR__.'/../vendor/autoload.php';
    $app = require_once __DIR__.'/../bootstrap/app.php';
    $kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
    $kernel->bootstrap();

    echo "<h2>Diagnostik Database Transaksi QRIS</h2>";

    // 1. Check if column exists
    $hasColumn = Illuminate\Support\Facades\Schema::hasColumn('transaksi', 'bukti_bayar');
    echo "Kolom 'bukti_bayar' di tabel 'transaksi': " . ($hasColumn ? "✅ ADA" : "❌ TIDAK ADA") . "<br><br>";

    // 2. Query last 10 transactions
    $transactions = Illuminate\Support\Facades\DB::table('transaksi')
        ->orderBy('id', 'desc')
        ->take(10)
        ->get();

    echo "<table border='1' cellpadding='8' style='border-collapse: collapse; font-family: sans-serif; font-size: 13px;'>";
    echo "<tr style='background: #f3f4f6;'>
            <th>ID</th>
            <th>Pelanggan ID</th>
            <th>Metode Bayar</th>
            <th>Bukti Bayar (DB)</th>
            <th>Total Harga</th>
            <th>Status</th>
            <th>Tanggal dibuat</th>
          </tr>";

    foreach ($transactions as $trx) {
        $bukti = isset($trx->bukti_bayar) ? $trx->bukti_bayar : 'KOLOM TIDAK ADA';
        $style = $trx->metode_bayar === 'Qris' ? "style='background: #faf5ff;'" : "";
        echo "<tr $style>
                <td>{$trx->id}</td>
                <td>{$trx->customers_id}</td>
                <td style='font-weight: bold;'>{$trx->metode_bayar}</td>
                <td style='color: " . ($bukti ? 'green' : 'red') . "; font-weight: bold;'>" . ($bukti ? htmlspecialchars($bukti) : 'NULL/KOSONG') . "</td>
                <td>Rp " . number_format($trx->total_harga, 0, ',', '.') . "</td>
                <td>{$trx->status}</td>
                <td>{$trx->created_at}</td>
              </tr>";
    }
    echo "</table>";

} catch (\Exception $e) {
    echo "⚠️ Terjadi kesalahan: " . $e->getMessage();
}
