<?php
// Start session
session_start();

// Load configuration and controllers
require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/config/router.php';
require_once __DIR__ . '/controllers/HomeController.php';
require_once __DIR__ . '/controllers/GameController.php';
require_once __DIR__ . '/controllers/CategoryController.php';
require_once __DIR__ . '/controllers/SearchController.php';
require_once __DIR__ . '/controllers/AdminController.php';
require_once __DIR__ . '/controllers/BlogController.php';
require_once __DIR__ . '/controllers/PageController.php';
require_once __DIR__ . '/controllers/UserController.php';
require_once __DIR__ . '/controllers/AuthController.php';
require_once __DIR__ . '/controllers/CoinsController.php';

// Create router instance
$router = new Router();

// API endpoints
$router->get('/api/category', function() {
    // Set the content type to JSON
    header('Content-Type: application/json');

    // Get the slug from the query string
    $slug = isset($_GET['slug']) ? $_GET['slug'] : '';

    if (empty($slug)) {
        echo json_encode(['error' => 'Slug parameter is required']);
        return;
    }

    // Get the category data
    require_once __DIR__ . '/models/Category.php';
    $categoryModel = new Category();
    $category = $categoryModel->getCategoryBySlug($slug);

    if (!$category) {
        echo json_encode(['error' => 'Category not found']);
        return;
    }

    // Return the category data
    echo json_encode($category);
});

// Serve ads.txt file
$router->get('/ads.txt', function() {
    if (file_exists(__DIR__ . '/ads.txt')) {
        header('Content-Type: text/plain');
        readfile(__DIR__ . '/ads.txt');
    } else {
        // Return 404 if file doesn't exist
        header("HTTP/1.0 404 Not Found");
        echo "ads.txt file not found.";
    }
});

// Define routes
$router->get('/', function() {
    $homeController = new HomeController();
    $homeController->index();
});

$router->get('/game/:slug', function($slug) {
    $gameController = new GameController();
    $gameController->show($slug);
});

$router->get('/category/:slug', function($slug) {
    $categoryController = new CategoryController();
    $categoryController->show($slug);
});

$router->get('/search', function() {
    $searchController = new SearchController();
    $searchController->index();
});

$router->get('/categories', function() {
    $categoryController = new CategoryController();
    $categoryController->index();
});

// Blog routes
$router->get('/blog', function() {
    $blogController = new BlogController();
    $blogController->index();
});

$router->get('/blog/:slug', function($slug) {
    $blogController = new BlogController();
    $blogController->show($slug);
});

$router->get('/blog/tag/:tag', function($tag) {
    $blogController = new BlogController();
    $blogController->tag($tag);
});

// User routes
$router->get('/register', function() {
    $userController = new UserController();
    $userController->registerPage();
});

$router->post('/register', function() {
    $userController = new UserController();
    $userController->registerPage();
});

$router->get('/login', function() {
    $userController = new UserController();
    $userController->loginPage();
});

$router->post('/login', function() {
    $userController = new UserController();
    $userController->loginPage();
});

$router->get('/logout', function() {
    $userController = new UserController();
    $userController->logout();
});

// Google OAuth routes
$router->get('/auth/google', function() {
    $authController = new AuthController();
    $authController->googleAuth();
});

$router->get('/auth/google/callback', function() {
    $authController = new AuthController();
    $authController->googleCallback();
});

$router->get('/forgot-password', function() {
    $userController = new UserController();
    $userController->forgotPasswordPage();
});

$router->post('/forgot-password', function() {
    $userController = new UserController();
    $userController->forgotPasswordPage();
});

$router->get('/reset-password', function() {
    $userController = new UserController();
    $userController->resetPasswordPage();
});

$router->post('/reset-password', function() {
    $userController = new UserController();
    $userController->resetPasswordPage();
});

$router->get('/verify-email', function() {
    $userController = new UserController();
    $userController->verifyEmail();
});

$router->get('/profile', function() {
    $userController = new UserController();
    $userController->profilePage();
});

$router->post('/profile', function() {
    $userController = new UserController();
    $userController->profilePage();
});

$router->post('/record-game-play', function() {
    $userController = new UserController();
    $userController->recordGamePlay();
});

$router->post('/toggle-favorite', function() {
    $userController = new UserController();
    $userController->toggleFavorite();
});

$router->get('/is-game-favorited', function() {
    $userController = new UserController();
    $userController->isGameFavorited();
});

