<?php
// ============================================================
// DashboardController
// ============================================================
requireLogin();
$db   = Database::getInstance();
$role = $_SESSION['role'];
$uid  = (int)$_SESSION['user_id'];

$stats = [];

if ($role === 'admin') {
    $stats['total_users']       = $db->query('SELECT COUNT(*) FROM users WHERE role != "admin"')->fetchColumn();
    $stats['total_labs']        = $db->query('SELECT COUNT(*) FROM laboratories')->fetchColumn();
    $stats['total_companies']   = $db->query('SELECT COUNT(*) FROM companies')->fetchColumn();
    $stats['total_requests']    = $db->query('SELECT COUNT(*) FROM requests')->fetchColumn();
    $stats['pending_requests']  = $db->query('SELECT COUNT(*) FROM requests WHERE status = "pending"')->fetchColumn();
    $stats['completed_requests']= $db->query('SELECT COUNT(*) FROM requests WHERE status = "completed"')->fetchColumn();
    $stats['total_certs']       = $db->query('SELECT COUNT(*) FROM certificates')->fetchColumn();
    $recentLogs = $db->query('
        SELECT al.*, u.officer_name FROM activity_logs al
        LEFT JOIN users u ON al.user_id = u.user_id
        ORDER BY al.created_at DESC LIMIT 10
    ')->fetchAll();
    $recentRequests = $db->query('
        SELECT r.*, c.company_name, e.equipment_name, l.lab_name
        FROM requests r
        JOIN companies c ON r.company_id = c.company_id
        JOIN equipment e ON r.equipment_id = e.equipment_id
        JOIN laboratories l ON r.lab_id = l.lab_id
        ORDER BY r.date_created DESC LIMIT 8
    ')->fetchAll();

} elseif ($role === 'front_desk') {
    $stats['my_requests']       = $db->prepare('SELECT COUNT(*) FROM requests WHERE created_by = ?');
    $stats['my_requests']->execute([$uid]);
    $stats['my_requests']       = $stats['my_requests']->fetchColumn();
    $stats['total_companies']   = $db->query('SELECT COUNT(*) FROM companies')->fetchColumn();
    $stats['total_equipment']   = $db->query('SELECT COUNT(*) FROM equipment')->fetchColumn();
    $stats['pending_requests']  = $db->query('SELECT COUNT(*) FROM requests WHERE status = "pending"')->fetchColumn();
    $recentRequests = $db->prepare('
        SELECT r.*, c.company_name, e.equipment_name, l.lab_name
        FROM requests r
        JOIN companies c ON r.company_id = c.company_id
        JOIN equipment e ON r.equipment_id = e.equipment_id
        JOIN laboratories l ON r.lab_id = l.lab_id
        WHERE r.created_by = ?
        ORDER BY r.date_created DESC LIMIT 8
    ');
    $recentRequests->execute([$uid]);
    $recentRequests = $recentRequests->fetchAll();
    $recentLogs = [];

} else { // lab_officer
    $labs = getUserLabs($uid);
    $labIds = array_column($labs, 'lab_id');
    $inClause = implode(',', array_fill(0, count($labIds), '?'));

    if ($labIds) {
        $stmt = $db->prepare("SELECT COUNT(*) FROM requests WHERE lab_id IN ($inClause)");
        $stmt->execute($labIds);
        $stats['lab_requests'] = $stmt->fetchColumn();

        $stmt = $db->prepare("SELECT COUNT(*) FROM requests WHERE lab_id IN ($inClause) AND status = 'pending'");
        $stmt->execute($labIds);
        $stats['pending_requests'] = $stmt->fetchColumn();

        $stmt = $db->prepare("SELECT COUNT(*) FROM requests WHERE lab_id IN ($inClause) AND status = 'completed'");
        $stmt->execute($labIds);
        $stats['completed_requests'] = $stmt->fetchColumn();
    } else {
        $stats = ['lab_requests' => 0, 'pending_requests' => 0, 'completed_requests' => 0];
    }

    $stmt = $db->prepare('SELECT COUNT(*) FROM certificates WHERE issued_by = ?');
    $stmt->execute([$uid]);
    $stats['my_certs'] = $stmt->fetchColumn();

    $recentRequests = [];
    if ($labIds) {
        $stmt = $db->prepare("
            SELECT r.*, c.company_name, e.equipment_name, l.lab_name
            FROM requests r
            JOIN companies c ON r.company_id = c.company_id
            JOIN equipment e ON r.equipment_id = e.equipment_id
            JOIN laboratories l ON r.lab_id = l.lab_id
            WHERE r.lab_id IN ($inClause)
            ORDER BY r.date_created DESC LIMIT 8
        ");
        $stmt->execute($labIds);
        $recentRequests = $stmt->fetchAll();
    }
    $recentLogs = [];
}

include __DIR__ . '/../views/shared/dashboard.php';
