<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Core\Controller;

/**
 * Home Controller
 *
 * Handles the homepage of the Gmind Institute website.
 * Loads all data required for the various home page sections
 * and passes them to the view for rendering.
 */
class HomeController extends Controller
{
    /**
     * Display the home page.
     *
     * Gathers data for all home page sections:
     * - Sliders (carousel)
     * - Announcements (scrolling ticker)
     * - Featured courses
     * - Branches
     * - Affiliations & Partners
     * - Toppers
     * - Branch news
     * - FAQs
     * - Testimonials
     * - Stats (from settings)
     *
     * @return void
     */
    public function index(): void
    {
        // Load models
        $sliderModel      = $this->model('Slider');
        $announcementModel = $this->model('Announcement');
        $courseModel      = $this->model('Course');
        $branchModel      = $this->model('Branch');
        $affiliationModel = $this->model('Affiliation');
        $topperModel      = $this->model('Topper');
        $branchNewsModel  = $this->model('BranchNews');
        $faqModel         = $this->model('Faq');
        $testimonialModel = $this->model('Testimonial');
        $settingModel     = $this->model('Setting');

        // Fetch all data for home page sections
        $sliders       = $sliderModel->getActive();
        $announcements = $announcementModel->getActive();
        $featuredCourses = $courseModel->getFeatured(6);
        $branches      = $branchModel->getLatest(8);
        $affiliations  = $affiliationModel->getByType('affiliation');
        $partners      = $affiliationModel->getByType('partner');
        $toppers       = $topperModel->getActive(8);
        $branchNews    = $branchNewsModel->getLatest(6);
        $faqs          = $faqModel->getForHome(6);
        $testimonials  = $testimonialModel->getApproved(6);
        $avgRating     = $testimonialModel->getAverageRating();

        // Fetch site stats from settings
        $stats = [
            'total_students'    => $settingModel->get('stat_total_students') ?? '0',
            'total_branches'    => (string) $branchModel->getActiveCount(),
            'total_courses'     => (string) $courseModel->getActiveCount(),
            'placement_rate'    => $settingModel->get('stat_placement_rate') ?? '0',
            'years_experience'  => $settingModel->get('stat_years_experience') ?? '0',
        ];

        // Fetch site-wide settings for the view
        $settings = $settingModel->getAll();

        // Pass all data to the view
        $this->view('home/index', [
            'sliders'         => $sliders,
            'announcements'   => $announcements,
            'featuredCourses' => $featuredCourses,
            'branches'        => $branches,
            'affiliations'    => $affiliations,
            'partners'        => $partners,
            'toppers'         => $toppers,
            'branchNews'      => $branchNews,
            'faqs'            => $faqs,
            'testimonials'    => $testimonials,
            'avgRating'       => $avgRating,
            'stats'           => $stats,
            'settings'        => $settings,
        ]);
    }
}
