<?php

declare(strict_types=1);

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

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\State\ProviderInterface;
use App\Module\Integration\Infrastructure\ApiPlatform\Resource\ZimbraTestConnection;
use App\Module\ProjectManagement\Infrastructure\Service\CalDavService;
use Throwable;

final readonly class ZimbraTestConnectionProvider implements ProviderInterface, ProcessorInterface
{
    public function __construct(
        private CalDavService $calDavService,
    ) {}

    public function provide(Operation $operation, array $uriVariables = [], array $context = []): ZimbraTestConnection
    {
        return new ZimbraTestConnection();
    }

    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): ZimbraTestConnection
    {
        $result = new ZimbraTestConnection();

        try {
            $result->success = $this->calDavService->testConnection();
        } catch (Throwable) {
            $result->success = false;
        }

        return $result;
    }
}
