<?php
declare(strict_types=1);

// Ensure autoloader is loaded
require_once __DIR__ . '/../vendor/autoload.php';

use DI\ContainerBuilder;
use Slim\Factory\AppFactory;

// Path definitions
define('APP_ROOT', __DIR__ . '/..');

// Load .env file if exists
if (file_exists(APP_ROOT . '/.env')) {
    $dotenv = Dotenv\Dotenv::createImmutable(APP_ROOT);
    $dotenv->load();
}

// Initialize container
$containerBuilder = new ContainerBuilder();

// Add container definitions
$containerBuilder->addDefinitions(APP_ROOT . '/src/container.php');

// Create app
$container = $containerBuilder->build();
AppFactory::setContainer($container);
$app = AppFactory::create();

// Register middleware
$middleware = require APP_ROOT . '/src/middleware.php';
$middleware($app);

// Register routes
$routes = require APP_ROOT . '/src/routes.php';
$routes($app);

// Run app
$app->run();
