<?php

declare(strict_types=1);

namespace Apexl\Io\Logger\Services\HandlerCreator;

use Apexl\Io\Logger\Enums\Handler;
use Apexl\Io\Logger\Interfaces\HandlerCreatorInterface;
use UnexpectedValueException;
use UnhandledMatchError;

final readonly class HandlerCreatorFactory
{
    public function __construct(
        private ErrorLogHandlerCreator $errorLogHandlerCreator,
        private FileLogHandlerCreator $fileLogHandlerCreator,
        private LogstashLogHandlerCreator $logstashLogHandlerCreator,
        private DatabaseLogHandlerCreator $databaseLogHandlerCreator,
        private MailLogHandlerCreator $mailLogHandlerCreator,
        private SlackLogHandlerCreator $slackLogHandlerCreator,
        private GoogleChatLogHandlerCreator $googleChatLogHandlerCreator,
    ) {}

    public function make(
        Handler $destination,
    ): HandlerCreatorInterface {
        try {
            return match ($destination) {
                Handler::ERROR_LOG => $this->errorLogHandlerCreator,
                Handler::FILE => $this->fileLogHandlerCreator,
                Handler::LOGSTASH => $this->logstashLogHandlerCreator,
                Handler::DATABASE => $this->databaseLogHandlerCreator,
                Handler::MAIL => $this->mailLogHandlerCreator,
                Handler::SLACK => $this->slackLogHandlerCreator,
                Handler::GOOGLE_CHAT => $this->googleChatLogHandlerCreator,
            };
        } catch (UnhandledMatchError) {
            throw new UnexpectedValueException(
                sprintf('Handler %s not found', $destination->value)
            );
        }
    }
}