<?php

declare(strict_types=1);

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

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Module\Mail\Domain\Entity\MailConfiguration;
use App\Module\Mail\Domain\Repository\MailConfigurationRepositoryInterface;
use App\Module\Mail\Infrastructure\ApiPlatform\Resource\MailSettings;
use App\Shared\Infrastructure\Service\TokenEncryptor;
use Doctrine\ORM\EntityManagerInterface;

final readonly class MailSettingsProcessor implements ProcessorInterface
{
    public function __construct(
        private EntityManagerInterface $em,
        private MailConfigurationRepositoryInterface $configRepository,
        private TokenEncryptor $tokenEncryptor,
    ) {}

    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): MailSettings
    {
        assert($data instanceof MailSettings);

        $config = $this->configRepository->findSingleton();
        if (null === $config) {
            $config = new MailConfiguration();
        }

        if (null !== $data->protocol) {
            $config->setProtocol($data->protocol);
        }
        if (null !== $data->imapHost) {
            $config->setImapHost($data->imapHost);
        }
        if (null !== $data->imapPort) {
            $config->setImapPort($data->imapPort);
        }
        if (null !== $data->imapEncryption) {
            $config->setImapEncryption($data->imapEncryption);
        }
        if (null !== $data->smtpHost) {
            $config->setSmtpHost($data->smtpHost);
        }
        if (null !== $data->smtpPort) {
            $config->setSmtpPort($data->smtpPort);
        }
        if (null !== $data->smtpEncryption) {
            $config->setSmtpEncryption($data->smtpEncryption);
        }
        if (null !== $data->username) {
            $config->setUsername($data->username);
        }
        if (null !== $data->sentFolderPath) {
            $config->setSentFolderPath($data->sentFolderPath);
        }
        $config->setEnabled($data->enabled);

        if (null !== $data->password && '' !== $data->password) {
            $config->setEncryptedPassword($this->tokenEncryptor->encrypt($data->password));
        }

        $this->em->persist($config);
        $this->em->flush();

        $result                 = new MailSettings();
        $result->protocol       = $config->getProtocol();
        $result->imapHost       = $config->getImapHost();
        $result->imapPort       = $config->getImapPort();
        $result->imapEncryption = $config->getImapEncryption();
        $result->smtpHost       = $config->getSmtpHost();
        $result->smtpPort       = $config->getSmtpPort();
        $result->smtpEncryption = $config->getSmtpEncryption();
        $result->username       = $config->getUsername();
        $result->sentFolderPath = $config->getSentFolderPath();
        $result->enabled        = $config->isEnabled();
        $result->hasPassword    = $config->hasPassword();

        return $result;
    }
}