// Admin routes
$router->get('/admin', function() {
    $adminController = new AdminController();
    $adminController->dashboard();
});

$router->get('/admin/login', function() {
    $adminController = new AdminController();
    $adminController->loginPage();
});

$router->post('/admin/login', function() {
    $adminController = new AdminController();
    $adminController->loginPage();
});

$router->get('/admin/dashboard', function() {
    $adminController = new AdminController();
    $adminController->dashboard();
});

$router->get('/admin/settings', function() {
    $adminController = new AdminController();
    $adminController->settings();
});

$router->post('/admin/settings', function() {
    $adminController = new AdminController();
    $adminController->settings();
});

$router->get('/admin/import-games', function() {
    $adminController = new AdminController();
    $adminController->importGames();
});

$router->post('/admin/import-games', function() {
    $adminController = new AdminController();
    $adminController->importGames();
});

$router->get('/admin/import-games-stream', function() {
    $adminController = new AdminController();
    $adminController->importGamesStream();
});

$router->get('/admin/import-games-status', function() {
    $adminController = new AdminController();
    $adminController->importGamesStatus();
});

// Games management routes
$router->get('/admin/games', function() {
    $adminController = new AdminController();
    $adminController->games();
});

$router->post('/admin/games/delete', function() {
    $adminController = new AdminController();
    $adminController->deleteGame();
});

$router->post('/admin/games/delete-by-source', function() {
    $adminController = new AdminController();
    $adminController->deleteGamesBySource();
});

$router->post('/admin/games/update-category', function() {
    $adminController = new AdminController();
    $adminController->updateGameCategory();
});

$router->get('/admin/games/edit/:id', function($id) {
    $adminController = new AdminController();
    $adminController->editGameForm($id);
});

$router->post('/admin/games/update', function() {
    $adminController = new AdminController();
    $adminController->updateGame();
});

$router->post('/admin/games/update-featured', function() {
    $adminController = new AdminController();
    $adminController->updateGameFeatured();
});

// Categories management routes
$router->get('/admin/categories', function() {
    $adminController = new AdminController();
    $adminController->categories();
});

$router->post('/admin/categories/add', function() {
    $adminController = new AdminController();
    $adminController->addCategory();
});

$router->post('/admin/categories/edit', function() {
    $adminController = new AdminController();
    $adminController->editCategory();
});

$router->post('/admin/categories/delete', function() {
    $adminController = new AdminController();
    $adminController->deleteCategory();
});

$router->post('/admin/categories/update-sort-order', function() {
    $adminController = new AdminController();
    $adminController->updateCategorySortOrder();
});

// Admin credentials routes
$router->get('/admin/credentials', function() {
    $adminController = new AdminController();
    $adminController->credentials();
});

$router->post('/admin/credentials', function() {
    $adminController = new AdminController();
    $adminController->credentials();
});

// Blog management routes
$router->get('/admin/blog-posts', function() {
    $adminController = new AdminController();
    $adminController->blogPosts();
});

$router->get('/admin/blog-posts/add', function() {
    $adminController = new AdminController();
    $adminController->addBlogPost();
});

$router->post('/admin/blog-posts/add', function() {
    $adminController = new AdminController();
    $adminController->addBlogPost();
});

$router->get('/admin/blog-posts/edit', function() {
    $adminController = new AdminController();
    $adminController->editBlogPost();
});

$router->post('/admin/blog-posts/edit', function() {
    $adminController = new AdminController();
    $adminController->editBlogPost();
});

$router->post('/admin/blog-posts/delete', function() {
    $adminController = new AdminController();
    $adminController->deleteBlogPost();
});

$router->get('/admin/logout', function() {
    $adminController = new AdminController();
    $adminController->logout();
});

$router->post('/admin/delete-blog-post', function() {
    $adminController = new AdminController();
    $adminController->deleteBlogPost();
});

$router->get('/admin/code-and-ads', function() {
    $adminController = new AdminController();
    $adminController->codeAndAds();
});

$router->post('/admin/code-and-ads', function() {
    $adminController = new AdminController();
    $adminController->codeAndAds();
});

// Page routes
$router->get('/page/:slug', function($slug) {
    $pageController = new PageController();
    $pageController->show($slug);
});

// Page management routes
$router->get('/admin/pages', function() {
    $adminController = new AdminController();
    $adminController->pages();
});

