<?php
require_once __DIR__ . '/../config/auth.php';
require_once __DIR__ . '/../includes/functions.php';

if ($_SESSION['role_id'] != 1) {
    $_SESSION['error'] = 'Only Super Admin can access debug page.';
    header('Location: ' . getRedirectUrl());
    exit;
}

$pdo = getDB();
$output = [];

// Total active products
$stmt = $pdo->query("SELECT COUNT(*) FROM products WHERE status = 1");
$output['Total Active Products'] = (int)$stmt->fetchColumn();

// By type
foreach (['phone', 'accessory', 'spare_part'] as $type) {
    $stmt = $pdo->prepare("SELECT COUNT(*) FROM products WHERE status = 1 AND product_type = ?");
    $stmt->execute([$type]);
    $output["Active $type products"] = (int)$stmt->fetchColumn();
}

// Products with stock > 0
$stmt = $pdo->query("SELECT COUNT(*) FROM products WHERE status = 1 AND current_stock > 0");
$output['Products with current_stock > 0'] = (int)$stmt->fetchColumn();

// Products hidden because stock <= 0 (non-phone)
$stmt = $pdo->query("SELECT COUNT(*) FROM products WHERE status = 1 AND product_type IN ('accessory','spare_part') AND current_stock <= 0");
$output['Accessories/Spare Parts hidden (stock <= 0)'] = (int)$stmt->fetchColumn();

// Phone products
$stmt = $pdo->query("SELECT COUNT(*) FROM products WHERE status = 1 AND product_type = 'phone'");
$total_phones = (int)$stmt->fetchColumn();

$stmt = $pdo->query("SELECT COUNT(*) FROM product_imei WHERE status = 'Available'");
$output['Available IMEI'] = (int)$stmt->fetchColumn();

$stmt = $pdo->query("
    SELECT COUNT(DISTINCT p.id) FROM products p 
    JOIN product_imei pi ON p.id = pi.product_id 
    WHERE p.status = 1 AND p.product_type = 'phone' AND pi.status = 'Available'
");
$output['Phones with Available IMEI'] = (int)$stmt->fetchColumn();

$stmt = $pdo->query("
    SELECT COUNT(*) FROM products p 
    WHERE p.status = 1 AND p.product_type = 'phone' 
    AND NOT EXISTS (SELECT 1 FROM product_imei pi WHERE pi.product_id = p.id AND pi.status = 'Available')
");
$output['Phones hidden (no Available IMEI)'] = (int)$stmt->fetchColumn();

// Sample products that should show
$stmt = $pdo->query("
    SELECT p.id, p.product_name, p.product_type, p.current_stock, 
           (SELECT COUNT(*) FROM product_imei WHERE product_id = p.id AND status = 'Available') as available_imei
    FROM products p WHERE p.status = 1
    ORDER BY p.product_name LIMIT 20
");
$output['Sample products'] = $stmt->fetchAll();

// Last SQL errors if any
try {
    $pdo->query("SELECT 1");
    $output['Last SQL Error'] = 'None';
} catch (Exception $e) {
    $output['Last SQL Error'] = $e->getMessage();
}

include __DIR__ . '/../includes/header.php';
include __DIR__ . '/../includes/sidebar.php';
?>
<div class="content-wrapper">
    <div class="container-fluid">
        <h4 class="page-title"><i class="fas fa-bug me-2"></i>POS Product Debug</h4>
        <div class="card">
            <div class="card-body">
                <table class="table table-bordered">
                    <?php foreach ($output as $key => $value): ?>
                    <tr>
                        <td><strong><?= htmlspecialchars($key) ?></strong></td>
                        <td>
                            <?php if (is_array($value)): ?>
                                <table class="table table-sm">
                                    <tr><th>ID</th><th>Name</th><th>Type</th><th>Stock</th><th>Avail IMEI</th></tr>
                                    <?php foreach ($value as $row): ?>
                                    <tr>
                                        <td><?= $row['id'] ?></td>
                                        <td><?= htmlspecialchars($row['product_name']) ?></td>
                                        <td><?= $row['product_type'] ?></td>
                                        <td><?= $row['current_stock'] ?></td>
                                        <td><?= $row['available_imei'] ?></td>
                                    </tr>
                                    <?php endforeach; ?>
                                </table>
                            <?php else: ?>
                                <?= htmlspecialchars($value) ?>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </table>
            </div>
        </div>
    </div>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
