<?php
require __DIR__ . '/../includes/bootstrap.php';
require_login();

$pdo = db();

$stats = [
    'students' => (int) $pdo->query('SELECT COUNT(*) FROM students')->fetchColumn(),
    'activities' => (int) $pdo->query('SELECT COUNT(*) FROM activities WHERE status = 1')->fetchColumn(),
    'attendance_today' => (int) $pdo->query('SELECT COUNT(*) FROM attendance WHERE DATE(time_in) = CURDATE()')->fetchColumn(),
    'users' => (int) $pdo->query('SELECT COUNT(*) FROM users WHERE status = 1')->fetchColumn(),
];

$upcomingActivities = $pdo->query(
    'SELECT a.*, d.name AS department_name, c.name AS course_name 
     FROM activities a
     LEFT JOIN departments d ON a.department_id = d.id
     LEFT JOIN courses c ON a.course_id = c.id
     WHERE a.date >= CURDATE()
     ORDER BY a.date ASC, a.time_in ASC
     LIMIT 5'
)->fetchAll();

$recentAttendance = $pdo->query(
    'SELECT att.*, s.full_name, act.name AS activity_name
     FROM attendance att
     INNER JOIN students s ON att.student_id = s.id
     INNER JOIN activities act ON att.activity_id = act.id
     ORDER BY att.updated_at DESC
     LIMIT 10'
)->fetchAll();

render_header('Dashboard', ['active' => 'dashboard']);
?>
<div class="row g-4">
    <div class="col-md-3">
        <div class="stat-card">
            <h6>Registered Students</h6>
            <p class="stat-value"><?= $stats['students'] ?></p>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card">
            <h6>Active Activities</h6>
            <p class="stat-value"><?= $stats['activities'] ?></p>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card">
            <h6>Attendance (Today)</h6>
            <p class="stat-value"><?= $stats['attendance_today'] ?></p>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card">
            <h6>Active Users</h6>
            <p class="stat-value"><?= $stats['users'] ?></p>
        </div>
    </div>
</div>

<div class="row mt-4 g-4">
    <div class="col-lg-6">
        <div class="card shadow-sm">
            <div class="card-header bg-white">
                <div class="d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">Upcoming Activities</h5>
                    <a href="<?= url_for('admin/activities.php') ?>" class="btn btn-sm btn-outline-primary">Manage</a>
                </div>
            </div>
            <div class="card-body">
                <?php if (!$upcomingActivities): ?>
                    <p class="text-muted text-center mb-0">No scheduled activities yet.</p>
                <?php else: ?>
                    <div class="list-group list-group-flush">
                        <?php foreach ($upcomingActivities as $activity): ?>
                            <div class="list-group-item">
                                <div class="d-flex justify-content-between">
                                    <div>
                                        <h6 class="mb-1"><?= sanitize($activity['name']) ?></h6>
                                        <div class="text-muted-sm">
                                            <?= format_date($activity['date']) ?> •
                                            <?= sanitize(substr($activity['time_in'], 0, 5)) ?>
                                            —
                                            <?= sanitize(substr($activity['time_out'], 0, 5)) ?>
                                        </div>
                                        <?php if ($activity['required_students'] !== 'all'): ?>
                                            <span class="chip mt-2">
                                                <?= $activity['required_students'] === 'specific_course'
                                                    ? sanitize($activity['course_name'] ?? 'Course Restricted')
                                                    : sanitize($activity['department_name'] ?? 'Department Restricted') ?>
                                            </span>
                                        <?php endif; ?>
                                    </div>
                                    <span class="badge rounded-pill bg-primary-subtle text-primary align-self-start">
                                        <?= sanitize($activity['location']) ?>
                                    </span>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <div class="col-lg-6">
        <div class="card shadow-sm">
            <div class="card-header bg-white">
                <h5 class="mb-0">Latest Attendance Logs</h5>
            </div>
            <div class="card-body">
                <?php if (!$recentAttendance): ?>
                    <p class="text-muted text-center mb-0">No attendance captured yet.</p>
                <?php else: ?>
                    <div class="table-responsive">
                        <table class="table align-middle mb-0">
                            <thead>
                            <tr>
                                <th>Student</th>
                                <th>Activity</th>
                                <th>Status</th>
                                <th>Recorded</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php foreach ($recentAttendance as $row): ?>
                                <tr>
                                    <td><?= sanitize($row['full_name']) ?></td>
                                    <td><?= sanitize($row['activity_name']) ?></td>
                                    <td>
                                        <span class="badge bg-success-subtle text-success badge-status">
                                            <?= strtoupper(sanitize($row['status'])) ?>
                                        </span>
                                    </td>
                                    <td><?= format_datetime($row['updated_at']) ?></td>
                                </tr>
                            <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
<?php
render_footer();
