<?php
declare(strict_types=1);

use Psr\Container\ContainerInterface;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Processor\UidProcessor;

require_once __DIR__ . '/config.php';

return [
    'logger' => function (ContainerInterface $c) {
        $logger = new Logger('tigo-nova');

        $processor = new UidProcessor();
        $logger->pushProcessor($processor);

        $handler = new StreamHandler(APP_ROOT . '/logs/app.log', 'debug');
        $logger->pushHandler($handler);

        return $logger;
    },
    'db' => function (ContainerInterface $c) {
        $host = DB_HOST;
        $dbname = DB_NAME;
        $user = DB_USER;
        $pass = DB_PASS;
        
        $dsn = "mysql:host=$host;dbname=$dbname;charset=utf8mb4";
        $options = [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES => false,
        ];
        
        try {
            return new PDO($dsn, $user, $pass, $options);
        } catch (\PDOException $e) {
            throw new \PDOException($e->getMessage(), (int)$e->getCode());
        }
    }
];
