{# app/templates/admin/store-detail.html #}
{% extends "admin/base.html" %}
{% from 'shared/macros/alerts.html' import loading_state, error_state %}
{% from 'shared/macros/headers.html' import detail_page_header %}
{% from 'shared/macros/modals.html' import confirm_modal, confirm_modal_dynamic %}

{% block title %}Store Details{% endblock %}

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

{% block content %}
{% call detail_page_header("store?.name || 'Store Details'", '/admin/stores', subtitle_show='store') %}
    <span x-text="storeCode"></span>
    <span class="text-gray-400 mx-2">•</span>
    <span x-text="store?.subdomain"></span>
{% endcall %}

{{ loading_state('Loading store details...') }}

{{ error_state('Error loading store') }}

<!-- Store Details -->
<div x-show="!loading && store">
    <!-- Quick Actions Card -->
    <div class="px-4 py-3 mb-6 bg-white rounded-lg shadow-md dark:bg-gray-800">
        <h3 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
            Quick Actions
        </h3>
        <div class="flex flex-wrap items-center gap-3">
            <a
                :href="`/admin/stores/${storeCode}/edit`"
                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">
                <span x-html="$icon('edit', 'w-4 h-4 mr-2')"></span>
                Edit Store
            </a>
            <button
                @click="promptDeleteStore()"
                class="flex items-center px-4 py-2 text-sm font-medium leading-5 text-white transition-colors duration-150 bg-red-600 border border-transparent rounded-lg hover:bg-red-700 focus:outline-none focus:shadow-outline-red">
                <span x-html="$icon('delete', 'w-4 h-4 mr-2')"></span>
                Delete Store
            </button>
        </div>
    </div>

    <!-- Status Cards -->
    <div class="grid gap-6 mb-8 md:grid-cols-4">
        <!-- Verification Status -->
        <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
            <div class="p-3 mr-4 rounded-full"
                 :class="store?.is_verified ? 'text-green-500 bg-green-100 dark:text-green-100 dark:bg-green-500' : 'text-orange-500 bg-orange-100 dark:text-orange-100 dark:bg-orange-500'">
                <span x-html="$icon(store?.is_verified ? 'badge-check' : 'clock', 'w-5 h-5')"></span>
            </div>
            <div>
                <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                    Verification
                </p>
                <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="store?.is_verified ? 'Verified' : 'Pending'">
                    -
                </p>
            </div>
        </div>

        <!-- Active Status -->
        <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
            <div class="p-3 mr-4 rounded-full"
                 :class="store?.is_active ? 'text-green-500 bg-green-100 dark:text-green-100 dark:bg-green-500' : 'text-red-500 bg-red-100 dark:text-red-100 dark:bg-red-500'">
                <span x-html="$icon(store?.is_active ? 'check-circle' : 'x-circle', 'w-5 h-5')"></span>
            </div>
            <div>
                <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                    Status
                </p>
                <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="store?.is_active ? 'Active' : 'Inactive'">
                    -
                </p>
            </div>
        </div>

        <!-- Created Date -->
        <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('calendar', 'w-5 h-5')"></span>
            </div>
            <div>
                <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                    Created
                </p>
                <p class="text-sm font-semibold text-gray-700 dark:text-gray-200" x-text="formatDate(store?.created_at)">
                    -
                </p>
            </div>
        </div>

        <!-- Updated Date -->
        <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
            <div class="p-3 mr-4 text-purple-500 bg-purple-100 rounded-full dark:text-purple-100 dark:bg-purple-500">
                <span x-html="$icon('refresh', 'w-5 h-5')"></span>
            </div>
            <div>
                <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">
                    Last Updated
                </p>
                <p class="text-sm font-semibold text-gray-700 dark:text-gray-200" x-text="formatDate(store?.updated_at)">
                    -
                </p>
            </div>
        </div>
    </div>

    <!-- Subscription Cards -->
    <template x-for="entry in subscriptions" :key="entry.subscription.id">
        <div class="px-4 py-3 mb-6 bg-white rounded-lg shadow-md dark:bg-gray-800">
            <div class="flex items-center justify-between mb-4">
                <h3 class="text-lg font-semibold text-gray-700 dark:text-gray-200">
                    Subscription
                    <span x-show="entry.platform_name" class="text-sm font-normal text-gray-500 dark:text-gray-400">
                        — <span x-text="entry.platform_name"></span>
                    </span>
                </h3>
                <a
                    :href="'/admin/merchants/' + store?.merchant_id"
                    class="flex items-center px-3 py-1.5 text-sm font-medium text-purple-600 hover:text-purple-700 dark:text-purple-400 dark:hover:text-purple-300">
                    <span x-html="$icon('external-link', 'w-4 h-4 mr-1')"></span>
                    Manage on Merchant Page
                </a>
            </div>

            <!-- Tier and Status -->
            <div class="flex flex-wrap items-center gap-4 mb-4">
                <div class="flex items-center gap-2">
                    <span class="text-sm text-gray-600 dark:text-gray-400">Tier:</span>
                    <span class="px-2.5 py-0.5 text-sm font-medium rounded-full"
                          :class="{
                              'bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300': entry.subscription?.tier === 'essential',
                              'bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-300': entry.subscription?.tier === 'professional',
                              'bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-300': entry.subscription?.tier === 'business',
                              'bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-300': entry.subscription?.tier === 'enterprise'
                          }"
                          x-text="entry.subscription?.tier ? entry.subscription.tier.charAt(0).toUpperCase() + entry.subscription.tier.slice(1) : '-'">
                    </span>
                </div>
                <div class="flex items-center gap-2">
                    <span class="text-sm text-gray-600 dark:text-gray-400">Status:</span>
                    <span class="px-2.5 py-0.5 text-sm font-medium rounded-full"
                          :class="{
                              'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-300': entry.subscription?.status === 'active',
                              'bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-300': entry.subscription?.status === 'trial',
                              'bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-300': entry.subscription?.status === 'past_due',
                              'bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-300': entry.subscription?.status === 'cancelled' || entry.subscription?.status === 'expired'
                          }"
                          x-text="entry.subscription?.status ? entry.subscription.status.replace('_', ' ').charAt(0).toUpperCase() + entry.subscription.status.slice(1) : '-'">
                    </span>
                </div>
                <template x-if="entry.subscription?.is_annual">
                    <span class="px-2.5 py-0.5 text-xs font-medium text-purple-800 bg-purple-100 rounded-full dark:bg-purple-900 dark:text-purple-300">
                        Annual
                    </span>
                </template>
            </div>

            <!-- Period Info -->
            <div class="flex flex-wrap gap-4 mb-4 text-sm">
                <div>
                    <span class="text-gray-600 dark:text-gray-400">Period:</span>
                    <span class="ml-1 text-gray-700 dark:text-gray-300" x-text="formatDate(entry.subscription?.period_start)"></span>
                    <span class="text-gray-400">&rarr;</span>
                    <span class="text-gray-700 dark:text-gray-300" x-text="formatDate(entry.subscription?.period_end)"></span>
                </div>
                <template x-if="entry.subscription?.trial_ends_at">
                    <div>
                        <span class="text-gray-600 dark:text-gray-400">Trial ends:</span>
                        <span class="ml-1 text-gray-700 dark:text-gray-300" x-text="formatDate(entry.subscription?.trial_ends_at)"></span>
                    </div>
                </template>
            </div>

            <!-- Usage Meters -->
            <div class="grid gap-4 md:grid-cols-3">
                <template x-for="metric in entry.features" :key="metric.name">
                    <div class="p-3 bg-gray-50 rounded-lg dark:bg-gray-700">
                        <div class="flex items-center justify-between mb-2">
                            <span class="text-xs font-medium text-gray-600 dark:text-gray-400 uppercase" x-text="metric.name"></span>
                        </div>
                        <div class="flex items-baseline gap-1">
                            <span class="text-xl font-bold text-gray-700 dark:text-gray-200" x-text="metric.current"></span>
                            <span class="text-sm text-gray-500 dark:text-gray-400">
                                / <span x-text="metric.is_unlimited ? '∞' : metric.limit"></span>
                            </span>
                        </div>
                        <div class="mt-2 h-1.5 bg-gray-200 rounded-full dark:bg-gray-600" x-show="!metric.is_unlimited">
                            <div class="h-1.5 rounded-full transition-all"
                                 :class="getUsageBarColor(metric.current, metric.limit)"
                                 :style="`width: ${Math.min(100, metric.percentage || 0)}%`">
                            </div>
                        </div>
                    </div>
                </template>
                <template x-if="entry.features.length === 0">
                    <div class="p-3 bg-gray-50 rounded-lg dark:bg-gray-700 md:col-span-3">
                        <p class="text-sm text-gray-500 dark:text-gray-400 text-center">No usage data available</p>
                    </div>
                </template>
            </div>
        </div>
    </template>

    <!-- No Subscription Notice -->
    <div class="px-4 py-3 mb-6 bg-yellow-50 border border-yellow-200 rounded-lg dark:bg-yellow-900/20 dark:border-yellow-800" x-show="subscriptions.length === 0 && !loading">
        <div class="flex items-center gap-3">
            <span x-html="$icon('exclamation', 'w-5 h-5 text-yellow-600 dark:text-yellow-400')"></span>
            <div>
                <p class="text-sm font-medium text-yellow-800 dark:text-yellow-200">No Subscription Found</p>
                <p class="text-sm text-yellow-700 dark:text-yellow-300">This store doesn't have a subscription yet.</p>
            </div>
            <a
                :href="'/admin/merchants/' + store?.merchant_id"
                class="ml-auto px-3 py-1.5 text-sm font-medium text-white bg-yellow-600 rounded-lg hover:bg-yellow-700">
                Manage on Merchant Page
            </a>
        </div>
    </div>

    <!-- Main Info Cards -->
    <div class="grid gap-6 mb-8 md:grid-cols-2">
        <!-- Basic Information -->
        <div class="px-4 py-3 bg-white rounded-lg shadow-md dark:bg-gray-800">
            <h3 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
                Basic Information
            </h3>
            <div class="space-y-3">
                <div>
                    <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Store Code</p>
                    <p class="text-sm text-gray-700 dark:text-gray-300" x-text="store?.store_code || '-'">-</p>
                </div>
                <div>
                    <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Name</p>
                    <p class="text-sm text-gray-700 dark:text-gray-300" x-text="store?.name || '-'">-</p>
                </div>
                <div>
                    <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Subdomain</p>
                    <p class="text-sm text-gray-700 dark:text-gray-300" x-text="store?.subdomain || '-'">-</p>
                </div>
                <div>
                    <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Description</p>
                    <p class="text-sm text-gray-700 dark:text-gray-300" x-text="store?.description || 'No description provided'">-</p>
                </div>
            </div>
        </div>

        <!-- Contact Information -->
        <div class="px-4 py-3 bg-white rounded-lg shadow-md dark:bg-gray-800">
            <h3 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
                Contact Information
            </h3>
            <div class="space-y-3">
                <div>
                    <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Owner Email</p>
                    <p class="text-sm text-gray-700 dark:text-gray-300" x-text="store?.owner_email || '-'">-</p>
                    <p class="text-xs text-gray-500 dark:text-gray-400">Owner's authentication email</p>
                </div>
                <div>
                    <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Contact Email</p>
                    <p class="text-sm text-gray-700 dark:text-gray-300" x-text="store?.contact_email || '-'">-</p>
                    <p class="text-xs text-gray-500 dark:text-gray-400">Public business contact</p>
                </div>
                <div>
                    <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Phone</p>
                    <p class="text-sm text-gray-700 dark:text-gray-300" x-text="store?.contact_phone || '-'">-</p>
                </div>
                <div>
                    <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Website</p>
                    <a
                        x-show="store?.website"
                        :href="store?.website"
                        target="_blank"
                        class="text-sm text-purple-600 hover:text-purple-700 dark:text-purple-400"
                        x-text="store?.website">
                    </a>
                    <span x-show="!store?.website" class="text-sm text-gray-700 dark:text-gray-300">-</span>
                </div>
            </div>
        </div>
    </div>

    <!-- Business Details -->
    <div class="px-4 py-3 mb-8 bg-white rounded-lg shadow-md dark:bg-gray-800">
        <h3 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
            Business Details
        </h3>
        <div class="grid gap-6 md:grid-cols-2">
            <div>
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase mb-2">Business Address</p>
                <p class="text-sm text-gray-700 dark:text-gray-300 whitespace-pre-line" x-text="store?.business_address || 'No address provided'">-</p>
            </div>
            <div>
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase mb-2">Tax Number</p>
                <p class="text-sm text-gray-700 dark:text-gray-300" x-text="store?.tax_number || 'Not provided'">-</p>
            </div>
        </div>
    </div>

    <!-- Owner Information -->
    <div class="px-4 py-3 mb-8 bg-white rounded-lg shadow-md dark:bg-gray-800">
        <h3 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
            Owner Information
        </h3>
        <div class="grid gap-6 md:grid-cols-3">
            <div>
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase mb-2">Owner User ID</p>
                <p class="text-sm text-gray-700 dark:text-gray-300" x-text="store?.owner_user_id || '-'">-</p>
            </div>
            <div>
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase mb-2">Owner Username</p>
                <p class="text-sm text-gray-700 dark:text-gray-300" x-text="store?.owner_username || '-'">-</p>
            </div>
            <div>
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase mb-2">Owner Email</p>
                <p class="text-sm text-gray-700 dark:text-gray-300" x-text="store?.owner_email || '-'">-</p>
            </div>
        </div>
    </div>

    <!-- Marketplace URLs -->
    <div class="px-4 py-3 mb-8 bg-white rounded-lg shadow-md dark:bg-gray-800" x-show="store?.letzshop_csv_url_fr || store?.letzshop_csv_url_en || store?.letzshop_csv_url_de">
        <h3 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
            Marketplace CSV URLs
        </h3>
        <div class="space-y-3">
            <div x-show="store?.letzshop_csv_url_fr">
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase mb-1">French (FR)</p>
                <a
                    :href="store?.letzshop_csv_url_fr"
                    target="_blank"
                    class="text-sm text-purple-600 hover:text-purple-700 dark:text-purple-400 break-all"
                    x-text="store?.letzshop_csv_url_fr">
                </a>
            </div>
            <div x-show="store?.letzshop_csv_url_en">
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase mb-1">English (EN)</p>
                <a
                    :href="store?.letzshop_csv_url_en"
                    target="_blank"
                    class="text-sm text-purple-600 hover:text-purple-700 dark:text-purple-400 break-all"
                    x-text="store?.letzshop_csv_url_en">
                </a>
            </div>
            <div x-show="store?.letzshop_csv_url_de">
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase mb-1">German (DE)</p>
                <a
                    :href="store?.letzshop_csv_url_de"
                    target="_blank"
                    class="text-sm text-purple-600 hover:text-purple-700 dark:text-purple-400 break-all"
                    x-text="store?.letzshop_csv_url_de">
                </a>
            </div>
        </div>
    </div>

    <!-- Custom Domains -->
    <div class="px-4 py-3 mb-8 bg-white rounded-lg shadow-md dark:bg-gray-800">
        <div class="flex items-center justify-between mb-4">
            <h3 class="text-lg font-semibold text-gray-700 dark:text-gray-200">
                Custom Domains
            </h3>
            <button
                @click="showAddDomainForm = !showAddDomainForm"
                class="flex items-center px-3 py-1.5 text-sm font-medium text-white bg-purple-600 rounded-lg hover:bg-purple-700">
                <span x-html="$icon('plus', 'w-4 h-4 mr-1')"></span>
                Add Domain
            </button>
        </div>

        <!-- Add Domain Form -->
        <div x-show="showAddDomainForm" x-transition class="mb-4 p-4 bg-gray-50 rounded-lg dark:bg-gray-700">
            <div class="grid gap-4 md:grid-cols-2">
                <div>
                    <label class="block text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase mb-1">Domain</label>
                    <input
                        type="text"
                        x-model="newDomain.domain"
                        placeholder="shop.example.com"
                        class="w-full px-3 py-2 text-sm border rounded-lg dark:bg-gray-800 dark:border-gray-600 dark:text-gray-300 focus:outline-none focus:ring-2 focus:ring-purple-500" />
                </div>
                <div>
                    <label class="block text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase mb-1">Platform</label>
                    <select
                        x-model="newDomain.platform_id"
                        class="w-full px-3 py-2 text-sm border rounded-lg dark:bg-gray-800 dark:border-gray-600 dark:text-gray-300 focus:outline-none focus:ring-2 focus:ring-purple-500">
                        <option value="">Select platform...</option>
                        <template x-for="entry in subscriptions" :key="entry.subscription?.id">
                            <option :value="entry.platform_id" x-text="entry.platform_name"></option>
                        </template>
                    </select>
                </div>
            </div>
            <div class="flex justify-end gap-2 mt-3">
                <button
                    @click="showAddDomainForm = false; newDomain = {domain: '', platform_id: ''}"
                    class="px-3 py-1.5 text-sm font-medium text-gray-600 bg-gray-200 rounded-lg hover:bg-gray-300 dark:bg-gray-600 dark:text-gray-300 dark:hover:bg-gray-500">
                    Cancel
                </button>
                <button
                    @click="addDomain()"
                    :disabled="!newDomain.domain || domainSaving"
                    class="px-3 py-1.5 text-sm font-medium text-white bg-purple-600 rounded-lg hover:bg-purple-700 disabled:opacity-50 disabled:cursor-not-allowed">
                    <span x-show="!domainSaving">Add Domain</span>
                    <span x-show="domainSaving">Adding...</span>
                </button>
            </div>
        </div>

        <!-- Domains Loading -->
        <div x-show="domainsLoading" class="text-center py-4">
            <p class="text-sm text-gray-500 dark:text-gray-400">Loading domains...</p>
        </div>

        <!-- Domains List -->
        <div x-show="!domainsLoading && domains.length > 0" class="space-y-3">
            <template x-for="domain in domains" :key="domain.id">
                <div class="p-3 bg-gray-50 rounded-lg dark:bg-gray-700 flex items-center justify-between">
                    <div class="flex-1 min-w-0">
                        <div class="flex items-center gap-2 mb-1">
                            <span class="text-sm font-medium text-gray-700 dark:text-gray-200 truncate" x-text="domain.domain"></span>
                            <span x-show="domain.is_primary"
                                  class="px-1.5 py-0.5 text-xs font-medium text-purple-800 bg-purple-100 rounded-full dark:bg-purple-900 dark:text-purple-300">
                                Primary
                            </span>
                            <span :class="domain.is_verified
                                    ? 'text-green-800 bg-green-100 dark:bg-green-900 dark:text-green-300'
                                    : 'text-yellow-800 bg-yellow-100 dark:bg-yellow-900 dark:text-yellow-300'"
                                  class="px-1.5 py-0.5 text-xs font-medium rounded-full"
                                  x-text="domain.is_verified ? 'Verified' : 'Unverified'">
                            </span>
                            <span :class="domain.is_active
                                    ? 'text-green-800 bg-green-100 dark:bg-green-900 dark:text-green-300'
                                    : 'text-red-800 bg-red-100 dark:bg-red-900 dark:text-red-300'"
                                  class="px-1.5 py-0.5 text-xs font-medium rounded-full"
                                  x-text="domain.is_active ? 'Active' : 'Inactive'">
                            </span>
                            <span x-show="domain.ssl_status === 'active'"
                                  class="px-1.5 py-0.5 text-xs font-medium text-green-800 bg-green-100 rounded-full dark:bg-green-900 dark:text-green-300">
                                SSL
                            </span>
                        </div>
                        <!-- Verification instructions (shown when unverified) -->
                        <div x-show="!domain.is_verified && domain.verification_token" class="mt-2 text-xs text-gray-500 dark:text-gray-400">
                            Add TXT record: <code class="px-1 py-0.5 bg-gray-200 dark:bg-gray-600 rounded text-xs">_orion-verify.<span x-text="domain.domain"></span></code>
                            with value <code class="px-1 py-0.5 bg-gray-200 dark:bg-gray-600 rounded text-xs" x-text="domain.verification_token"></code>
                        </div>
                    </div>
                    <div class="flex items-center gap-1 ml-3 flex-shrink-0">
                        <!-- Verify Button -->
                        <button
                            x-show="!domain.is_verified"
                            @click="verifyDomain(domain.id)"
                            class="px-2 py-1 text-xs font-medium text-blue-600 hover:text-blue-700 dark:text-blue-400"
                            title="Verify DNS">
                            <span x-html="$icon('badge-check', 'w-4 h-4')"></span>
                        </button>
                        <!-- Toggle Active -->
                        <button
                            x-show="domain.is_verified"
                            @click="toggleDomainActive(domain.id, !domain.is_active)"
                            :class="domain.is_active ? 'text-yellow-600 hover:text-yellow-700' : 'text-green-600 hover:text-green-700'"
                            class="px-2 py-1 text-xs font-medium"
                            :title="domain.is_active ? 'Deactivate' : 'Activate'">
                            <span x-html="$icon(domain.is_active ? 'eye-off' : 'eye', 'w-4 h-4')"></span>
                        </button>
                        <!-- Set Primary -->
                        <button
                            x-show="domain.is_verified && !domain.is_primary"
                            @click="setDomainPrimary(domain.id)"
                            class="px-2 py-1 text-xs font-medium text-purple-600 hover:text-purple-700 dark:text-purple-400"
                            title="Set as primary">
                            <span x-html="$icon('star', 'w-4 h-4')"></span>
                        </button>
                        <!-- Delete -->
                        <button
                            @click="deleteDomain(domain.id, domain.domain)"
                            class="px-2 py-1 text-xs font-medium text-red-600 hover:text-red-700 dark:text-red-400"
                            title="Delete domain">
                            <span x-html="$icon('delete', 'w-4 h-4')"></span>
                        </button>
                    </div>
                </div>
            </template>
        </div>

        <!-- No Domains -->
        <div x-show="!domainsLoading && domains.length === 0" class="text-center py-4">
            <p class="text-sm text-gray-500 dark:text-gray-400">No custom domains configured.</p>
        </div>
    </div>

    <!-- Custom Subdomains (per-platform) -->
    <div class="px-4 py-3 mb-8 bg-white rounded-lg shadow-md dark:bg-gray-800">
        <div class="flex items-center justify-between mb-4">
            <h3 class="text-lg font-semibold text-gray-700 dark:text-gray-200">
                Custom Subdomains
            </h3>
            <button
                @click="loadCustomSubdomains()"
                class="flex items-center px-3 py-1.5 text-sm font-medium text-gray-600 bg-gray-200 rounded-lg hover:bg-gray-300 dark:bg-gray-600 dark:text-gray-300 dark:hover:bg-gray-500">
                <span x-html="$icon('refresh', 'w-4 h-4 mr-1')"></span>
                Refresh
            </button>
        </div>

        <p class="text-xs text-gray-500 dark:text-gray-400 mb-4">
            Each platform membership can have a custom subdomain override. If not set, the store's default subdomain
            (<strong x-text="store?.subdomain"></strong>) is used on each platform.
        </p>

        <!-- Loading -->
        <div x-show="customSubdomainsLoading" class="text-center py-4">
            <p class="text-sm text-gray-500 dark:text-gray-400">Loading subdomains...</p>
        </div>

        <!-- Subdomains List -->
        <div x-show="!customSubdomainsLoading && customSubdomains.length > 0" class="space-y-3">
            <template x-for="entry in customSubdomains" :key="entry.store_platform_id">
                <div class="p-3 bg-gray-50 rounded-lg dark:bg-gray-700">
                    <div class="flex items-center justify-between mb-2">
                        <div class="flex items-center gap-2">
                            <span x-html="$icon('globe', 'w-4 h-4 text-purple-500')"></span>
                            <span class="text-sm font-medium text-gray-700 dark:text-gray-200" x-text="entry.platform_name"></span>
                            <span class="px-1.5 py-0.5 text-xs font-medium text-gray-600 bg-gray-200 rounded-full dark:bg-gray-600 dark:text-gray-300" x-text="entry.platform_code"></span>
                        </div>
                        <div class="flex items-center gap-1">
                            <!-- Edit button (show when not editing) -->
                            <button
                                x-show="editingSubdomainPlatformId !== entry.platform_id"
                                @click="startEditSubdomain(entry)"
                                class="px-2 py-1 text-xs font-medium text-blue-600 hover:text-blue-700 dark:text-blue-400"
                                title="Edit subdomain">
                                <span x-html="$icon('edit', 'w-4 h-4')"></span>
                            </button>
                            <!-- Clear button (show when custom subdomain is set and not editing) -->
                            <button
                                x-show="entry.custom_subdomain && editingSubdomainPlatformId !== entry.platform_id"
                                @click="clearCustomSubdomain(entry.platform_id, entry.custom_subdomain)"
                                class="px-2 py-1 text-xs font-medium text-red-600 hover:text-red-700 dark:text-red-400"
                                title="Clear custom subdomain">
                                <span x-html="$icon('delete', 'w-4 h-4')"></span>
                            </button>
                        </div>
                    </div>

                    <!-- Display mode -->
                    <div x-show="editingSubdomainPlatformId !== entry.platform_id" class="text-sm">
                        <div class="flex items-center gap-2">
                            <span class="text-gray-500 dark:text-gray-400">Custom:</span>
                            <template x-if="entry.custom_subdomain">
                                <span>
                                    <span class="font-medium text-purple-600 dark:text-purple-400" x-text="entry.custom_subdomain"></span>
                                    <span class="text-gray-400" x-text="'.' + entry.platform_domain" x-show="entry.platform_domain"></span>
                                </span>
                            </template>
                            <template x-if="!entry.custom_subdomain">
                                <span class="text-gray-400 italic">Not set</span>
                            </template>
                        </div>
                        <div class="flex items-center gap-2 mt-1">
                            <span class="text-gray-500 dark:text-gray-400">Default:</span>
                            <span class="text-gray-600 dark:text-gray-300" x-text="entry.default_url || '--'"></span>
                        </div>
                    </div>

                    <!-- Edit mode -->
                    <div x-show="editingSubdomainPlatformId === entry.platform_id" x-transition class="mt-2">
                        <div class="flex items-center gap-2">
                            <input
                                type="text"
                                x-model="editingSubdomainValue"
                                :placeholder="entry.default_subdomain || 'custom-subdomain'"
                                @keydown.enter="saveCustomSubdomain(entry.platform_id)"
                                @keydown.escape="cancelEditSubdomain()"
                                class="flex-1 px-3 py-1.5 text-sm border rounded-lg dark:bg-gray-800 dark:border-gray-600 dark:text-gray-300 focus:outline-none focus:ring-2 focus:ring-purple-500" />
                            <span class="text-sm text-gray-400" x-text="'.' + entry.platform_domain" x-show="entry.platform_domain"></span>
                        </div>
                        <div class="flex justify-end gap-2 mt-2">
                            <button
                                @click="cancelEditSubdomain()"
                                class="px-3 py-1 text-xs font-medium text-gray-600 bg-gray-200 rounded-lg hover:bg-gray-300 dark:bg-gray-600 dark:text-gray-300 dark:hover:bg-gray-500">
                                Cancel
                            </button>
                            <button
                                @click="saveCustomSubdomain(entry.platform_id)"
                                :disabled="!editingSubdomainValue || subdomainSaving"
                                class="px-3 py-1 text-xs font-medium text-white bg-purple-600 rounded-lg hover:bg-purple-700 disabled:opacity-50 disabled:cursor-not-allowed">
                                <span x-show="!subdomainSaving">Save</span>
                                <span x-show="subdomainSaving">Saving...</span>
                            </button>
                        </div>
                    </div>
                </div>
            </template>
        </div>

        <!-- No Platform Memberships -->
        <div x-show="!customSubdomainsLoading && customSubdomains.length === 0" class="text-center py-4">
            <p class="text-sm text-gray-500 dark:text-gray-400">No active platform memberships found.</p>
        </div>
    </div>

    <!-- More Actions -->
    <div class="px-4 py-3 bg-white rounded-lg shadow-md dark:bg-gray-800">
        <h3 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
            More Actions
        </h3>
        <div class="flex flex-wrap gap-3">
            <!-- View Parent Merchant -->
            <a
                :href="'/admin/merchants/' + store?.merchant_id + '/edit'"
                class="inline-flex items-center px-4 py-2 text-sm font-medium text-white transition-colors duration-150 bg-blue-600 border border-transparent rounded-lg hover:bg-blue-700 focus:outline-none focus:shadow-outline-blue"
            >
                <span x-html="$icon('office-building', 'w-4 h-4 mr-2')"></span>
                View Parent Merchant
            </a>

            <!-- Customize Theme -->
            <a
                :href="`/admin/stores/${storeCode}/theme`"
                class="inline-flex items-center px-4 py-2 text-sm font-medium 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('color-swatch', 'w-4 h-4 mr-2')"></span>
                Customize Theme
            </a>
        </div>
        <p class="mt-3 text-xs text-gray-500 dark:text-gray-400">
            <span x-html="$icon('information-circle', 'w-4 h-4 inline mr-1')"></span>
            This store belongs to merchant: <strong x-text="store?.merchant_name"></strong>.
            Contact info and ownership are managed at the merchant level.
        </p>
    </div>

    <!-- Delete Store Confirmation Modal (Step 1) -->
    {{ confirm_modal_dynamic(
        'deleteStoreModal',
        'Delete Store',
        "'Are you sure you want to delete \"' + (store?.name || '') + '\"? This will permanently delete all products, orders, customers, and team members. This action cannot be undone!'",
        'confirmDeleteStoreStep()',
        'showDeleteStoreModal',
        'Delete',
        'Cancel',
        'danger'
    ) }}

    <!-- Delete Store Final Confirmation Modal (Step 2) -->
    {{ confirm_modal_dynamic(
        'deleteStoreFinalModal',
        'Final Confirmation',
        "'FINAL CONFIRMATION: Are you absolutely sure you want to permanently delete \"' + (store?.name || '') + '\" and ALL associated data?'",
        'deleteStore()',
        'showDeleteStoreFinalModal',
        'Permanently Delete',
        'Cancel',
        'danger'
    ) }}
</div>
{% endblock %}

{% block extra_scripts %}
<script defer src="{{ url_for('tenancy_static', path='admin/js/store-detail.js') }}"></script>
{% endblock %}
