<?php

declare(strict_types=1);

namespace App\Services\Evidence;

use App\Support\Evidence\EvidenceCompletenessState;

final class EvidenceCompletenessEvaluator
{
    /**
     * @param  list<array{state: string, required: bool}>  $items
     */
    public function evaluate(array $items): EvidenceCompletenessState
    {
        $requiredItems = array_values(array_filter($items, static fn (array $item): bool => $item['required'] === true));

        if ($requiredItems === []) {
            return EvidenceCompletenessState::Missing;
        }

        foreach ($requiredItems as $item) {
            if ($item['state'] === EvidenceCompletenessState::Missing->value) {
                return EvidenceCompletenessState::Missing;
            }
        }

        foreach ($requiredItems as $item) {
            if ($item['state'] === EvidenceCompletenessState::Stale->value) {
                return EvidenceCompletenessState::Stale;
            }
        }

        foreach ($requiredItems as $item) {
            if ($item['state'] === EvidenceCompletenessState::Partial->value) {
                return EvidenceCompletenessState::Partial;
            }
        }

        return EvidenceCompletenessState::Complete;
    }
}
