<?php
require_once __DIR__ . '/../models/Game.php';
require_once __DIR__ . '/../models/Category.php';
require_once __DIR__ . '/../models/SiteSettings.php';
require_once __DIR__ . '/../models/Blog.php';
require_once __DIR__ . '/../models/User.php';

class HomeController {
    private $game;
    private $category;
    private $siteSettings;
    private $blog;
    private $user;

    public function __construct() {
        $this->game = new Game();
        $this->category = new Category();
        $this->siteSettings = new SiteSettings();
        $this->blog = new Blog();
        $this->user = new User();
    }

    public function index() {
        $settings = $this->siteSettings->getAllSettings();

        $featuredGames = $this->game->getRandomGames(12);
        $recentGames = $this->game->getAllGames(24);
        $popularCategories = $this->category->getPopularCategories(6);

        // Always get blog posts for homepage
        $blogPosts = $this->blog->getRecentPosts(3);

        // Get leaderboard data if enabled
        $showLeaderboard = $settings['show_leaderboard'] ?? '1';
        $leaderboardLimit = (int)($settings['leaderboard_limit'] ?? 10);
        $leaderboard = [];

        if ($showLeaderboard === '1') {
            $leaderboard = $this->user->getLeaderboard($leaderboardLimit);
        }

        $heroTitle = $settings['hero_title'] ?? 'Welcome to Our Gaming Portal';
        $heroDescription = $settings['hero_description'] ?? 'Discover the best free online games and enjoy hours of entertainment.';
        $heroImagePath = $settings['hero_image_path'] ?? '/public/images/hero-default.jpg';

        // Pass the game model to the view
        $gameModel = $this->game;

        include __DIR__ . '/../views/home.php';
    }
}
