<?php
session_start();

require_once __DIR__ . '/../vendor/autoload.php';

use TribeGen\Database\Database;

// Initialize database if needed
Database::getInstance()->initializeTables();

// Route the request
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$path = str_replace('/tribegen', '', $path);

// API routes
if (strpos($path, '/api/') === 0) {
    header('Content-Type: application/json');

    $apiPath = substr($path, 5);

    // Auth routes
    if ($apiPath === 'auth/register') {
        include __DIR__ . '/../src/api/auth.php';
    } elseif ($apiPath === 'auth/login') {
        include __DIR__ . '/../src/api/auth.php';
    } elseif ($apiPath === 'auth/logout') {
        include __DIR__ . '/../src/api/auth.php';
    } elseif ($apiPath === 'game/new') {
        include __DIR__ . '/../src/api/game.php';
    } elseif ($apiPath === 'game/load') {
        include __DIR__ . '/../src/api/game.php';
    } elseif ($apiPath === 'game/save') {
        include __DIR__ . '/../src/api/game.php';
    } elseif ($apiPath === 'game/generation') {
        include __DIR__ . '/../src/api/game.php';
    } elseif ($apiPath === 'game/event') {
        include __DIR__ . '/../src/api/game.php';
    } else {
        http_response_code(404);
        echo json_encode(['error' => 'Not found']);
    }
} else {
    // Serve index.html for all other routes (SPA)
    include __DIR__ . '/index.html';
}
