<?php
require_once APP_PATH . '/Core/BaseController.php';
require_once APP_PATH . '/Models/University.php';
require_once APP_PATH . '/Models/Scholarship.php';
require_once APP_PATH . '/Models/Mentor.php';

class HomeController extends BaseController {

    public function index(): void {
        $uniModel  = new University();
        $schModel  = new Scholarship();
        $mentModel = new Mentor();
        $db        = Database::getInstance();

        $featured_universities = $uniModel->getFeatured(6);
        $featured_scholarships = $schModel->getFeatured(3);
        $top_mentors           = $mentModel->getTopMentors(4);

        // Stats
        $stats = [
            'universities' => (int) $db->query("SELECT COUNT(*) FROM universities WHERE status='active'")->fetchColumn(),
            'scholarships' => (int) $db->query("SELECT COUNT(*) FROM scholarships WHERE is_active=1")->fetchColumn(),
            'mentors'      => (int) $db->query("SELECT COUNT(*) FROM mentor_profiles WHERE verified=1")->fetchColumn(),
            'students'     => (int) $db->query("SELECT COUNT(*) FROM users WHERE role='student' AND status='active'")->fetchColumn(),
        ];

        $this->view('home.index', [
            'title'                => 'BOAFO — Your University Journey Starts Here',
            'featured_universities'=> $featured_universities,
            'featured_scholarships'=> $featured_scholarships,
            'top_mentors'          => $top_mentors,
            'stats'                => $stats,
        ]);
    }

    public function about(): void {
        $this->view('home.about', ['title' => 'About BOAFO']);
    }

    public function contact(): void {
        $this->view('home.contact', [
            'title'   => 'Contact Us — BOAFO',
            'success' => $this->getFlash('success'),
            'error'   => $this->getFlash('error'),
        ]);
    }

    public function sendContact(): void {
        CSRF::check();
        // In production, send email via PHPMailer
        $this->flash('success', 'Thank you for your message! We will get back to you within 24 hours.');
        $this->redirect(APP_URL . '/contact');
    }

    public function stats(): void {
        $db = Database::getInstance();
        $this->json([
            'universities' => (int) $db->query("SELECT COUNT(*) FROM universities WHERE status='active'")->fetchColumn(),
            'scholarships' => (int) $db->query("SELECT COUNT(*) FROM scholarships WHERE is_active=1")->fetchColumn(),
            'mentors'      => (int) $db->query("SELECT COUNT(*) FROM mentor_profiles WHERE verified=1")->fetchColumn(),
            'students'     => (int) $db->query("SELECT COUNT(*) FROM users WHERE role='student' AND status='active'")->fetchColumn(),
        ]);
    }
}

require_once APP_PATH . '/Core/CSRF.php';
