<?php
require_once '../config/db.php';
header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html>
<head>
<title>Trade API Debug v2</title>
<style>
body{font-family:monospace;background:#0f172a;color:#e2e8f0;padding:20px;}
h2{color:#93c5fd;margin-top:24px;}
table{border-collapse:collapse;width:100%;margin-bottom:20px;}
th{background:#1e3a8a;color:#93c5fd;padding:8px 12px;text-align:left;}
td{padding:7px 12px;border-bottom:1px solid #1e293b;}
.ok{color:#6ee7b7;font-weight:bold;}
.err{color:#f87171;font-weight:bold;}
.warn{color:#fbbf24;font-weight:bold;}
.box{background:#1e293b;border-radius:8px;padding:14px 18px;margin-bottom:14px;border-left:3px solid #3b82f6;}
</style>
</head>
<body>
<?php

echo "<h2>Step 1 — Database connection check</h2>";
try {
    $test = $pdo->query("SELECT 1")->fetch();
    echo "<div class='box'><span class='ok'>✓ DB connected successfully</span></div>";
} catch(Exception $e) {
    echo "<div class='box'><span class='err'>✗ DB Error: " . $e->getMessage() . "</span></div>";
    exit;
}

// ── Konsa database use ho raha hai ──
echo "<h2>Step 2 — Current Database Name</h2>";
$db = $pdo->query("SELECT DATABASE()")->fetchColumn();
echo "<div class='box'>Current DB: <span class='ok'>$db</span></div>";

// ── Saari tables list ──
echo "<h2>Step 3 — All Tables in this Database</h2>";
$tables = $pdo->query("SHOW TABLES")->fetchAll(PDO::FETCH_COLUMN);
echo "<div class='box'>";
foreach($tables as $t) {
    $highlight = (stripos($t, 'trade') !== false) ? " <span class='ok'>← trade table!</span>" : "";
    echo "• $t $highlight<br>";
}
echo "</div>";

// ── trade_logs exact check ──
echo "<h2>Step 4 — trade_logs table check</h2>";
$exists = in_array('trade_logs', $tables);
if(!$exists) {
    echo "<div class='box'><span class='err'>✗ 'trade_logs' table does NOT exist in DB: $db</span><br>";
    echo "<span class='warn'>PhpMyAdmin mein check karo — shayad alag database mein hai</span></div>";
} else {
    echo "<div class='box'><span class='ok'>✓ trade_logs table exists</span></div>";
    
    // Count
    $cnt = $pdo->query("SELECT COUNT(*) FROM trade_logs")->fetchColumn();
    echo "<div class='box'>Total rows: <span class='ok'>$cnt</span></div>";
    
    // All rows
    $rows = $pdo->query("SELECT * FROM trade_logs ORDER BY id DESC")->fetchAll(PDO::FETCH_ASSOC);
    if($rows) {
        echo "<table><tr>";
        foreach(array_keys($rows[0]) as $col) echo "<th>$col</th>";
        echo "</tr>";
        foreach($rows as $r) {
            echo "<tr>";
            foreach($r as $v) echo "<td>".htmlspecialchars($v)."</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
}

// ── config/db.php ka path check ──
echo "<h2>Step 5 — db.php config check</h2>";
$dbFile = realpath('../config/db.php');
echo "<div class='box'>db.php path: <span class='warn'>$dbFile</span><br>";
if(file_exists($dbFile)) {
    $content = file_get_contents($dbFile);
    // Extract dbname only
    preg_match('/dbname=([^;]+)/i', $content, $m);
    preg_match('/host=([^;]+)/i', $content, $h);
    echo "Host: <span class='ok'>" . ($h[1] ?? 'not found') . "</span><br>";
    echo "DBName in DSN: <span class='ok'>" . ($m[1] ?? 'not found') . "</span>";
} else {
    echo "<span class='err'>db.php not found at this path!</span>";
}
echo "</div>";

?>
</body>
</html>