<?php

declare(strict_types=1);

namespace App\State\Mail;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\ApiResource\MailSettings;
use App\Repository\MailConfigurationRepository;

final readonly class MailSettingsProvider implements ProviderInterface
{
    public function __construct(
        private MailConfigurationRepository $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;
    }
}
