<?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 DeviceComplianceSnapshotTypeRenderer extends FallbackSnapshotTypeRenderer
{
    public function supports(string $policyType): bool
    {
        return $policyType === 'deviceCompliancePolicy';
    }

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

        $odataType = $meta['odata_type'] ?? null;

        if (is_string($odataType) && trim($odataType) !== '') {
            $attributes[] = new RenderedAttribute('OData type', trim($odataType), 'secondary');
        }

        $assignmentTargetCount = $meta['assignment_target_count'] ?? null;

        if (is_numeric($assignmentTargetCount)) {
            $attributes[] = new RenderedAttribute(
                'Assignment targets',
                (string) ((int) $assignmentTargetCount),
                'secondary',
            );
        }

        $threatLevel = $meta['device_threat_protection_required_security_level'] ?? null;

        if (is_string($threatLevel) && trim($threatLevel) !== '') {
            $attributes[] = new RenderedAttribute(
                'Threat protection',
                $this->humanizeValue($threatLevel),
                'secondary',
            );
        }

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