<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Core\Controller;

/**
 * Dashboard Controller
 *
 * Handles the student dashboard page showing enrolled courses,
 * recent exam results, and account summary.
 */
class DashboardController extends Controller
{
    /**
     * Display the student dashboard.
     *
     * Shows a summary of the student's activity including
     * enrolled courses, recent exam results, and profile info.
     *
     * @return void
     */
    public function index(): void
    {
        requireLogin();

        $userId = $_SESSION['user_id'] ?? null;
        if ($userId === null) {
            redirect(BASE_PATH . '/login');
        }

        // Get user info
        $userModel = $this->model('User');
        $user = $userModel->find((int) $userId);

        // Get student record if exists
        $studentModel = $this->model('Student');
        $student = $studentModel->getByUserId((int) $userId);

        // Get recent exam results
        $examResultModel = $this->model('ExamResult');
        $recentResults = $student
            ? $examResultModel->getByStudent((int) ($student['id'] ?? 0))
            : [];

        // Get available exams
        $examModel = $this->model('Exam');
        $availableExams = method_exists($examModel, 'getPublished')
            ? $examModel->getPublished()
            : [];

        // Get settings for the page
        $settingModel = $this->model('Setting');
        $settings = $settingModel->getAll();

        $this->view('auth/dashboard', [
            'title'          => 'Student Dashboard - Gmind Institute',
            'currentPage'    => 'dashboard',
            'user'           => $user,
            'student'        => $student,
            'recentResults'  => $recentResults,
            'availableExams' => $availableExams,
            'settings'       => $settings,
        ]);
    }
}
