<?php
/**
 * TechStackBase API Front Controller
 * This file MUST exist for rewrites to work
 */

declare(strict_types=1);

// Start session early
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Basic headers (safe defaults)
header('X-Content-Type-Options: nosniff');
header('X-Frame-Options: SAMEORIGIN');
header('X-XSS-Protection: 1; mode=block');

// Bootstrap system
require_once __DIR__ . '/system/bootstrap.php';

/*
|--------------------------------------------------------------------------
| Simple Router
|--------------------------------------------------------------------------
| If a real file exists, let Apache serve it.
| Otherwise, route via PATH_INFO
*/
$path = $_SERVER['REQUEST_URI'];
$path = strtok($path, '?');

// Remove leading slash
$path = ltrim($path, '/');

// If file exists, include it directly
$file = __DIR__ . '/' . $path;

if (is_file($file)) {
    require $file;
    exit;
}

// Otherwise 404
http_response_code(404);
header('Content-Type: application/json');
echo json_encode([
    'status'  => 'error',
    'message' => 'Endpoint not found'
]);
exit;
