<?php

declare(strict_types=1);

namespace Admin\Controllers;

/**
 * Admin Dashboard Controller
 *
 * Renders the admin dashboard with summary statistics
 * and recent activity data.
 */
class DashboardController extends AdminController
{
    /**
     * Display the admin dashboard.
     *
     * Gathers counts of students, courses, exams, branches,
     * pending testimonials, recent contacts, and recent exam results.
     *
     * @param array<string, string> $params Route parameters
     * @return void
     */
    public function index(array $params = []): void
    {
        requireAdmin();

        // Student count
        $stmt = $this->db->query('SELECT COUNT(*) AS total FROM `students`');
        $studentCount = (int) ($stmt->fetch()['total'] ?? 0);

        // Active courses count
        $stmt = $this->db->query('SELECT COUNT(*) AS total FROM `courses` WHERE `active` = 1');
        $courseCount = (int) ($stmt->fetch()['total'] ?? 0);

        // Exams count
        $stmt = $this->db->query('SELECT COUNT(*) AS total FROM `exams`');
        $examCount = (int) ($stmt->fetch()['total'] ?? 0);

        // Branches count
        $stmt = $this->db->query('SELECT COUNT(*) AS total FROM `branches` WHERE `active` = 1');
        $branchCount = (int) ($stmt->fetch()['total'] ?? 0);

        // Pending testimonials
        $stmt = $this->db->query('SELECT COUNT(*) AS total FROM `testimonials` WHERE `is_approved` = 0');
        $pendingTestimonials = (int) ($stmt->fetch()['total'] ?? 0);

        // Unread contacts count
        $stmt = $this->db->query('SELECT COUNT(*) AS total FROM `contacts` WHERE `is_read` = 0');
        $unreadContacts = (int) ($stmt->fetch()['total'] ?? 0);

        // Recent contacts (last 5)
        $stmt = $this->db->query(
            'SELECT * FROM `contacts` ORDER BY `created_at` DESC LIMIT 5'
        );
        $recentContacts = $stmt->fetchAll() ?: [];

        // Recent exam results (last 5)
        $stmt = $this->db->query(
            'SELECT er.*, e.title AS exam_title, u.name AS student_name
             FROM `exam_results` er
             LEFT JOIN `exams` e ON er.exam_id = e.id
             LEFT JOIN `students` s ON er.student_id = s.id
             LEFT JOIN `users` u ON s.user_id = u.id
             ORDER BY er.created_at DESC LIMIT 5'
        );
        $recentResults = $stmt->fetchAll() ?: [];

        // Published exams count
        $stmt = $this->db->query('SELECT COUNT(*) AS total FROM `exams` WHERE `is_published` = 1');
        $publishedExams = (int) ($stmt->fetch()['total'] ?? 0);

        $this->adminView('dashboard', [
            'title'              => 'Dashboard',
            'currentPage'        => 'dashboard',
            'studentCount'       => $studentCount,
            'courseCount'        => $courseCount,
            'examCount'          => $examCount,
            'branchCount'        => $branchCount,
            'pendingTestimonials' => $pendingTestimonials,
            'unreadContacts'     => $unreadContacts,
            'recentContacts'     => $recentContacts,
            'recentResults'      => $recentResults,
            'publishedExams'     => $publishedExams,
        ]);
    }
}