$router->get('/admin/pages/add', function() {
    $adminController = new AdminController();
    $adminController->addPage();
});

$router->post('/admin/pages/add', function() {
    $adminController = new AdminController();
    $adminController->addPage();
});

$router->get('/admin/pages/edit', function() {
    $adminController = new AdminController();
    $adminController->editPage();
});

$router->post('/admin/pages/edit', function() {
    $adminController = new AdminController();
    $adminController->editPage();
});

$router->post('/admin/pages/delete', function() {
    $adminController = new AdminController();
    $adminController->deletePage();
});

// Menu management routes
$router->get('/admin/menus', function() {
    $adminController = new AdminController();
    $adminController->menus();
});

$router->get('/admin/menus/add', function() {
    $adminController = new AdminController();
    $adminController->addMenu();
});

$router->post('/admin/menus/add', function() {
    $adminController = new AdminController();
    $adminController->addMenu();
});

$router->get('/admin/menus/edit', function() {
    $adminController = new AdminController();
    $adminController->editMenu();
});

$router->post('/admin/menus/edit', function() {
    $adminController = new AdminController();
    $adminController->editMenu();
});

$router->post('/admin/menus/delete', function() {
    $adminController = new AdminController();
    $adminController->deleteMenu();
});

$router->post('/admin/menu-items/delete', function() {
    $adminController = new AdminController();
    $adminController->deleteMenuItem();
});

$router->post('/admin/menu-items/edit', function() {
    $adminController = new AdminController();
    $adminController->editMenuItem();
});

// User management routes
$router->get('/admin/users', function() {
    $adminController = new AdminController();
    $adminController->users();
});

$router->get('/admin/edit-user', function() {
    $adminController = new AdminController();
    $adminController->editUser();
});

$router->post('/admin/edit-user', function() {
    $adminController = new AdminController();
    $adminController->editUser();
});

$router->post('/admin/delete-user', function() {
    $adminController = new AdminController();
    $adminController->deleteUser();
});

$router->post('/admin/test-smtp', function() {
    $adminController = new AdminController();
    $adminController->testSmtp();
});

$router->post('/admin/openrouter-models', function() {
    $adminController = new AdminController();
    $adminController->openRouterModels();
});

$router->post('/admin/generate-description', function() {
    $adminController = new AdminController();
    $adminController->generateGameDescription();
});

// Points system routes
$router->get('/admin/points-system', function() {
    $adminController = new AdminController();
    $adminController->pointsSystem();
});

$router->post('/admin/points-system', function() {
    $adminController = new AdminController();
    $adminController->pointsSystem();
});

$router->post('/admin/points-system/reset', function() {
    $adminController = new AdminController();
    $adminController->resetPointsSystemStats();
});

$router->get('/admin/withdrawal-requests', function() {
    $adminController = new AdminController();
    $adminController->withdrawalRequests();
});

$router->post('/admin/withdrawal-requests/update', function() {
    $adminController = new AdminController();
    $adminController->updateWithdrawalStatus();
});

$router->post('/admin/withdrawal-requests/delete', function() {
    $adminController = new AdminController();
    $adminController->deleteWithdrawalRequest();
});

$router->get('/admin/withdrawal-requests/details/:id', function($id) {
    $adminController = new AdminController();
    $adminController->withdrawalRequestDetails($id);
});

// Coins API routes
$router->post('/api/coins/award', function() {
    $coinsController = new CoinsController();
    $coinsController->awardCoins();
});

$router->get('/api/coins/balance', function() {
    $coinsController = new CoinsController();
    $coinsController->getBalance();
});

$router->post('/api/coins/withdraw', function() {
    $coinsController = new CoinsController();
    $coinsController->createWithdrawal();
});

$router->get('/api/coins/withdrawals', function() {
    $coinsController = new CoinsController();
    $coinsController->getUserWithdrawals();
});

$router->get('/api/coins/transactions', function() {
    $coinsController = new CoinsController();
    $coinsController->getTransactionHistory();
});

$router->get('/api/coins/withdrawal-settings', function() {
    $coinsController = new CoinsController();
    $coinsController->getWithdrawalSettings();
});

// User coins page
$router->get('/coins', function() {
    include __DIR__ . '/views/user/coins.php';
});

// 404 handler
$router->notFound(function() {
    http_response_code(404);
    include __DIR__ . '/views/404.php';
});

// Resolve the route
$router->resolve();
