<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include '../layouts/dbconn.php';

echo "<h1>ENT Queue Diagnostic</h1>";

// 1. Check Departments
echo "<h2>1. Departments Check</h2>";
$sql = "SELECT * FROM departments ORDER BY department_name";
$result = $conn->query($sql);
$ent_dept_ids = [];
if ($result->num_rows > 0) {
    echo "<table border='1'><tr><th>ID</th><th>Name</th><th>Code</th></tr>";
    while ($row = $result->fetch_assoc()) {
        echo "<tr><td>{$row['department_id']}</td><td>{$row['department_name']}</td><td>{$row['department_code']}</td></tr>";
        $ent_dept_ids[] = $row['department_id'];
    }
    echo "</table>";
} else {
    echo "<p style='color:red'>No ENT departments found!</p>";
}

// 2. Check Recent Queue Entries (Today)
echo "<h2>2. Today's Queue Entries (All Departments)</h2>";
$today = date('Y-m-d');
$sql = "SELECT q.queue_id, q.patient_name, q.queue_status, q.department_id, d.department_name, q.queue_entry_time 
        FROM patient_queue q 
        LEFT JOIN departments d ON q.department_id = d.department_id
        WHERE DATE(q.queue_entry_time) = '$today'
        ORDER BY q.queue_id DESC LIMIT 20";
$result = $conn->query($sql);

if ($result && $result->num_rows > 0) {
    echo "<table border='1'><tr><th>Queue ID</th><th>Patient</th><th>Status</th><th>Dept ID</th><th>Dept Name</th><th>Time</th></tr>";
    while ($row = $result->fetch_assoc()) {
        $highlight = in_array($row['department_id'], $ent_dept_ids) ? "style='background-color:lightgreen'" : "";
        echo "<tr $highlight>
                <td>{$row['queue_id']}</td>
                <td>{$row['patient_name']}</td>
                <td>{$row['queue_status']}</td>
                <td>{$row['department_id']}</td>
                <td>{$row['department_name']}</td>
                <td>{$row['queue_entry_time']}</td>
              </tr>";
    }
    echo "</table>";
} else {
    echo "<p>No queue entries found for today ($today).</p>";
}

// 3. Test Dashboard Query Logic
echo "<h2>3. Testing Dashboard Query</h2>";
if (!empty($ent_dept_ids)) {
    $dept_ids_str = implode(',', $ent_dept_ids);
    $sql = "SELECT COUNT(*) as count 
            FROM patient_queue q
            INNER JOIN departments d ON q.department_id = d.department_id
            WHERE q.queue_status IN ('WAITING', 'IN_SERVICE')
            AND DATE(q.queue_entry_time) = CURDATE()
            AND d.department_code LIKE '%ENT%'";

    $result = $conn->query($sql);
    $row = $result->fetch_assoc();
    echo "<p>Entries matching Dashboard criteria (Status=WAITING/IN_SERVICE, Date=Today, DeptCode like %ENT%): <strong>{$row['count']}</strong></p>";

    if ($row['count'] == 0) {
        echo "<h3>Why 0? Checking step-by-step:</h3>";

        // A. Check Status
        $sql = "SELECT COUNT(*) as count FROM patient_queue WHERE queue_status IN ('WAITING', 'IN_SERVICE') AND DATE(queue_entry_time) = CURDATE()";
        $r = $conn->query($sql)->fetch_assoc();
        echo "Patients arriving today with status WAITING/IN_SERVICE: {$r['count']}<br>";

        // B. Check Date
        $sql = "SELECT COUNT(*) as count FROM patient_queue WHERE DATE(queue_entry_time) = CURDATE()";
        $r = $conn->query($sql)->fetch_assoc();
        echo "Total patients today: {$r['count']}<br>";

        // C. Check Department
        $sql = "SELECT COUNT(*) as count FROM patient_queue WHERE department_id IN ($dept_ids_str) AND DATE(queue_entry_time) = CURDATE()";
        $r = $conn->query($sql)->fetch_assoc();
        echo "Patients assigned to ENT Dept IDs ($dept_ids_str) today: {$r['count']}<br>";
    }
}
?>