@php
    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('admin.title'), 'url' => route('admin.dashboard')],
        ['label' => __('tenants.title')],
    ];
@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.table.table :columns="[
        ['label' => __('tenants.fields.name')],
        ['label' => __('tenants.fields.slug')],
        ['label' => __('tenants.fields.email')],
        ['label' => __('tenants.fields.plan')],
        ['label' => __('tenants.fields.subscription')],
        ['label' => __('tenants.fields.lifecycle')],
        ['label' => __('tenants.fields.status')],
        ['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.table.actions-trigger
                        :type="__('tenants.singular')"
                        :id="$tenant->id"
                        :view-href="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="text-center text-muted py-4">
                    {{ __('tenants.empty') }}
                </td>
            </x-ui.table.row>
        @endforelse
    </x-ui.table.table>

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