<?php

// Autoloader
spl_autoload_register(function ($class) {
    $prefix = 'App\\';
    $base_dir = __DIR__ . '/app/';

    $len = strlen($prefix);
    if (strncmp($prefix, $class, $len) !== 0) {
        return;
    }

    $relative_class = substr($class, $len);
    $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';

    if (file_exists($file)) {
        require $file;
    }
});

require_once 'app/Helpers/functions.php';
require_once 'db/config.php';

session_start();

// Initialize Language Service
\App\Services\LanguageService::init();

use App\Core\Router;

// Maintenance Mode Check
if (get_setting('maintenance_mode') === '1') {
    $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    $isAdmin = strpos($uri, '/admin') === 0 || strpos($uri, '/login') === 0 || strpos($uri, '/logout') === 0;
    if (!$isAdmin && !isset($_SESSION['admin_id'])) {
        require_once 'views/maintenance.php';
        exit;
    }
}

$router = new Router();
$router->post('/api/newsletter/subscribe', 'NewsletterController@subscribe');
$router->post('/api/report', 'ContactController@ajaxReport');
$router->post('/api/ai/chat', 'AIController@chat');

// Sitemap
$router->get('/sitemap.xml', 'SitemapController@index');

// Language Switch
$router->get('/lang/:code', function($params) {
    $code = $params['code'];
    \App\Services\LanguageService::setLang($code);
    header('Location: ' . ($_SERVER['HTTP_REFERER'] ?? '/'));
    exit;
});

// Home & APKs
$router->get('/', 'HomeController@index');
$router->get('/apk/:slug', 'HomeController@apkDetail');
$router->get('/download/:slug', 'HomeController@download');

// Blog
$router->get('/blog', 'BlogController@index');
$router->get('/blog/:slug', 'BlogController@detail');

// Static Pages
$router->get('/contact', 'ContactController@index');
$router->post('/contact', 'ContactController@submit');
$router->get('/help-center', 'HomeController@helpCenter');
$router->get('/privacy-policy', 'HomeController@privacyPolicy');
$router->get('/terms-of-service', 'HomeController@termsOfService');

// Auth
$router->get('/login', 'AuthController@loginForm');
$router->post('/login', 'AuthController@login');
$router->get('/register', 'AuthController@registerForm');
$router->post('/register', 'AuthController@register');
$router->get('/logout', 'AuthController@logout');
$router->get('/profile', 'AuthController@profile');
$router->post('/withdraw', 'AuthController@requestWithdrawal');

// Admin Auth
$router->get('/admin/login', 'AdminController@loginForm');
$router->post('/admin/login', 'AdminController@login');
$router->get('/admin/logout', 'AdminController@logout');

// Admin Dashboard
$router->get('/admin/dashboard', 'AdminController@dashboard');

// Admin Settings
$router->get('/admin/settings', 'AdminController@settingsForm');
$router->post('/admin/settings', 'AdminController@saveSettings');

// Admin Users
$router->get('/admin/users', 'AdminController@users');
$router->post('/admin/users/toggle-ban/:id', 'AdminController@toggleBan');

// Admin APKs
$router->get('/admin/apks', 'AdminController@apks');
$router->get('/admin/apks/mass-upload', 'AdminController@massUploadForm');
$router->post('/admin/apks/mass-upload', 'AdminController@massUpload');
$router->get('/admin/apks/add', 'AdminController@addApkForm');
$router->post('/admin/apks/add', 'AdminController@addApk');
$router->get('/admin/apks/edit/:id', 'AdminController@editApkForm');
$router->post('/admin/apks/edit/:id', 'AdminController@editApk');
$router->get('/admin/apks/delete/:id', 'AdminController@deleteApk');
$router->post('/admin/apks/reorder', 'AdminController@updateOrder');

// Admin Posts (Blog)
$router->get('/admin/posts', 'AdminController@posts');
$router->get('/admin/posts/add', 'AdminController@addPostForm');
$router->post('/admin/posts/add', 'AdminController@addPost');
$router->get('/admin/posts/edit/:id', 'AdminController@editPostForm');
$router->post('/admin/posts/edit/:id', 'AdminController@editPost');
$router->get('/admin/posts/delete/:id', 'AdminController@deletePost');

// Admin Categories
$router->get('/admin/categories', 'AdminController@categories');
$router->post('/admin/categories/add', 'AdminController@addCategory');
$router->get('/admin/categories/delete/:id', 'AdminController@deleteCategory');

// Admin Withdrawals
$router->get('/admin/withdrawals', 'AdminController@withdrawals');
$router->get('/admin/withdrawals/approve/:id', 'AdminController@approveWithdrawal');
$router->get('/admin/withdrawals/reject/:id', 'AdminController@rejectWithdrawal');

$router->dispatch();