@php
    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('dashboard.title')],
    ];
    $upgradeUrl = route('dashboard.index') . '#dashboard-modules';
    $moduleList = $data->modules;
    $enabledCount = collect($moduleList)->filter(fn ($m) => $m->enabled)->count();
    $totalCount = count($moduleList);
@endphp

<x-ui.layout.page
    :title="__('dashboard.title')"
    :subtitle="__('dashboard.subtitle')"
    page-icon="fe fe-home"
    :breadcrumbs="$breadcrumbs"
>
    @if ($data->isTrial && $data->trialDaysLeft !== null)
        <x-ui.subscription.trial-banner :days-left="$data->trialDaysLeft" :upgrade-url="$upgradeUrl" />
    @endif

    @if (count($data->attentionAlerts) > 0)
        <h5 class="fw-semibold mb-3" style="color: {{ config('theme.colors.text') }};">
            {{ __('dashboard.action_center.attention_heading') }}
        </h5>
        <div class="row g-3 mb-4">
            @foreach ($data->attentionAlerts as $alert)
                <div class="col-12">
                    <div class="card border-0 shadow-sm">
                        <div class="card-body d-flex flex-column flex-md-row align-items-md-center justify-content-between gap-3">
                            <div class="min-w-0">
                                <div class="fw-semibold" style="color: {{ config('theme.colors.text') }};">{{ $alert->title }}</div>
                                <div class="text-muted small">{{ $alert->body }}</div>
                            </div>
                            @if ($alert->actionUrl)
                                <a href="{{ $alert->actionUrl }}" class="btn btn-sm text-white flex-shrink-0"
                                    style="background: {{ config('theme.gradients.primary') }};">
                                    {{ $alert->actionLabel ?? __('dashboard.action_center.review') }}
                                </a>
                            @endif
                        </div>
                    </div>
                </div>
            @endforeach
        </div>
    @endif

    @if (count($data->actionWidgets) > 0)
        <h5 class="fw-semibold mb-3" style="color: {{ config('theme.colors.text') }};">
            {{ __('dashboard.action_center.heading') }}
        </h5>
        <div class="row g-3 mb-4">
            @foreach ($data->actionWidgets as $w)
                <x-ui.dashboard.alert-widget
                    :title="$w->title"
                    :count="$w->count"
                    :severity="$w->severity"
                    :action-label="$w->actionLabel"
                    :action-url="$w->actionUrl"
                    :icon="$w->icon"
                />
            @endforeach
        </div>
    @endif

    <div class="row g-3 mb-4">
        <x-ui.dashboard.stat-card
            :label="__('dashboard.stats.plan')"
            :value="$data->planName ?? __('dashboard.stats.no_plan')"
            icon="fe fe-layers"
        />
        <x-ui.dashboard.stat-card
            :label="__('dashboard.stats.subscription')"
            :value="__($data->subscriptionStatusKey)"
            icon="fe fe-activity"
        />
        @if ($data->isTrial && $data->trialDaysLeft !== null)
            <x-ui.dashboard.stat-card
                :label="__('dashboard.stats.trial_days')"
                :value="(string) $data->trialDaysLeft"
                icon="fe fe-clock"
            />
        @endif
    </div>

    <div class="row g-3 mb-4">
        <x-ui.dashboard.stat-card
            :label="__('dashboard.stats.modules_enabled')"
            :value="$totalCount > 0 ? $enabledCount . ' / ' . $totalCount : '—'"
            icon="fe fe-grid"
        />
        <x-ui.dashboard.stat-card
            :label="__('dashboard.stats.coverage')"
            :value="$totalCount > 0 ? (int) round(($enabledCount / $totalCount) * 100) . '%' : '—'"
            icon="fe fe-pie-chart"
        />
    </div>

    @if (count($data->statCards) > 0)
        <h5 class="fw-semibold mb-3" style="color: {{ config('theme.colors.text') }};">
            {{ __('dashboard.stats_heading') }}
        </h5>
        <div class="row g-3 mb-4">
            @foreach ($data->statCards as $row)
                <x-ui.dashboard.stat-card
                    :label="$row['label']"
                    :value="(string) $row['value']"
                    :icon="$row['icon']"
                />
            @endforeach
        </div>
    @endif

    <h5 id="dashboard-modules" class="fw-semibold mb-3" style="color: {{ config('theme.colors.text') }};">
        {{ __('dashboard.modules_heading') }}
    </h5>

    <div class="row g-3">
        @foreach ($moduleList as $module)
            <x-ui.modules.card
                :module="$module"
                :enabled="$module->enabled"
                :upgrade-url="$upgradeUrl"
            />
        @endforeach
    </div>
</x-ui.layout.page>
