<?php
// backend/index.php
header("Access-Control-Allow-Origin: http://localhost:3000");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");
header("Content-Type: application/json");

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit();
}

require_once __DIR__ . '/controllers/AuthController.php';
require_once __DIR__ . '/controllers/ResumeController.php';

$requestUri = str_replace('/backend/index.php', '', $_SERVER['REQUEST_URI']);
$requestUri = strtok($requestUri, '?');
$method = $_SERVER['REQUEST_METHOD'];

$authController = new AuthController();
$resumeController = new ResumeController();

switch ($requestUri) {
    case '/api/auth/register':
        if ($method === 'POST') $authController->register();
        break;
    case '/api/auth/login':
        if ($method === 'POST') $authController->login();
        break;
    case '/api/auth/logout':
        if ($method === 'POST') $authController->logout();
        break;
    case '/api/auth/me':
        if ($method === 'GET') $authController->me();
        break;
    case '/api/resume/save':
        if ($method === 'POST') $resumeController->save();
        break;
    case '/api/resume/list':
        if ($method === 'GET') $resumeController->list();
        break;
    case '/api/resume/get':
        if ($method === 'GET') $resumeController->get();
        break;
    case '/api/resume/last':
        if ($method === 'GET') $resumeController->getLast();
        break;
    case '/api/resume/generate':
        if ($method === 'POST') $resumeController->generate();
        break;
    default:
        http_response_code(404);
        echo json_encode(['error' => 'Endpoint not found']);
}
?>