<?php
// bootstrap.php
// Posizione consigliata: project_root/bootstrap.php

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

use Api\Core\Container;
use Api\Core\Config;
use Api\Core\HttpClient;
use Api\Core\CorsManager;
use Api\Core\RateLimiter\RateLimiterInterface;
use Api\Core\RateLimiter\FileRateLimiter;
use Api\Core\RateLimiter\InMemoryRateLimiter;
use Api\Core\Log\LoggerFactory;
use ElixForms\ElixFormsClient;
use ElixForms\Auth\ElixFormsApiClient;
use ElixForms\Auth\ElixFormsAuthenticationClient;
use Psr\Log\LoggerInterface;

$container = new Container();

$config = new Config();
$container->singleton(Config::class, function() use ($config) {
    return $config;
});

$container->singleton(HttpClient::class, function() {
    return new HttpClient();
});

// CORS manager binding
$container->singleton(CorsManager::class, function($c) {
    $config = $c->make(Config::class);
    $corsConfig = $config->get('cors', []);
    return new CorsManager($corsConfig);
});

// Rate limiter driver configurabile via config or env: 'file' or 'memory'
$rlDriver = $config->get('rate_limiter_driver', 'file');

if ($rlDriver === 'memory') {
    $container->singleton(RateLimiterInterface::class, function($c) {
        $config = $c->make(Config::class);
        $requests = (int) $config->get('rate_limit_requests', 100);
        $window = (int) $config->get('rate_limit_window_seconds', 60);
        return new InMemoryRateLimiter($requests, $window);
    });
} else {
    $container->singleton(RateLimiterInterface::class, function($c) {
        $config = $c->make(Config::class);
        $dir = $config->get('rate_limit_storage_dir', sys_get_temp_dir() . '/api_rate_limit');
        $requests = (int) $config->get('rate_limit_requests', 100);
        $window = (int) $config->get('rate_limit_window_seconds', 60);
        return new FileRateLimiter($dir, $requests, $window);
    });
}

// Logger binding as PSR LoggerInterface
$container->singleton(LoggerInterface::class, function($c) {
    $config = $c->make(Config::class);
    // LoggerFactory::create returns a Monolog\Logger instance
    return LoggerFactory::create($config->get('log_ident', 'api'));
});

// ElixForms client binding
$container->singleton(ElixFormsClient::class, function($c) {
    $config = $c->make(Config::class);
    $baseUrl = $config->get('elixforms_api_base_url');
    return new ElixFormsClient($baseUrl);
});

$container->singleton(ElixFormsAuthenticationClient::class, function($c) {
    $config = $c->make(Config::class);

    return new ElixFormsAuthenticationClient($config->get('elixforms_api_base_url'));
});

$container->singleton(ElixFormsApiClient::class, function($c) {
    $config = $c->make(Config::class);

    return new ElixFormsApiClient(
        $config->get('elixforms_api_base_url'),
        $c->make(HttpClient::class)
    );
});

// If you have other services, bind them here, for example:
// $container->singleton(SomeService::class, function($c) {
//     return new SomeService($c->make(LoggerInterface::class), ...);
// });

return $container;
