<?php

declare(strict_types=1);

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

use App\Config\Container;

// Load .env file safely
$envFile = __DIR__ . '/../.env';
if (file_exists($envFile)) {
    $dotenv = new \Symfony\Component\Dotenv\Dotenv();
    $dotenv->load($envFile);
}

// Boot Eloquent BEFORE creating the app
Container::boot();

$container = Container::build();
\Slim\Factory\AppFactory::setContainer($container);

$app = \Slim\Factory\AppFactory::create();

// CORS Middleware
$app->add(new \Tuupola\Middleware\CorsMiddleware([
    'origin' => ['https://fahrschultermin.de', 'https://www.fahrschultermin.de'],
    'methods' => ['GET', 'POST', 'PATCH', 'DELETE', 'OPTIONS'],
    'headers.allow' => ['Content-Type', 'Authorization', 'X-Requested-With'],
    'credentials' => true,
    'cache.maxage' => 86400,
]));

// Routes
(require __DIR__ . '/../src/Routes/api.php')($app);

$app->run();