<?php
// debug.php - writes to a log file
const BASE_PATH = __DIR__;
spl_autoload_register(static function (string $class): void {
    $prefix = 'App\\';
    if (!str_starts_with($class, $prefix)) return;
    $relativeClass = substr($class, strlen($prefix));
    $path = BASE_PATH . '/app/' . str_replace('\\', '/', $relativeClass) . '.php';
    if (is_file($path)) require_once $path;
});

$config = require BASE_PATH . '/config/app.php';
date_default_timezone_set('UTC');
session_name($config['session_name']);

$sessionPath = $config['session_path'] ?? BASE_PATH . '/storage/sessions';
if (!is_dir($sessionPath)) mkdir($sessionPath, 0775, true);
session_save_path($sessionPath);

$isHttps = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off';
$sameSite = $isHttps ? 'None' : 'Lax';
session_set_cookie_params([
    'lifetime' => 0, 'path' => '/', 'domain' => $config['session_domain'] ?? '',
    'secure' => $isHttps, 'httponly' => true, 'samesite' => $sameSite,
]);

session_start();

file_put_contents(__DIR__ . '/debug.log', "=== Debug Log ===\n");
file_put_contents(__DIR__ . '/debug.log', "REQUEST_URI: " . ($_SERVER['REQUEST_URI'] ?? 'NOT SET') . "\n", FILE_APPEND);
file_put_contents(__DIR__ . '/debug.log', "REQUEST_METHOD: " . ($_SERVER['REQUEST_METHOD'] ?? 'NOT SET') . "\n", FILE_APPEND);
file_put_contents(__DIR__ . '/debug.log', "HTTP_HOST: " . ($_SERVER['HTTP_HOST'] ?? 'NOT SET') . "\n", FILE_APPEND);
file_put_contents(__DIR__ . '/debug.log', "HTTPS: " . ($_SERVER['HTTPS'] ?? 'NOT SET') . "\n", FILE_APPEND);

\App\Support\Database::configure($config);

$request = new \App\Support\Request();
$path = $request->path();
$method = $request->method();

file_put_contents(__DIR__ . '/debug.log', "Request->path(): $path\n", FILE_APPEND);
file_put_contents(__DIR__ . '/debug.log', "Request->method(): $method\n", FILE_APPEND);

// Instantiate and register routes
$auth = new \App\Http\Controllers\AuthController();
$bootstrapController = new \App\Http\Controllers\BootstrapController();
$instructorCtrl = new \App\Http\Controllers\InstructorController();
$tenantCtrl = new \App\Http\Controllers\TenantController();
$studentAuth = new \App\Http\Controllers\StudentAuthController();
$student = new \App\Http\Controllers\StudentController();

$router = new \App\Support\Router($request);

// Register all routes
$router->post('/api/v1/auth/login', [$auth, 'login']);
$router->post('/api/v1/auth/logout', [$auth, 'logout']);
$router->get('/api/v1/auth/me', [$auth, 'me']);
$router->post('/api/v1/auth/register-student', [$studentAuth, 'register']);
$router->get('/api/v1/student/me', [$studentAuth, 'me']);
$router->get('/api/v1/bootstrap', $bootstrapController);

$router->get('/api/v1/instructors', [new \App\Http\Controllers\InstructorsController(), 'index']);
$router->get('/api/v1/instructors/{id}/availability', [$instructorCtrl, 'availability']);
$router->get('/api/v1/instructors/{id}/vacations', [$instructorCtrl, 'vacations']);
$router->get('/api/v1/instructors/{id}/break-rules', [$instructorCtrl, 'breakRules']);

$router->get('/api/v1/tenant/pending-registrations', [$tenantCtrl, 'pendingRegistrations']);
$router->get('/api/v1/tenant/registration-code', [$tenantCtrl, 'getRegistrationCode']);

$router->get('/api/v1/student/instructors', [$student, 'instructors']);

$router->get('/api/v1/appointments', [new \App\Http\Controllers\AppointmentsController(), 'index']);
$router->get('/api/v1/reports/work-timesheet', [new \App\Http\Controllers\WorkTimesheetController(), 'index']);

file_put_contents(__DIR__ . '/debug.log', "Routes registered, dispatching...\n", FILE_APPEND);

$router->dispatch();