<?php

declare(strict_types=1);

namespace App\State;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\ApiResource\ZimbraSettings;
use App\Repository\ZimbraConfigurationRepository;

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