@php
    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('admin.title'), 'url' => route('admin.dashboard')],
        ['label' => __('tenants.title')],
    ];
    $emptyTitle = request()->hasAny(['q', 'is_active', 'plan_id', 'sort', 'dir'])
        ? __('common.no_matching_records')
        : __('tenants.empty');
@endphp

<x-ui.layout.page
    :title="__('tenants.title')"
    :subtitle="__('tenants.subtitle')"
    page-icon="fe fe-users"
    :breadcrumbs="$breadcrumbs"
>
    <x-slot:actions>
        @can('tenants.create')
            <x-ui.buttons.primary :href="route('admin.tenants.create')" icon="fe fe-plus">
                {{ __('tenants.actions.add') }}
            </x-ui.buttons.primary>
        @endcan
    </x-slot:actions>

    <x-ui.operational.index-shell>
        <x-slot:toolbar>
            <x-ui.operational.action-bar>
                <x-slot:filters>
                    <x-ui.operational.index-filters :fields="[
                        [
                            'type' => 'search',
                            'name' => 'q',
                            'label' => __('common.search'),
                            'width' => '14rem',
                        ],
                        [
                            'type' => 'select',
                            'name' => 'plan_id',
                            'label' => __('tenants.fields.plan'),
                            'options' => collect($plans)->prepend(__('common.all'), '')->all(),
                            'width' => '10rem',
                        ],
                        [
                            'type' => 'select',
                            'name' => 'is_active',
                            'label' => __('tenants.fields.status'),
                            'options' => ['' => __('common.all'), '1' => __('common.active'), '0' => __('common.inactive')],
                            'width' => '9rem',
                        ],
                    ]" />
                </x-slot:filters>
            </x-ui.operational.action-bar>
        </x-slot:toolbar>

        <x-ui.table.table :columns="[
            ['label' => __('tenants.fields.name'), 'sort' => 'name'],
            ['label' => __('tenants.fields.slug'), 'sort' => 'slug'],
            ['label' => __('tenants.fields.email')],
            ['label' => __('tenants.fields.plan')],
            ['label' => __('tenants.fields.subscription')],
            ['label' => __('tenants.fields.lifecycle')],
            ['label' => __('tenants.fields.status'), 'sort' => 'is_active'],
            ['label' => __('common.actions'), 'align' => 'end', 'width' => '120px'],
        ]">
            @forelse ($tenants as $tenant)
            @php
                $subscription = $tenant->activeSubscription;
                $statusKey = $subscription?->status;
            @endphp
            <x-ui.table.row>
                <td><strong>{{ $tenant->name }}</strong></td>
                <td><code>{{ $tenant->slug }}</code></td>
                <td>{{ $tenant->email ?? '—' }}</td>
                <td>{{ $tenant->plan?->name ?? '—' }}</td>
                <td>
                    @if ($statusKey)
                        <span class="badge bg-primary-subtle text-primary">
                            {{ __('tenants.subscription_status.'.$statusKey) }}
                        </span>
                        @if ($subscription?->trial_ends_at)
                            <small class="text-muted d-block">
                                {{ __('tenants.trial_until', ['date' => $subscription->trial_ends_at->format('Y-m-d')]) }}
                            </small>
                        @endif
                    @else
                        <span class="text-muted">—</span>
                    @endif
                </td>
                <td><code class="small">{{ $tenant->lifecycle_state }}</code></td>
                <td>
                    @if ($tenant->is_active)
                        <span class="badge bg-success">{{ __('common.active') }}</span>
                    @else
                        <span class="badge bg-secondary">{{ __('common.inactive') }}</span>
                    @endif
                </td>
                <td class="text-end">
                    <x-ui.action-modal.trigger
                        :entity="__('tenants.singular')"
                        :record="$tenant->name"
                        :edit-url="auth()->user()?->can('tenants.update', $tenant) ? route('admin.tenants.edit', $tenant) : null"
                        :delete-url="auth()->user()?->can('tenants.delete', $tenant) ? route('admin.tenants.destroy', $tenant) : null"
                    />
                </td>
            </x-ui.table.row>
        @empty
            <x-ui.table.row>
                <td colspan="8" class="p-0">
                    <x-ui.operational.empty-state :title="$emptyTitle" />
                </td>
            </x-ui.table.row>
        @endforelse
    </x-ui.table.table>

        <x-slot:pagination>
            <x-ui.operational.pagination :paginator="$tenants" />
        </x-slot:pagination>
    </x-ui.operational.index-shell>

    <x-ui.action-modal />
</x-ui.layout.page>
