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

class SearchController {
    private $gameModel;
    private $categoryModel;

    public function __construct() {
        $this->gameModel = new Game();
        $this->categoryModel = new Category();
    }

    public function index() {
        $query = isset($_GET['q']) ? trim($_GET['q']) : '';
        
        if (empty($query)) {
            header('Location: /');
            exit;
        }
        
        $page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
        $limit = 12;
        $offset = ($page - 1) * $limit;
        
        $games = $this->gameModel->searchGames($query, $limit, $offset);
        $totalGames = $this->gameModel->getTotalSearchResults($query);
        $totalPages = ceil($totalGames / $limit);
        
        $popularCategories = $this->categoryModel->getPopularCategories(10);
        
        include __DIR__ . '/../views/search.php';
    }
}
