<?php
// Check Data in Tables
header('Content-Type: text/html; charset=utf-8');
require_once __DIR__ . '/config/config.php';
require_once __DIR__ . '/config/database.php';

echo "<h2>📊 Database Data Inspector for <code>" . htmlspecialchars(DB_NAME) . "</code></h2>";

$db = Database::getInstance();

// 1. Check Users
echo "<h3>1. Users Table (ผู้ใช้ทั้งหมดในระบบ)</h3>";
$users = $db->query("SELECT id, username, email, full_name, role, status, created_at FROM users ORDER BY id ASC")->fetchAll();
echo "<table border='1' cellpadding='6' style='border-collapse:collapse;'>";
echo "<tr style='background:#f2f2f2;'><th>ID</th><th>Username</th><th>Email</th><th>Full Name</th><th>Role</th><th>Status</th><th>Created At</th></tr>";
foreach ($users as $u) {
    echo "<tr>";
    echo "<td>" . $u['id'] . "</td>";
    echo "<td><strong>" . htmlspecialchars($u['username']) . "</strong></td>";
    echo "<td>" . htmlspecialchars($u['email']) . "</td>";
    echo "<td>" . htmlspecialchars($u['full_name']) . "</td>";
    echo "<td>" . htmlspecialchars($u['role']) . "</td>";
    echo "<td>" . htmlspecialchars($u['status']) . "</td>";
    echo "<td>" . htmlspecialchars($u['created_at']) . "</td>";
    echo "</tr>";
}
echo "</table>";

// 2. Check Products
echo "<h3>2. Products Table (สินค้าทั้งหมดในระบบ)</h3>";
$products = $db->query("SELECT p.id, p.title, p.price, p.sale_price, p.stock_quantity, p.status, sp.shop_name FROM products p LEFT JOIN seller_profiles sp ON p.seller_id = sp.id ORDER BY p.id ASC")->fetchAll();
echo "<table border='1' cellpadding='6' style='border-collapse:collapse;'>";
echo "<tr style='background:#f2f2f2;'><th>ID</th><th>Title</th><th>Price</th><th>Stock</th><th>Status</th><th>Shop Name</th></tr>";
foreach ($products as $p) {
    echo "<tr>";
    echo "<td>" . $p['id'] . "</td>";
    echo "<td>" . htmlspecialchars($p['title']) . "</td>";
    echo "<td>฿" . number_format($p['price'], 2) . "</td>";
    echo "<td>" . $p['stock_quantity'] . "</td>";
    echo "<td>" . htmlspecialchars($p['status']) . "</td>";
    echo "<td>" . htmlspecialchars($p['shop_name'] ?? 'None') . "</td>";
    echo "</tr>";
}
echo "</table>";

// 3. Check Orders
echo "<h3>3. Orders Table (ประวัติคำสั่งซื้อ)</h3>";
$orders = $db->query("SELECT o.id, o.order_number, u.username, o.total_amount, o.status, o.created_at FROM orders o LEFT JOIN users u ON o.user_id = u.id ORDER BY o.id DESC")->fetchAll();
echo "<table border='1' cellpadding='6' style='border-collapse:collapse;'>";
echo "<tr style='background:#f2f2f2;'><th>ID</th><th>Order No.</th><th>User</th><th>Total</th><th>Status</th><th>Created At</th></tr>";
foreach ($orders as $o) {
    echo "<tr>";
    echo "<td>" . $o['id'] . "</td>";
    echo "<td>" . htmlspecialchars($o['order_number']) . "</td>";
    echo "<td>" . htmlspecialchars($o['username'] ?? 'None') . "</td>";
    echo "<td>฿" . number_format($o['total_amount'], 2) . "</td>";
    echo "<td>" . htmlspecialchars($o['status']) . "</td>";
    echo "<td>" . htmlspecialchars($o['created_at']) . "</td>";
    echo "</tr>";
}
echo "</table>";
