<?php

declare(strict_types=1);

namespace App\Services\Baselines\SnapshotRendering\Renderers;

use App\Models\BaselineSnapshotItem;
use App\Services\Baselines\SnapshotRendering\FidelityState;
use App\Services\Baselines\SnapshotRendering\GapSummary;
use App\Services\Baselines\SnapshotRendering\RenderedAttribute;
use App\Services\Baselines\SnapshotRendering\RenderedSnapshotItem;

final class IntuneRoleDefinitionSnapshotTypeRenderer extends FallbackSnapshotTypeRenderer
{
    public function supports(string $policyType): bool
    {
        return $policyType === 'intuneRoleDefinition';
    }

    public function render(BaselineSnapshotItem $item): RenderedSnapshotItem
    {
        $meta = $this->meta($item);
        $fidelity = FidelityState::fromEvidence(data_get($meta, 'evidence.fidelity'));
        $attributes = $this->baseAttributes($item, $meta);

        $attributes[] = new RenderedAttribute(
            'Role source',
            match (data_get($meta, 'rbac.is_built_in')) {
                true => 'Built-in',
                false => 'Custom',
                default => 'Unknown',
            },
        );

        $permissionBlocks = data_get($meta, 'rbac.role_permission_count');

        if (is_numeric($permissionBlocks)) {
            $attributes[] = new RenderedAttribute('Permission blocks', (string) ((int) $permissionBlocks));
        }

        $attributes[] = new RenderedAttribute(
            'Identity',
            data_get($meta, 'identity.strategy') === 'external_id'
                ? 'Role definition ID'
                : 'Display name',
        );

        return $this->buildItem(
            item: $item,
            fidelity: $fidelity,
            structuredAttributes: $attributes,
            gapSummary: GapSummary::fromItemMeta($meta, $fidelity),
            meta: $meta,
        );
    }
}
