<?php

declare(strict_types=1);

namespace Apexl\Io\Modules;

use Apexl\Config\ConfigurationInterface;
use Apexl\Io\Interfaces\ProvidesHooksInterface;
use Apexl\Io\Logger\Dtos\LoggerConfig;
use Apexl\Io\Logger\Hooks\ConsoleCommands;
use Apexl\Io\Logger\Hooks\ErrorListener;
use Apexl\Io\Logger\Hooks\WhoopsHandlerListener;
use Apexl\Io\Module;
use Slim\App;

readonly class LoggerModule extends Module implements ProvidesHooksInterface
{
    public function __construct(
        ConfigurationInterface $config,
        private readonly LoggerConfig $loggerConfig,
        App $app,
    ) {
        parent::__construct($config, $app);
    }

    public function hooks(): array
    {
        // The command is registered whether or not this install wants the error
        // listeners — catchErrors decides who handles thrown errors, not whether
        // daedalus gets its logging commands.
        $hooks = [
            ConsoleCommands::class,
        ];

        if ($this->loggerConfig->catchErrors) {
            $hooks[] = WhoopsHandlerListener::class;
            $hooks[] = ErrorListener::class;
        }

        return $hooks;
    }
}
