@php
    /** @var \App\Models\Workspace $workspace */
    $workspace = $this->workspace;
    $tenants = $this->workspaceTenants();
    $runs = $this->recentRuns();
@endphp

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

            <x-slot name="description">
                Workspace #{{ (int) $workspace->getKey() }}
            </x-slot>

            <div class="grid grid-cols-1 gap-4 sm:grid-cols-3">
                <div class="rounded-lg bg-gray-50 px-4 py-3 dark:bg-white/5">
                    <p class="text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">Tenants</p>
                    <p class="mt-1 text-2xl font-bold text-gray-950 dark:text-white">{{ number_format((int) $workspace->tenants_count) }}</p>
                </div>
            </div>

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

        <x-filament::section>
            <x-slot name="heading">
                Tenants summary
            </x-slot>

            @if ($tenants->isEmpty())
                <p class="text-sm text-gray-500 dark:text-gray-400">No tenants are attached to this workspace.</p>
            @else
                <div class="space-y-2">
                    @foreach ($tenants as $tenant)
                        <a
                            href="{{ \App\Support\System\SystemDirectoryLinks::tenantDetail($tenant) }}"
                            class="flex items-center justify-between 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"
                        >
                            <span class="font-medium text-gray-950 dark:text-white">{{ $tenant->name }}</span>
                            <x-filament::badge
                                :color="\App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::TenantStatus, (string) $tenant->status)->color"
                            >
                                {{ \App\Support\Badges\BadgeRenderer::spec(\App\Support\Badges\BadgeDomain::TenantStatus, (string) $tenant->status)->label }}
                            </x-filament::badge>
                        </a>
                    @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 workspace.</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>
