<?php
require_once __DIR__ . '/header.php';
require_login();

$user    = current_user();
$isSuper = is_super_admin();

function count_one(PDO $pdo, string $sql, array $params = []): int {
    $stmt = $pdo->prepare($sql);
    $stmt->execute($params);
    return (int)($stmt->fetch()['c'] ?? 0);
}

if ($isSuper) {
    $candCount    = count_one($pdo, "SELECT COUNT(*) AS c FROM candidates WHERE deleted_at IS NULL");
    $attemptCount = count_one($pdo, "SELECT COUNT(*) AS c FROM attempts WHERE deleted_at IS NULL");
    $formCount    = count_one($pdo, "SELECT COUNT(*) AS c FROM forms WHERE deleted_at IS NULL");
} else {
    $fid = (int)$user['facility_id'];
    $candCount    = count_one($pdo, "SELECT COUNT(*) AS c FROM candidates WHERE deleted_at IS NULL AND facility_id = ?", [$fid]);
    $attemptCount = count_one($pdo, "
        SELECT COUNT(*) AS c FROM attempts a
        JOIN candidates c ON c.id = a.candidate_id
        WHERE a.deleted_at IS NULL AND c.facility_id = ?", [$fid]);
    $formCount    = count_one($pdo, "SELECT COUNT(*) AS c FROM forms WHERE deleted_at IS NULL AND (facility_id IS NULL OR facility_id = ?)", [$fid]);
}
$activeExams = count_one($pdo, "SELECT COUNT(*) AS c FROM exams WHERE status='active' AND deleted_at IS NULL");
?>

<h2>Welcome, <?php echo h($user['full_name']); ?></h2>
<p class="muted">Role: <?php echo h($user['role']); ?><?php if ($user['facility_id']): ?> · Facility ID <?php echo (int)$user['facility_id']; ?><?php endif; ?></p>

<div class="kpi-grid">
    <div class="kpi"><div class="label">Candidates</div><div class="value"><?php echo $candCount; ?></div></div>
    <div class="kpi"><div class="label">Forms</div><div class="value"><?php echo $formCount; ?></div></div>
    <div class="kpi"><div class="label">Recorded Attempts</div><div class="value"><?php echo $attemptCount; ?></div></div>
    <div class="kpi"><div class="label">Active Exams</div><div class="value"><?php echo $activeExams; ?></div></div>
</div>

<h3>Quick actions</h3>
<div class="flex">
    <a href="grade.php" class="btn btn-primary">Start Grading</a>
    <a href="candidates.php" class="btn">Manage Candidates</a>
    <a href="forms.php" class="btn">Manage Forms</a>
    <a href="exams.php" class="btn">Exam Days</a>
    <a href="analytics.php" class="btn">Analytics</a>
    <?php if ($isSuper): ?>
        <a href="live_dashboard.php" class="btn btn-live">● Live Dashboard</a>
    <?php endif; ?>
</div>

<h3 class="mt-3">How it works</h3>
<ul>
    <li><strong>Candidates</strong>: register individually or import via CSV; barcodes auto-generated.</li>
    <li><strong>Forms</strong>: design OSCE checklists and grade bands.</li>
    <li><strong>Exams</strong>: build an exam day – pick stations, assign examiners.</li>
    <li><strong>Grade</strong>: scan a candidate barcode, grade against the checklist. Drafts auto-save as you type.</li>
    <li><strong>Live Dashboard</strong>: real-time view of every station, with 2-min and 1-min warnings.</li>
</ul>

<?php require __DIR__ . '/footer.php'; ?>
