@php
    /** @var \App\Models\ManagedEnvironment $tenant */
    $tenant = $this->tenant;
    $customerHealthDecision = $this->customerHealthDecision();
    $providerConnections = $this->providerConnections();
    $permissions = $this->managedEnvironmentPermissions();
    $runs = $this->recentRuns();
    $workspacePostureValue = $tenant->isRemovedFromWorkspace() ? 'removed_from_workspace' : 'active';
@endphp

<x-filament-panels::page>
    <div class="space-y-6">
        <x-filament::section>
            <x-slot name="heading">
                {{ $tenant->name }}
            </x-slot>

            <x-slot name="description">
                Workspace: {{ $tenant->workspace?->name ?? 'Unknown' }}
            </x-slot>

            <div class="flex flex-wrap items-center gap-2">
                <x-filament::badge
                    :color="\App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::TenantStatus, (string) $tenant->status)->color"
                    :icon="\App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::TenantStatus, (string) $tenant->status)->icon"
                >
                    {{ \App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::TenantStatus, (string) $tenant->status)->label }}
                </x-filament::badge>

                <x-filament::badge
                    :color="\App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::TenantWorkspacePosture, $workspacePostureValue)->color"
                    :icon="\App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::TenantWorkspacePosture, $workspacePostureValue)->icon"
                >
                    {{ \App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::TenantWorkspacePosture, $workspacePostureValue)->label }}
                </x-filament::badge>

                @if ($tenant->external_id)
                    <span class="text-xs text-gray-500 dark:text-gray-400">External ID: {{ $tenant->external_id }}</span>
                @endif
            </div>

            @if ($tenant->isRemovedFromWorkspace())
                <div class="mt-4 rounded-lg border border-warning-200 bg-warning-50 px-4 py-3 text-sm text-warning-800 dark:border-warning-500/30 dark:bg-warning-500/10 dark:text-warning-200">
                    <p class="font-semibold">Removed from workspace</p>
                    <p class="mt-1">{{ $tenant->workspaceRemovalReason() ?? 'No removal reason recorded.' }}</p>
                    <p class="mt-1">Active tenant selection and new tenant operations are blocked. Audit, evidence, and operation history remain visible.</p>
                </div>
            @endif

            <div class="mt-4">
                <x-filament::link :href="$this->adminTenantUrl()" icon="heroicon-m-arrow-top-right-on-square">
                    Open in tenant admin
                </x-filament::link>

                <p class="mt-2 text-xs text-gray-500 dark:text-gray-400">
                    Requires tenant admin membership.
                </p>
            </div>
        </x-filament::section>

        @if ($customerHealthDecision)
            @include('filament.system.pages.directory.partials.customer-health-decision-card', ['decision' => $customerHealthDecision])
        @endif

        <x-filament::section>
            <x-slot name="heading">
                Connectivity signals
            </x-slot>

            @if ($providerConnections->isEmpty())
                <p class="text-sm text-gray-500 dark:text-gray-400">No provider connections found.</p>
            @else
                <div class="space-y-2">
                    @foreach ($providerConnections as $connection)
                        <div class="rounded-lg border border-gray-200 px-4 py-3 dark:border-white/10">
                            <div class="flex flex-wrap items-center gap-2">
                                <span class="font-medium text-gray-950 dark:text-white">{{ $connection->provider }}</span>

                                @if ($connection->display_name)
                                    <span class="text-xs text-gray-500 dark:text-gray-400">{{ $connection->display_name }}</span>
                                @endif

                                <x-filament::badge
                                    :color="\App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::BooleanEnabled, $connection->is_enabled)->color"
                                    :icon="\App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::BooleanEnabled, $connection->is_enabled)->icon"
                                >
                                    {{ \App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::BooleanEnabled, $connection->is_enabled)->label }}
                                </x-filament::badge>

                                <x-filament::badge
                                    :color="\App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::ProviderConsentStatus, $connection->consent_status)->color"
                                    :icon="\App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::ProviderConsentStatus, $connection->consent_status)->icon"
                                >
                                    {{ \App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::ProviderConsentStatus, $connection->consent_status)->label }}
                                </x-filament::badge>

                                <x-filament::badge
                                    :color="\App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::ProviderVerificationStatus, $connection->verification_status)->color"
                                    :icon="\App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::ProviderVerificationStatus, $connection->verification_status)->icon"
                                >
                                    {{ \App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::ProviderVerificationStatus, $connection->verification_status)->label }}
                                </x-filament::badge>

                                @if ($connection->is_default)
                                    <x-filament::badge color="info">Default</x-filament::badge>
                                @endif
                            </div>

                            <div class="mt-2 flex flex-wrap gap-4 text-xs text-gray-500 dark:text-gray-400">
                                <span>Last check: {{ $connection->last_health_check_at?->diffForHumans() ?? 'Never' }}</span>

                                @if ($connection->last_error_reason_code)
                                    <span>Last error: {{ $connection->last_error_reason_code }}</span>
                                @endif
                            </div>
                        </div>
                    @endforeach
                </div>
            @endif
        </x-filament::section>

        <x-filament::section>
            <x-slot name="heading">
                Permission signals
            </x-slot>

            @if ($permissions->isEmpty())
                <p class="text-sm text-gray-500 dark:text-gray-400">No cached permission checks available.</p>
            @else
                <div class="space-y-2">
                    @foreach ($permissions as $permission)
                        <div class="flex items-center justify-between rounded-lg border border-gray-200 px-4 py-3 dark:border-white/10">
                            <span class="font-medium text-gray-950 dark:text-white">{{ $permission->permission_key }}</span>
                            <x-filament::badge
                                :color="\App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::ManagedEnvironmentPermissionStatus, (string) $permission->status)->color"
                            >
                                {{ \App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::ManagedEnvironmentPermissionStatus, (string) $permission->status)->label }}
                            </x-filament::badge>
                        </div>
                    @endforeach
                </div>
            @endif
        </x-filament::section>

        <x-filament::section>
            <x-slot name="heading">
                Recent operations
            </x-slot>

            @if ($runs->isEmpty())
                <p class="text-sm text-gray-500 dark:text-gray-400">No recent operation runs for this tenant.</p>
            @else
                <div class="space-y-2">
                    @foreach ($runs as $run)
                        <a
                            href="{{ $run['url'] }}"
                            class="block rounded-lg border border-gray-200 px-4 py-3 hover:border-primary-400 hover:bg-gray-50 dark:border-white/10 dark:hover:border-primary-500 dark:hover:bg-white/5"
                        >
                            <div class="flex items-center justify-between">
                                <span class="font-medium text-gray-950 dark:text-white">#{{ $run['id'] }} · {{ $run['label'] }}</span>
                                <span class="text-xs text-gray-500 dark:text-gray-400">{{ $run['started'] }}</span>
                            </div>
                        </a>
                    @endforeach
                </div>
            @endif

            <div class="mt-4">
                <x-filament::link :href="$this->runsUrl()" icon="heroicon-m-arrow-top-right-on-square">
                    Open operations runs
                </x-filament::link>
            </div>
        </x-filament::section>
    </div>
</x-filament-panels::page>
