<?php

use Picea\Picea;

use Negundo\Client\{ NegundoMiddleware, SoftwareConfig };

use Laminas\Diactoros\Response\HtmlResponse;

use Psr\Http\Server\MiddlewareInterface,
    Psr\Http\Message\ServerRequestInterface,
    Psr\Http\Server\RequestHandlerInterface,
    Psr\Http\Message\ResponseInterface;

use function DI\{ create, get, autowire };

error_reporting(getenv('ERROR_REPORTING') ?: ( E_ALL & ~E_USER_DEPRECATED & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE ));

return [
    /* do nothing in production !*/
    "dump" => function($c) {
        if (! function_exists('dump') ) {
            function dump(...$what) {}
        }

        return new class implements MiddlewareInterface {
            public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface { return $handler->handle($request); }
        };
    },

    SoftwareConfig::class => create(SoftwareConfig::class)->constructor(getenv('NEGUNDO_HASH'), getenv('NEGUNDO_SERVER')),

    "errorHandler" => create(NegundoMiddleware::class)->constructor(get(SoftwareConfig::class), null, get('app.errorhandler.html')),

    'app.errorhandler.html' => function($c, Picea $picea) {
        return function(\Throwable $exception) use ($picea) {
            error_log($exception->getMessage());

            return new HtmlResponse($picea->renderHtml('lean/error/500', [
                'title' => "Une erreur s'est produite lors de l'exécution du script.",
                'subtitle' => "Êtes-vous connecté avec le bon compte ?",
                'message' => $exception->getMessage(),
                'exception' => $exception,
            ]), 500);
        };
    },

    'routes.caching' => create(Kash\ApcuCache::class)->constructor(get(Kash\CacheInvalidator::class), "lean.routes", random_int(3600, 7200)),
    'breadcrumbs.caching' => create(Kash\ApcuCache::class)->constructor(get(Kash\CacheInvalidator::class), "lean.breadcrumbs", random_int(3600, 7200)),
    'ulmus.caching' => create(Kash\ApcuCache::class)->constructor( get(Kash\CacheInvalidator::class), "ulmus.entities", random_int(3600, 7200)),
    'cronard.caching' => create(Kash\ApcuCache::class)->constructor( get(Kash\CacheInvalidator::class), "lean.cronards", random_int(3600, 7200)),
    'events.caching' => create(Kash\ApcuCache::class)->constructor( get(Kash\CacheInvalidator::class), "lean.events", random_int(3600, 7200)),
    'cli.caching' => create(Kash\ApcuCache::class)->constructor( get(Kash\CacheInvalidator::class), "lean.cli", random_int(3600, 7200)),
];