<?php

namespace App\Services\Inventory\DependencyTargets;

use App\Filament\Resources\InventoryItemResource;
use App\Models\ManagedEnvironment;

class DependencyTargetDto
{
    public function __construct(
        public readonly string $targetLabel,
        public readonly ?string $displayName,
        public readonly ?string $maskedId,
        public readonly bool $resolved,
        public readonly ?string $linkUrl,
        public readonly ?string $foundationType,
        public readonly ?string $reasonUnresolved,
        public readonly string $badgeText,
    ) {}

    public static function missing(): self
    {
        return new self(
            targetLabel: 'Missing target',
            displayName: null,
            maskedId: null,
            resolved: false,
            linkUrl: null,
            foundationType: null,
            reasonUnresolved: 'missing_target',
            badgeText: 'Missing target',
        );
    }

    public static function externalGroup(string $targetId): self
    {
        return static::externalGroupWithLabel($targetId, 'Group (external)');
    }

    public static function externalGroupWithLabel(string $targetId, string $label): self
    {
        $maskedId = static::mask($targetId);

        return new self(
            targetLabel: $label,
            displayName: null,
            maskedId: $maskedId,
            resolved: false,
            linkUrl: null,
            foundationType: 'aad_group',
            reasonUnresolved: 'external_reference',
            badgeText: "{$label}: {$maskedId}",
        );
    }

    public static function unresolvedFoundation(string $label, string $foundationType, string $targetId): self
    {
        $maskedId = static::mask($targetId);

        return new self(
            targetLabel: $label,
            displayName: null,
            maskedId: $maskedId,
            resolved: false,
            linkUrl: null,
            foundationType: $foundationType,
            reasonUnresolved: 'not_in_local_db',
            badgeText: "{$label} (unresolved): {$maskedId}",
        );
    }

    public static function resolvedFoundation(string $label, string $foundationType, string $targetId, string $displayName, ?int $inventoryItemId, ManagedEnvironment $tenant): self
    {
        $maskedId = static::mask($targetId);
        $url = $inventoryItemId ? InventoryItemResource::getUrl('view', ['record' => $inventoryItemId], tenant: $tenant) : null;

        return new self(
            targetLabel: $label,
            displayName: $displayName,
            maskedId: $maskedId,
            resolved: true,
            linkUrl: $url,
            foundationType: $foundationType,
            reasonUnresolved: null,
            badgeText: "{$label}: {$displayName} ({$maskedId})",
        );
    }

    public static function externalReference(string $targetId, ?string $foundationType = null): self
    {
        $maskedId = static::mask($targetId);
        $label = $foundationType ? "External ref ({$foundationType})" : 'External reference';

        return new self(
            targetLabel: $label,
            displayName: null,
            maskedId: $maskedId,
            resolved: false,
            linkUrl: null,
            foundationType: $foundationType,
            reasonUnresolved: 'unsupported_foundation_type',
            badgeText: "{$label}: {$maskedId}",
        );
    }

    public function toArray(): array
    {
        return [
            'target_label' => $this->targetLabel,
            'display_name' => $this->displayName,
            'masked_id' => $this->maskedId,
            'resolved' => $this->resolved,
            'link_url' => $this->linkUrl,
            'foundation_type' => $this->foundationType,
            'reason_unresolved' => $this->reasonUnresolved,
            'badge_text' => $this->badgeText,
        ];
    }

    private static function mask(string $id): string
    {
        return substr($id, 0, 6).'…';
    }
}
