<?php

declare(strict_types=1);

final class FormNotificationFailureService
{
    public function __construct(
        private readonly MicFormNotificationFailureRepository $failures,
        private readonly MicFormNotificationDispatcher $dispatcher,
    ) {
    }

    public function listRecent(): array
    {
        return ['failures' => $this->failures->listRecent(20)];
    }

    public function retry(string $failureId, string $submissionId): array
    {
        $result = $this->dispatcher->dispatch($submissionId);
        if ($result->ok) {
            $this->failures->delete($failureId);
        } else {
            $this->failures->markRetried($failureId);
        }
        return ['result' => $result->toArray()];
    }

    public function dismiss(string $failureId): array
    {
        $this->failures->delete($failureId);
        return ['dismissed' => true];
    }
}
