<?php

declare(strict_types=1);

namespace App\Module\Mail\Infrastructure\ApiPlatform\State;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Module\Mail\Domain\Repository\MailConfigurationRepositoryInterface;
use App\Module\Mail\Infrastructure\ApiPlatform\Resource\MailSettings;

final readonly class MailSettingsProvider implements ProviderInterface
{
    public function __construct(
        private MailConfigurationRepositoryInterface $configRepository,
    ) {}

    public function provide(Operation $operation, array $uriVariables = [], array $context = []): MailSettings
    {
        $config = $this->configRepository->findSingleton();
        $dto    = new MailSettings();

        if (null !== $config) {
            $dto->protocol       = $config->getProtocol();
            $dto->imapHost       = $config->getImapHost();
            $dto->imapPort       = $config->getImapPort();
            $dto->imapEncryption = $config->getImapEncryption();
            $dto->smtpHost       = $config->getSmtpHost();
            $dto->smtpPort       = $config->getSmtpPort();
            $dto->smtpEncryption = $config->getSmtpEncryption();
            $dto->username       = $config->getUsername();
            $dto->sentFolderPath = $config->getSentFolderPath();
            $dto->enabled        = $config->isEnabled();
            $dto->hasPassword    = $config->hasPassword();
        }

        return $dto;
    }
}
