<?php

declare(strict_types=1);

namespace App\Support\References;

use App\Support\Badges\BadgeDomain;
use App\Support\Badges\BadgeRenderer;
use App\Support\Badges\BadgeSpec;

final class ReferenceStatePresenter
{
    public function badgeSpec(ReferenceResolutionState|string $state): BadgeSpec
    {
        $normalized = $state instanceof ReferenceResolutionState ? $state->value : $state;

        return BadgeRenderer::spec(BadgeDomain::ReferenceResolutionState, $normalized);
    }

    public function description(ResolvedReference $reference, string $typeLabel): ?string
    {
        $description = $reference->meta['state_description'] ?? null;

        if (is_string($description) && trim($description) !== '') {
            return trim($description);
        }

        $subject = mb_strtolower($typeLabel);

        return match ($reference->state) {
            ReferenceResolutionState::Resolved => null,
            ReferenceResolutionState::PartiallyResolved => "Showing the best {$subject} label available from local context.",
            ReferenceResolutionState::Unresolved => "Only the preserved technical identifier is available for this {$subject}.",
            ReferenceResolutionState::DeletedOrMissing => "The referenced {$subject} is no longer available locally.",
            ReferenceResolutionState::Inaccessible => "The referenced {$subject} is not available in the current scope.",
            ReferenceResolutionState::ExternalLimitedContext => "This {$subject} comes from provider evidence and only limited cached context is available.",
        };
    }
}
