<?php
/**
 * fahrschuldesk API Bootstrap
 */

declare(strict_types=1);

// Error reporting
error_reporting(E_ALL);
ini_set('display_errors', '0');
ini_set('log_errors', '1');

// Base paths
define('ROOT_PATH', dirname(__DIR__));
define('STORAGE_PATH', ROOT_PATH . '/storage');
define('CONFIG_PATH', ROOT_PATH . '/config');

// Load Composer autoloader
require ROOT_PATH . '/vendor/autoload.php';

// Load configuration
$config = require CONFIG_PATH . '/app.php';

// Create DI container
$container = new \DI\Container();

// Database connection
$container->set('db', function () use ($config) {
    $dsn = sprintf(
        'pgsql:host=%s;port=%d;dbname=%s',
        $config['db']['host'],
        $config['db']['port'],
        $config['db']['database']
    );
    
    try {
        $pdo = new PDO($dsn, $config['db']['username'], $config['db']['password'], [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES => false,
        ]);
        return $pdo;
    } catch (PDOException $e) {
        throw new \RuntimeException('Database connection failed: ' . $e->getMessage());
    }
});

// JWT Secret
$container->set('jwt_secret', $config['jwt']['secret']);

// Return container
return $container;