<?php
/**
 * Epic Travel & Expeditions - Main API Entry Point
 * This file routes all API requests to appropriate handlers
 */

// Load configuration and dependencies
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/includes/database.php';
require_once __DIR__ . '/includes/jwt.php';
require_once __DIR__ . '/includes/functions.php';

// Set CORS headers
setCorsHeaders();

// Get request method and path
$method = $_SERVER['REQUEST_METHOD'];
$path = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/';
$path = trim($path, '/');

// Parse path
$pathParts = explode('/', $path);
$resource = isset($pathParts[0]) ? $pathParts[0] : '';
$id = isset($pathParts[1]) ? $pathParts[1] : null;

// Health check endpoint
if ($path === '' || $path === 'api') {
    jsonResponse([
        'message' => 'Epic Travel API is running',
        'status' => 'healthy'
    ]);
}

// Route to appropriate handler
try {
    switch ($resource) {
        case 'auth':
            require __DIR__ . '/api/auth.php';
            break;
            
        case 'destinations':
            require __DIR__ . '/api/destinations.php';
            break;
            
        case 'specials':
            require __DIR__ . '/api/specials.php';
            break;
            
        case 'contact':
            require __DIR__ . '/api/contact.php';
            break;
            
        case 'newsletter':
            require __DIR__ . '/api/newsletter.php';
            break;
            
        case 'upload':
            require __DIR__ . '/api/upload.php';
            break;
            
        case 'download':
            require __DIR__ . '/api/download.php';
            break;
            
        default:
            jsonResponse(['error' => 'Endpoint not found'], 404);
    }
} catch (Exception $e) {
    if (DEBUG_MODE) {
        jsonResponse(['error' => $e->getMessage()], 500);
    } else {
        jsonResponse(['error' => 'Internal server error'], 500);
    }
}
