{# app/templates/store/dashboard.html #}
{% extends "store/base.html" %}
{% from "shared/macros/feature_gate.html" import limit_warning, usage_bar, upgrade_card, tier_badge %}
{% from "shared/macros/alerts.html" import loading_state, error_state %}
{% from "shared/macros/tables.html" import table_wrapper, table_header %}

{% block title %}Dashboard{% endblock %}

{% block alpine_data %}storeDashboard(){% endblock %}

{% from "shared/macros/feature_gate.html" import email_settings_warning, onboarding_banner with context %}

{% block content %}
<!-- Email Settings Warning -->
{{ email_settings_warning() }}

<!-- Onboarding Banner -->
{{ onboarding_banner() }}

<!-- Limit Warnings -->
{{ limit_warning("orders") }}
{{ limit_warning("products") }}

<!-- Page Header with Refresh Button -->
{# noqa: FE-007 - Custom header with tier_badge alongside title #}
<div class="flex items-center justify-between my-6">
    <div class="flex items-center gap-3">
        <h2 class="text-2xl font-semibold text-gray-700 dark:text-gray-200">
            Dashboard
        </h2>
        {{ tier_badge() }}
    </div>
    <button
        @click="refresh()"
        :disabled="loading"
        class="flex items-center px-4 py-2 text-sm font-medium leading-5 text-white transition-colors duration-150 bg-purple-600 border border-transparent rounded-lg hover:bg-purple-700 focus:outline-none focus:shadow-outline-purple disabled:opacity-50 disabled:cursor-not-allowed"
    >
        <span x-show="!loading" x-html="$icon('refresh', 'w-4 h-4 mr-2')"></span>
        <span x-show="loading" x-html="$icon('spinner', 'w-4 h-4 mr-2')"></span>
        <span x-text="loading ? 'Loading...' : 'Refresh'"></span>
    </button>
</div>

<!-- Loading State -->
{{ loading_state('Loading dashboard...') }}

<!-- Error State -->
{{ error_state('Error loading dashboard') }}

<!-- Store Info Card -->
{% include 'store/partials/store_info.html' %}

<!-- Upgrade Recommendation Card (shows when approaching/at limits) -->
{{ upgrade_card(class='mb-6') }}

<!-- Stats Cards -->
<div x-show="!loading" class="grid gap-6 mb-8 md:grid-cols-2 xl:grid-cols-4">
    <!-- Card: Total Products -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-blue-500 bg-blue-100 rounded-full dark:text-blue-100 dark:bg-blue-500">
            <span x-html="$icon('shopping-bag', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                Total Products
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.products_count">
                0
            </p>
        </div>
    </div>

    <!-- Card: Total Orders -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-green-500 bg-green-100 rounded-full dark:text-green-100 dark:bg-green-500">
            <span x-html="$icon('shopping-cart', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                Total Orders
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.orders_count">
                0
            </p>
        </div>
    </div>

    <!-- Card: Total Customers -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-orange-500 bg-orange-100 rounded-full dark:text-orange-100 dark:bg-orange-500">
            <span x-html="$icon('users', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                Total Customers
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.customers_count">
                0
            </p>
        </div>
    </div>

    <!-- Card: Revenue -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-teal-500 bg-teal-100 rounded-full dark:text-teal-100 dark:bg-teal-500">
            <span x-html="$icon('currency-euro', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                Total Revenue
            </p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="formatCurrency(stats.revenue)">
                €0
            </p>
        </div>
    </div>
</div>

<!-- Usage Overview -->
<div x-show="!loading" class="grid gap-6 mb-8 md:grid-cols-3">
    <div class="p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        {{ usage_bar("orders", "Monthly Orders") }}
    </div>
    <div class="p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        {{ usage_bar("products", "Products") }}
    </div>
    <div class="p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        {{ usage_bar("team_members", "Team Members") }}
    </div>
</div>

<!-- Recent Orders Table -->
<div x-show="!loading && recentOrders.length > 0" class="mb-8">
    {% call table_wrapper() %}
        {{ table_header(['Order ID', 'Customer', 'Amount', 'Status', 'Date']) }}
        <tbody class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-800">
            <template x-for="order in recentOrders" :key="order.id">
                <tr class="text-gray-700 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors">
                    <td class="px-4 py-3">
                        <span class="text-xs" x-text="'#' + order.id"></span>
                    </td>
                    <td class="px-4 py-3 text-sm" x-text="order.customer_name">
                    </td>
                    <td class="px-4 py-3 text-sm font-semibold" x-text="formatCurrency(order.total_amount)">
                    </td>
                    <td class="px-4 py-3 text-xs">
                        <span class="px-2 py-1 font-semibold leading-tight rounded-full"
                              :class="{
                                  'text-green-700 bg-green-100 dark:bg-green-700 dark:text-green-100': order.status === 'completed',
                                  'text-orange-700 bg-orange-100 dark:text-white dark:bg-orange-600': order.status === 'pending',
                                  'text-blue-700 bg-blue-100 dark:bg-blue-700 dark:text-blue-100': order.status === 'processing'
                              }"
                              x-text="order.status"></span>
                    </td>
                    <td class="px-4 py-3 text-sm" x-text="formatDate(order.created_at)">
                    </td>
                </tr>
            </template>
        </tbody>
    {% endcall %}
</div>

<!-- Getting Started Section -->
<div x-show="!loading && recentOrders.length === 0" class="w-full mb-8 overflow-hidden rounded-lg shadow-xs">
    <div class="w-full p-8 bg-white dark:bg-gray-800 text-center">
        <div class="text-6xl mb-4">🚀</div>
        <h3 class="text-xl font-semibold text-gray-700 dark:text-gray-200 mb-2">
            Welcome to Your Store Dashboard!
        </h3>
        <p class="text-gray-600 dark:text-gray-400 mb-6">
            Start by importing products from the marketplace to build your catalog.
        </p>
        <a href="/store/{{ store_code }}/marketplace"
           class="inline-flex items-center px-4 py-2 text-sm font-medium leading-5 text-white transition-colors duration-150 bg-purple-600 border border-transparent rounded-lg hover:bg-purple-700 focus:outline-none focus:shadow-outline-purple">
            <span x-html="$icon('download', 'w-4 h-4 mr-2')"></span>
            Go to Marketplace Import
        </a>
    </div>
</div>
{% endblock %}

{% block extra_scripts %}
<script defer src="{{ url_for('core_static', path='store/js/dashboard.js') }}"></script>
{% endblock %}
