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

{% block title %}Edit Store{% endblock %}

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

{% block content %}
{% call edit_page_header('Edit Store', '/admin/stores', subtitle_show='store', back_label='Back to Stores') %}
    <span x-text="store?.name"></span>
    <span class="text-gray-400">•</span>
    <span x-text="store?.store_code"></span>
{% endcall %}

{{ loading_state('Loading store...', show_condition='loadingStore') }}

<!-- Edit Form -->
<div x-show="!loadingStore && 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">
            <button
                @click="showToggleVerificationModal = true"
                :disabled="saving"
                class="flex items-center px-4 py-2 text-sm font-medium leading-5 text-white transition-colors duration-150 rounded-lg focus:outline-none focus:shadow-outline-purple disabled:opacity-50"
                :class="store?.is_verified ? 'bg-orange-600 hover:bg-orange-700' : 'bg-green-600 hover:bg-green-700'">
                <span x-html="$icon(store?.is_verified ? 'x-circle' : 'badge-check', 'w-4 h-4 mr-2')"></span>
                <span x-text="store?.is_verified ? 'Unverify Store' : 'Verify Store'"></span>
            </button>

            <button
                @click="showToggleActiveModal = true"
                :disabled="saving"
                class="flex items-center px-4 py-2 text-sm font-medium leading-5 text-white transition-colors duration-150 rounded-lg focus:outline-none focus:shadow-outline-purple disabled:opacity-50"
                :class="store?.is_active ? 'bg-red-600 hover:bg-red-700' : 'bg-green-600 hover:bg-green-700'">
                <span x-html="$icon(store?.is_active ? 'lock-closed' : 'lock-open', 'w-4 h-4 mr-2')"></span>
                <span x-text="store?.is_active ? 'Deactivate' : 'Activate'"></span>
            </button>

            <!-- Status Badges -->
            <div class="ml-auto flex items-center gap-2">
                <span
                    x-show="store?.is_verified"
                    class="inline-flex items-center px-3 py-1 text-xs font-semibold leading-tight text-green-700 bg-green-100 rounded-full dark:bg-green-700 dark:text-green-100">
                    <span x-html="$icon('badge-check', 'w-3 h-3 mr-1')"></span>
                    Verified
                </span>
                <span
                    x-show="!store?.is_verified"
                    class="inline-flex items-center px-3 py-1 text-xs font-semibold leading-tight text-orange-700 bg-orange-100 rounded-full dark:bg-orange-700 dark:text-orange-100">
                    <span x-html="$icon('clock', 'w-3 h-3 mr-1')"></span>
                    Pending
                </span>
                <span
                    x-show="store?.is_active"
                    class="inline-flex items-center px-3 py-1 text-xs font-semibold leading-tight text-green-700 bg-green-100 rounded-full dark:bg-green-700 dark:text-green-100">
                    Active
                </span>
                <span
                    x-show="!store?.is_active"
                    class="inline-flex items-center px-3 py-1 text-xs font-semibold leading-tight text-red-700 bg-red-100 rounded-full dark:bg-red-700 dark:text-red-100">
                    Inactive
                </span>
            </div>
        </div>
    </div>

    <!-- Form Card -->
    <form @submit.prevent="handleSubmit" class="px-4 py-3 mb-8 bg-white rounded-lg shadow-md dark:bg-gray-800">
        <div class="grid gap-6 mb-8 md:grid-cols-2">
            <!-- Left Column: Basic Info -->
            <div>
                <h3 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
                    Basic Information
                </h3>

                <!-- Store Code (readonly) -->
                <label class="block mb-4 text-sm">
                    <span class="text-gray-700 dark:text-gray-400">
                        Store Code
                    </span>
                    <input
                        type="text"
                        :value="store?.store_code || ''"
                        disabled
                        class="block w-full mt-1 text-sm bg-gray-100 border-gray-300 rounded-md dark:bg-gray-700 dark:text-gray-400 dark:border-gray-600 cursor-not-allowed"
                    >
                    <span class="text-xs text-gray-600 dark:text-gray-400 mt-1">
                        Cannot be changed after creation
                    </span>
                </label>

                <!-- Name -->
                <label class="block mb-4 text-sm">
                    <span class="text-gray-700 dark:text-gray-400">
                        Store Name <span class="text-red-600">*</span>
                    </span>
                    <input
                        type="text"
                        x-model="formData.name"
                        required
                        maxlength="255"
                        :disabled="saving"
                        class="block w-full mt-1 text-sm dark:text-gray-300 dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray form-input"
                        :class="{ 'border-red-600 focus:border-red-400 focus:shadow-outline-red': errors.name }"
                    >
                    <span x-show="errors.name" class="text-xs text-red-600 dark:text-red-400 mt-1" x-text="errors.name"></span>
                </label>

                <!-- Subdomain -->
                <label class="block mb-4 text-sm">
                    <span class="text-gray-700 dark:text-gray-400">
                        Subdomain <span class="text-red-600">*</span>
                    </span>
                    <input
                        type="text"
                        x-model="formData.subdomain"
                        @input="formatSubdomain()"
                        required
                        maxlength="100"
                        :disabled="saving"
                        class="block w-full mt-1 text-sm dark:text-gray-300 dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray form-input"
                        :class="{ 'border-red-600 focus:border-red-400 focus:shadow-outline-red': errors.subdomain }"
                    >
                    <span class="text-xs text-gray-600 dark:text-gray-400 mt-1">
                        Lowercase letters, numbers, and hyphens only
                    </span>
                    <span x-show="errors.subdomain" class="text-xs text-red-600 dark:text-red-400 mt-1" x-text="errors.subdomain"></span>
                </label>

                <!-- Description -->
                <label class="block mb-4 text-sm">
                    <span class="text-gray-700 dark:text-gray-400">
                        Description
                    </span>
                    <textarea
                        x-model="formData.description"
                        rows="3"
                        :disabled="saving"
                        class="block w-full mt-1 text-sm dark:text-gray-300 dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray form-textarea"
                    ></textarea>
                </label>
            </div>

            <!-- Right Column: Contact Info -->
            <div>
                <div class="flex items-center justify-between mb-4">
                    <h3 class="text-lg font-semibold text-gray-700 dark:text-gray-200">
                        Contact Information
                    </h3>
                    <button
                        type="button"
                        @click="resetAllContactToMerchant()"
                        :disabled="saving || !hasAnyContactOverride()"
                        class="text-xs px-2 py-1 text-purple-600 dark:text-purple-400 hover:text-purple-800 dark:hover:text-purple-300 disabled:opacity-50 disabled:cursor-not-allowed"
                        title="Reset all contact fields to inherit from merchant">
                        <span x-html="$icon('refresh', 'w-3 h-3 inline mr-1')"></span>
                        Reset All to Merchant
                    </button>
                </div>

                <!-- Owner Email (readonly) -->
                <label class="block mb-4 text-sm">
                    <span class="text-gray-700 dark:text-gray-400">
                        Owner Email
                    </span>
                    <input
                        type="email"
                        :value="store?.owner_email || ''"
                        disabled
                        class="block w-full mt-1 text-sm bg-gray-100 border-gray-300 rounded-md dark:bg-gray-700 dark:text-gray-400 dark:border-gray-600 cursor-not-allowed"
                    >
                    <span class="text-xs text-gray-600 dark:text-gray-400 mt-1">
                        Owner's authentication email
                    </span>
                </label>

                <!-- Contact Email -->
                <label class="block mb-4 text-sm">
                    <div class="flex items-center justify-between">
                        <span class="text-gray-700 dark:text-gray-400">
                            Contact Email
                            <span x-show="!formData.contact_email"
                                  class="ml-1 text-xs text-purple-500 dark:text-purple-400"
                                  title="Inherited from merchant">
                                (from merchant)
                            </span>
                        </span>
                        <button
                            type="button"
                            x-show="formData.contact_email"
                            @click="resetFieldToMerchant('contact_email')"
                            :disabled="saving"
                            class="text-xs text-purple-600 hover:text-purple-800 dark:text-purple-400">
                            Reset
                        </button>
                    </div>
                    <input
                        type="email"
                        x-model="formData.contact_email"
                        :placeholder="store?.merchant_contact_email || 'contact@merchant.com'"
                        :disabled="saving"
                        class="block w-full mt-1 text-sm dark:text-gray-300 dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray form-input"
                        :class="{ 'border-red-600 focus:border-red-400 focus:shadow-outline-red': errors.contact_email }"
                    >
                    <span class="text-xs text-gray-600 dark:text-gray-400 mt-1">
                        <span x-show="!formData.contact_email">Using merchant value. Enter a value to override.</span>
                        <span x-show="formData.contact_email">Custom value (clear to inherit from merchant)</span>
                    </span>
                    <span x-show="errors.contact_email" class="text-xs text-red-600 dark:text-red-400 mt-1" x-text="errors.contact_email"></span>
                </label>

                <!-- Phone -->
                <label class="block mb-4 text-sm">
                    <div class="flex items-center justify-between">
                        <span class="text-gray-700 dark:text-gray-400">
                            Phone
                            <span x-show="!formData.contact_phone"
                                  class="ml-1 text-xs text-purple-500 dark:text-purple-400">
                                (from merchant)
                            </span>
                        </span>
                        <button
                            type="button"
                            x-show="formData.contact_phone"
                            @click="resetFieldToMerchant('contact_phone')"
                            :disabled="saving"
                            class="text-xs text-purple-600 hover:text-purple-800 dark:text-purple-400">
                            Reset
                        </button>
                    </div>
                    <input
                        type="tel"
                        x-model="formData.contact_phone"
                        :placeholder="store?.merchant_contact_phone || '+352 XXX XXX'"
                        :disabled="saving"
                        class="block w-full mt-1 text-sm dark:text-gray-300 dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray form-input"
                    >
                </label>

                <!-- Website -->
                <label class="block mb-4 text-sm">
                    <div class="flex items-center justify-between">
                        <span class="text-gray-700 dark:text-gray-400">
                            Website
                            <span x-show="!formData.website"
                                  class="ml-1 text-xs text-purple-500 dark:text-purple-400">
                                (from merchant)
                            </span>
                        </span>
                        <button
                            type="button"
                            x-show="formData.website"
                            @click="resetFieldToMerchant('website')"
                            :disabled="saving"
                            class="text-xs text-purple-600 hover:text-purple-800 dark:text-purple-400">
                            Reset
                        </button>
                    </div>
                    <input
                        type="url"
                        x-model="formData.website"
                        :placeholder="store?.merchant_website || 'https://merchant.com'"
                        :disabled="saving"
                        class="block w-full mt-1 text-sm dark:text-gray-300 dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray form-input"
                    >
                </label>
            </div>
        </div>

        <!-- Business Details -->
        <div class="mb-8">
            <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">
                <!-- Business Address -->
                <label class="block text-sm">
                    <div class="flex items-center justify-between">
                        <span class="text-gray-700 dark:text-gray-400">
                            Business Address
                            <span x-show="!formData.business_address"
                                  class="ml-1 text-xs text-purple-500 dark:text-purple-400">
                                (from merchant)
                            </span>
                        </span>
                        <button
                            type="button"
                            x-show="formData.business_address"
                            @click="resetFieldToMerchant('business_address')"
                            :disabled="saving"
                            class="text-xs text-purple-600 hover:text-purple-800 dark:text-purple-400">
                            Reset
                        </button>
                    </div>
                    <textarea
                        x-model="formData.business_address"
                        rows="3"
                        :disabled="saving"
                        :placeholder="store?.merchant_business_address || 'No merchant address'"
                        class="block w-full mt-1 text-sm dark:text-gray-300 dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray form-textarea"
                    ></textarea>
                </label>

                <!-- Tax Number -->
                <label class="block text-sm">
                    <div class="flex items-center justify-between">
                        <span class="text-gray-700 dark:text-gray-400">
                            Tax Number
                            <span x-show="!formData.tax_number"
                                  class="ml-1 text-xs text-purple-500 dark:text-purple-400">
                                (from merchant)
                            </span>
                        </span>
                        <button
                            type="button"
                            x-show="formData.tax_number"
                            @click="resetFieldToMerchant('tax_number')"
                            :disabled="saving"
                            class="text-xs text-purple-600 hover:text-purple-800 dark:text-purple-400">
                            Reset
                        </button>
                    </div>
                    <input
                        type="text"
                        x-model="formData.tax_number"
                        :disabled="saving"
                        :placeholder="store?.merchant_tax_number || 'No merchant tax number'"
                        class="block w-full mt-1 text-sm dark:text-gray-300 dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray form-input"
                    >
                </label>
            </div>
        </div>

        <!-- Marketplace Integration -->
        <div class="mb-8">
            <h3 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
                <span x-html="$icon('shopping-bag', 'inline w-5 h-5 mr-2')"></span>
                Letzshop Marketplace URLs
            </h3>
            <p class="mb-4 text-sm text-gray-600 dark:text-gray-400">
                Configure CSV feed URLs for automatic product imports from Letzshop marketplace
            </p>

            <div class="space-y-4">
                <!-- French CSV URL -->
                <label class="block text-sm">
                    <span class="text-gray-700 dark:text-gray-400">
                        French CSV URL
                    </span>
                    <input
                        type="url"
                        x-model="formData.letzshop_csv_url_fr"
                        :disabled="saving"
                        placeholder="https://letzshop.lu/feed/fr/products.csv"
                        class="block w-full mt-1 text-sm dark:text-gray-300 dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray form-input"
                    >
                    <span class="text-xs text-gray-600 dark:text-gray-400 mt-1">
                        URL for French language product feed
                    </span>
                </label>

                <!-- English CSV URL -->
                <label class="block text-sm">
                    <span class="text-gray-700 dark:text-gray-400">
                        English CSV URL
                    </span>
                    <input
                        type="url"
                        x-model="formData.letzshop_csv_url_en"
                        :disabled="saving"
                        placeholder="https://letzshop.lu/feed/en/products.csv"
                        class="block w-full mt-1 text-sm dark:text-gray-300 dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray form-input"
                    >
                    <span class="text-xs text-gray-600 dark:text-gray-400 mt-1">
                        URL for English language product feed
                    </span>
                </label>

                <!-- German CSV URL -->
                <label class="block text-sm">
                    <span class="text-gray-700 dark:text-gray-400">
                        German CSV URL
                    </span>
                    <input
                        type="url"
                        x-model="formData.letzshop_csv_url_de"
                        :disabled="saving"
                        placeholder="https://letzshop.lu/feed/de/products.csv"
                        class="block w-full mt-1 text-sm dark:text-gray-300 dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray form-input"
                    >
                    <span class="text-xs text-gray-600 dark:text-gray-400 mt-1">
                        URL for German language product feed
                    </span>
                </label>
            </div>
        </div>

        <!-- Save Button -->
        <div class="flex items-center justify-end gap-3 pt-6 border-t dark:border-gray-700">
            <a
                href="/admin/stores"
                class="px-4 py-2 text-sm font-medium leading-5 text-gray-700 transition-colors duration-150 bg-white border border-gray-300 rounded-lg dark:text-gray-400 dark:border-gray-600 dark:bg-gray-800 hover:border-gray-400 focus:outline-none">
                Cancel
            </a>
            <button
                type="submit"
                :disabled="saving"
                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="!saving">
                    <span x-html="$icon('check', 'w-4 h-4 mr-2 inline')"></span>
                    Save Changes
                </span>
                <span x-show="saving" class="flex items-center">
                    <span x-html="$icon('spinner', 'w-4 h-4 mr-2')"></span>
                    Saving...
                </span>
            </button>
        </div>
    </form>
</div>

<!-- Toggle Verification Confirmation Modal -->
{{ confirm_modal_dynamic(
    'toggleVerificationModal',
    'Toggle Store Verification',
    "store?.is_verified ? 'Are you sure you want to unverify this store?' : 'Are you sure you want to verify this store?'",
    'toggleVerification()',
    'showToggleVerificationModal',
    'Confirm',
    'Cancel',
    'warning'
) }}

<!-- Toggle Active Status Confirmation Modal -->
{{ confirm_modal_dynamic(
    'toggleActiveModal',
    'Toggle Store Status',
    "store?.is_active ? 'Are you sure you want to deactivate this store? This will affect their operations.' : 'Are you sure you want to activate this store?'",
    'toggleActive()',
    'showToggleActiveModal',
    'Confirm',
    'Cancel',
    'warning'
) }}

<!-- 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'
) }}
{% endblock %}

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