<?php
require_once 'config/config.php';

// Check if this is an API request
$requestUri = $_SERVER['REQUEST_URI'];
if (strpos($requestUri, '/api/') === 0) {
    include 'api/index.php';
    exit;
}

// Check if this is an admin request
if (strpos($requestUri, '/admin/') === 0) {
    // Extract the file part after /admin/
    $adminPath = substr($requestUri, 7); // Remove '/admin/' prefix
    $adminPath = strtok($adminPath, '?'); // Remove query string

    if (empty($adminPath)) {
        include 'admin/index.php';
    } else {
        $adminFile = 'admin/' . $adminPath;
        if (file_exists($adminFile)) {
            include $adminFile;
        } else {
            include 'admin/index.php';
        }
    }
    exit;
}

$route = $_GET['route'] ?? '';
$language = new Language();
$article = new Article();
$category = new Category();

// Force English as default if no session language is set
if (!isset($_SESSION['language'])) {
    $_SESSION['language'] = 'en';
}

if (isset($_GET['lang']) && array_key_exists($_GET['lang'], SUPPORTED_LANGUAGES)) {
    $language->setLanguage($_GET['lang']);
    $currentLang = $_GET['lang'];
} else {
    $currentLang = $language->getCurrentLanguage();
}

$translations = loadLanguage($currentLang);

switch ($route) {
    case 'internal':
        $articles = $article->getAll('internal', 10, 0);
        $pageTitle = __('internal_blog');
        include 'templates/blog_listing.php';
        break;

    case 'external':
        $articles = $article->getAll('external', 10, 0);
        $pageTitle = __('external_blog');
        include 'templates/blog_listing.php';
        break;

    case 'article':
        $slug = $_GET['slug'] ?? '';
        if ($slug) {
            $articleData = $article->getBySlug($slug);
            if ($articleData) {
                include 'templates/article_detail.php';
            } else {
                http_response_code(404);
                include 'templates/404.php';
            }
        } else {
            redirect('/');
        }
        break;

    case 'category':
        $categoryId = $_GET['id'] ?? '';
        if ($categoryId) {
            $articles = $article->getAll(null, 10, 0, $categoryId);
            $categoryData = $category->getById($categoryId);
            $pageTitle = $categoryData['name'] ?? __('categories');
            include 'templates/blog_listing.php';
        } else {
            redirect('/');
        }
        break;

    case 'search':
        $query = $_GET['q'] ?? '';
        if ($query) {
            $articles = $article->search($query, null, 10, 0);
            $pageTitle = __('search') . ': ' . htmlspecialchars($query);
            include 'templates/blog_listing.php';
        } else {
            redirect('/');
        }
        break;

    case 'about':
        include 'templates/about.php';
        break;

    case 'contact':
        include 'templates/contact.php';
        break;

    case 'documentation':
        include 'templates/documentation.php';
        break;

    default:
        $internalArticles = $article->getAll('internal', 5, 0);
        $externalArticles = $article->getAll('external', 5, 0);
        $categories = $category->getAll();
        include 'templates/home.php';
        break;
}
?>