<?php

declare(strict_types=1);

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

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Module\Integration\Domain\Repository\ZimbraConfigurationRepositoryInterface;
use App\Module\Integration\Infrastructure\ApiPlatform\Resource\ZimbraSettings;

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

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

        if (null !== $config) {
            $dto->serverUrl    = $config->getServerUrl();
            $dto->username     = $config->getUsername();
            $dto->calendarPath = $config->getCalendarPath();
            $dto->enabled      = $config->isEnabled();
            $dto->hasPassword  = $config->hasPassword();
        }

        return $dto;
    }
}
