{# app/templates/admin/marketplace-product-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 modal_simple %}

{% block title %}Marketplace Product Details{% endblock %}

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

{% block content %}
{% call detail_page_header("product?.title || 'Product Details'", back_url, subtitle_show='product') %}
    <span x-text="product?.marketplace || 'Unknown'"></span>
    <span class="text-gray-400 mx-2">|</span>
    <span x-text="'ID: ' + productId"></span>
{% endcall %}

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

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

<!-- Product Details -->
<div x-show="!loading && product">
    <!-- 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="openCopyModal()"
                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('duplicate', 'w-4 h-4 mr-2')"></span>
                Copy to Store Catalog
            </button>
            <a
                x-show="product?.source_url"
                :href="product?.source_url"
                target="_blank"
                class="flex items-center px-4 py-2 text-sm font-medium leading-5 text-gray-700 dark:text-gray-300 transition-colors duration-150 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-600">
                <span x-html="$icon('external-link', 'w-4 h-4 mr-2')"></span>
                View Source
            </a>
        </div>
    </div>

    <!-- Product Header with Image -->
    <div class="grid gap-6 mb-8 md:grid-cols-3">
        <!-- Product Image -->
        <div class="px-4 py-3 bg-white rounded-lg shadow-md dark:bg-gray-800">
            <div class="aspect-square bg-gray-100 dark:bg-gray-700 rounded-lg overflow-hidden">
                <template x-if="product?.image_link">
                    <img loading="lazy" :src="product?.image_link" :alt="product?.title" class="w-full h-full object-contain" />
                </template>
                <template x-if="!product?.image_link">
                    <div class="w-full h-full flex items-center justify-center">
                        <span x-html="$icon('photograph', 'w-16 h-16 text-gray-300')"></span>
                    </div>
                </template>
            </div>
            <!-- Additional Images -->
            <div x-show="product?.additional_images?.length > 0" class="mt-4">
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase mb-2">Additional Images</p>
                <div class="grid grid-cols-4 gap-2">
                    <template x-for="(img, index) in (product?.additional_images || [])" :key="index">
                        <div class="aspect-square bg-gray-100 dark:bg-gray-700 rounded overflow-hidden">
                            <img loading="lazy" :src="img" :alt="'Image ' + (index + 1)" class="w-full h-full object-cover" />
                        </div>
                    </template>
                </div>
            </div>
        </div>

        <!-- Product Info -->
        <div class="md:col-span-2 space-y-6">
            <!-- Basic Info Card -->
            <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">
                    Product Information
                </h3>
                <div class="grid gap-4 md:grid-cols-2">
                    <div>
                        <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Brand</p>
                        <p class="text-sm text-gray-700 dark:text-gray-300" x-text="product?.brand || 'No brand'">-</p>
                    </div>
                    <div>
                        <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Product Type</p>
                        <div class="flex items-center gap-2">
                            <span class="px-2 py-1 text-xs font-semibold rounded-full"
                                  :class="product?.is_digital ? 'text-blue-700 bg-blue-100 dark:bg-blue-900/30 dark:text-blue-400' : 'text-orange-700 bg-orange-100 dark:bg-orange-900/30 dark:text-orange-400'"
                                  x-text="product?.is_digital ? 'Digital' : 'Physical'">
                            </span>
                            <span x-show="product?.product_type_enum" class="text-xs text-gray-500" x-text="'(' + product?.product_type_enum + ')'"></span>
                        </div>
                    </div>
                    <div>
                        <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Condition</p>
                        <p class="text-sm text-gray-700 dark:text-gray-300" x-text="product?.condition || 'Not specified'">-</p>
                    </div>
                    <div>
                        <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Status</p>
                        <span class="px-2 py-1 text-xs font-semibold rounded-full"
                              :class="product?.is_active ? 'text-green-700 bg-green-100 dark:bg-green-900/30 dark:text-green-400' : 'text-red-700 bg-red-100 dark:bg-red-900/30 dark:text-red-400'"
                              x-text="product?.is_active ? 'Active' : 'Inactive'">
                        </span>
                    </div>
                </div>
            </div>

            <!-- Pricing Card -->
            <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">
                    Pricing
                </h3>
                <div class="grid gap-4 md:grid-cols-3">
                    <div>
                        <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Price</p>
                        <p class="text-lg font-bold text-gray-700 dark:text-gray-200" x-text="formatPrice(product?.price_numeric, product?.currency)">-</p>
                    </div>
                    <div x-show="product?.sale_price_numeric">
                        <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Sale Price</p>
                        <p class="text-lg font-bold text-green-600 dark:text-green-400" x-text="formatPrice(product?.sale_price_numeric, product?.currency)">-</p>
                    </div>
                    <div>
                        <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Availability</p>
                        <p class="text-sm text-gray-700 dark:text-gray-300" x-text="product?.availability || 'Not specified'">-</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Identifiers 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">
            Product Identifiers
        </h3>
        <div class="grid gap-4 md:grid-cols-4">
            <div>
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Marketplace ID</p>
                <p class="text-sm font-mono text-gray-700 dark:text-gray-300" x-text="product?.marketplace_product_id || '-'">-</p>
            </div>
            <div>
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">GTIN/EAN</p>
                <p class="text-sm font-mono text-gray-700 dark:text-gray-300" x-text="product?.gtin || '-'">-</p>
            </div>
            <div>
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">MPN</p>
                <p class="text-sm font-mono text-gray-700 dark:text-gray-300" x-text="product?.mpn || '-'">-</p>
            </div>
            <div>
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">SKU</p>
                <p class="text-sm font-mono text-gray-700 dark:text-gray-300" x-text="product?.sku || '-'">-</p>
            </div>
        </div>
    </div>

    <!-- Source Information 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">
            Source Information
        </h3>
        <div class="grid gap-4 md:grid-cols-3">
            <div>
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Marketplace</p>
                <p class="text-sm text-gray-700 dark:text-gray-300" x-text="product?.marketplace || 'Unknown'">-</p>
            </div>
            <div>
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Source Store</p>
                <p class="text-sm text-gray-700 dark:text-gray-300" x-text="product?.store_name || 'Unknown'">-</p>
            </div>
            <div x-show="product?.platform">
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Platform</p>
                <p class="text-sm text-gray-700 dark:text-gray-300" x-text="product?.platform">-</p>
            </div>
        </div>
        <div x-show="product?.source_url" class="mt-4">
            <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase mb-1">Source URL</p>
            <a :href="product?.source_url" target="_blank" class="text-sm text-purple-600 hover:text-purple-700 dark:text-purple-400 break-all" x-text="product?.source_url">-</a>
        </div>
    </div>

    <!-- Category Information -->
    <div class="px-4 py-3 mb-6 bg-white rounded-lg shadow-md dark:bg-gray-800" x-show="product?.google_product_category || product?.category_path">
        <h3 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
            Categories
        </h3>
        <div class="space-y-3">
            <div x-show="product?.google_product_category">
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Google Product Category</p>
                <p class="text-sm text-gray-700 dark:text-gray-300" x-text="product?.google_product_category">-</p>
            </div>
            <div x-show="product?.category_path">
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Category Path</p>
                <p class="text-sm text-gray-700 dark:text-gray-300" x-text="product?.category_path">-</p>
            </div>
        </div>
    </div>

    <!-- Physical Attributes -->
    <div class="px-4 py-3 mb-6 bg-white rounded-lg shadow-md dark:bg-gray-800" x-show="product?.color || product?.size || product?.weight">
        <h3 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
            Physical Attributes
        </h3>
        <div class="grid gap-4 md:grid-cols-3">
            <div x-show="product?.color">
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Color</p>
                <p class="text-sm text-gray-700 dark:text-gray-300" x-text="product?.color">-</p>
            </div>
            <div x-show="product?.size">
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Size</p>
                <p class="text-sm text-gray-700 dark:text-gray-300" x-text="product?.size">-</p>
            </div>
            <div x-show="product?.weight">
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Weight</p>
                <p class="text-sm text-gray-700 dark:text-gray-300">
                    <span x-text="product?.weight"></span>
                    <span x-text="product?.weight_unit || ''"></span>
                </p>
            </div>
        </div>
    </div>

    <!-- Translations Card with Tabs -->
    <div class="px-4 py-3 mb-6 bg-white rounded-lg shadow-md dark:bg-gray-800" x-show="product?.translations && Object.keys(product.translations).length > 0" x-data="{ activeTab: Object.keys(product?.translations || {})[0] || '' }">
        <div class="flex items-center justify-between mb-4">
            <h3 class="text-lg font-semibold text-gray-700 dark:text-gray-200">
                Translations
            </h3>
            <span class="text-sm text-gray-500 dark:text-gray-400">
                <span x-text="Object.keys(product?.translations || {}).length"></span> language(s)
            </span>
        </div>

        <!-- Language Tabs -->
        <div class="border-b border-gray-200 dark:border-gray-700 mb-4">
            <nav class="-mb-px flex space-x-4 overflow-x-auto" aria-label="Translation tabs">
                <template x-for="(trans, lang) in (product?.translations || {})" :key="lang">
                    <button
                        @click="activeTab = lang"
                        :class="{
                            'border-purple-500 text-purple-600 dark:text-purple-400': activeTab === lang,
                            'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300 dark:text-gray-400 dark:hover:text-gray-300': activeTab !== lang
                        }"
                        class="whitespace-nowrap py-2 px-1 border-b-2 font-medium text-sm transition-colors"
                    >
                        <span class="uppercase" x-text="lang"></span>
                        <span class="ml-1 text-xs text-gray-400" x-text="getLanguageName(lang)"></span>
                    </button>
                </template>
            </nav>
        </div>

        <!-- Tab Content -->
        <template x-for="(trans, lang) in (product?.translations || {})" :key="'content-' + lang">
            <div x-show="activeTab === lang" x-transition:enter="transition ease-out duration-200" x-transition:enter-start="opacity-0" x-transition:enter-end="opacity-100">
                <div class="space-y-4">
                    <!-- Title -->
                    <div class="bg-gray-50 dark:bg-gray-700/50 p-4 rounded-lg">
                        <div class="flex items-center justify-between mb-2">
                            <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Title</p>
                            <button
                                @click="copyToClipboard(trans?.title)"
                                class="p-1 text-gray-400 hover:text-gray-600 dark:hover:text-gray-300"
                                title="Copy to clipboard"
                            >
                                <span x-html="$icon('clipboard', 'w-4 h-4')"></span>
                            </button>
                        </div>
                        <p class="text-base font-medium text-gray-900 dark:text-gray-100" x-text="trans?.title || 'No title'"></p>
                    </div>

                    <!-- Short Description -->
                    <div x-show="trans?.short_description" class="bg-gray-50 dark:bg-gray-700/50 p-4 rounded-lg">
                        <div class="flex items-center justify-between mb-2">
                            <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Short Description</p>
                            <button
                                @click="copyToClipboard(trans?.short_description)"
                                class="p-1 text-gray-400 hover:text-gray-600 dark:hover:text-gray-300"
                                title="Copy to clipboard"
                            >
                                <span x-html="$icon('clipboard', 'w-4 h-4')"></span>
                            </button>
                        </div>
                        <p class="text-sm text-gray-700 dark:text-gray-300" x-text="trans?.short_description"></p>
                    </div>

                    <!-- Description -->
                    <div x-show="trans?.description" class="bg-gray-50 dark:bg-gray-700/50 p-4 rounded-lg">
                        <div class="flex items-center justify-between mb-2">
                            <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Description</p>
                            <button
                                @click="copyToClipboard(trans?.description)"
                                class="p-1 text-gray-400 hover:text-gray-600 dark:hover:text-gray-300"
                                title="Copy to clipboard"
                            >
                                <span x-html="$icon('clipboard', 'w-4 h-4')"></span>
                            </button>
                        </div>
                        <div class="text-sm text-gray-700 dark:text-gray-300 prose prose-sm dark:prose-invert max-w-none max-h-96 overflow-y-auto" x-html="trans?.description || 'No description'"></div> <!-- noqa: SEC015 sanitized: admin-authored content -->
                    </div>

                    <!-- Empty state if no content -->
                    <div x-show="!trans?.title && !trans?.short_description && !trans?.description" class="text-center py-8">
                        <span x-html="$icon('document-text', 'w-12 h-12 mx-auto text-gray-300 dark:text-gray-600')"></span>
                        <p class="mt-2 text-sm text-gray-500 dark:text-gray-400">No translation content for this language</p>
                    </div>
                </div>
            </div>
        </template>
    </div>

    <!-- Timestamps -->
    <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">
            Record Information
        </h3>
        <div class="grid gap-4 md:grid-cols-2">
            <div>
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Created At</p>
                <p class="text-sm text-gray-700 dark:text-gray-300" x-text="formatDate(product?.created_at)">-</p>
            </div>
            <div>
                <p class="text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase">Last Updated</p>
                <p class="text-sm text-gray-700 dark:text-gray-300" x-text="formatDate(product?.updated_at)">-</p>
            </div>
        </div>
    </div>
</div>

<!-- Copy to Store Modal -->
{% call modal_simple('copyToStoreModal', 'Copy to Store Catalog', show_var='showCopyModal', size='md') %}
<div class="space-y-4">
    <p class="text-sm text-gray-600 dark:text-gray-400">
        Copy this product to a store's catalog.
    </p>

    <!-- Target Store Selection -->
    <div>
        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
            Target Store <span class="text-red-500">*</span>
        </label>
        <select
            x-model="copyForm.store_id"
            class="w-full px-4 py-2 text-sm text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg focus:border-purple-400 focus:outline-none"
        >
            <option value="">Select a store...</option>
            <template x-for="store in targetStores" :key="store.id">
                <option :value="store.id" x-text="store.name + ' (' + store.store_code + ')'"></option>
            </template>
        </select>
        <p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
            The product will be copied to this store's catalog
        </p>
    </div>

    <!-- Options -->
    <div class="space-y-2">
        <label class="flex items-center">
            <input
                type="checkbox"
                x-model="copyForm.skip_existing"
                class="rounded border-gray-300 dark:border-gray-600 text-purple-600 focus:ring-purple-500"
            />
            <span class="ml-2 text-sm text-gray-700 dark:text-gray-300">Skip if already exists in catalog</span>
        </label>
    </div>

    <!-- Actions -->
    <div class="flex items-center justify-end gap-3 pt-4 border-t border-gray-200 dark:border-gray-700">
        <button
            @click="showCopyModal = false"
            class="px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-600 transition-colors"
        >
            Cancel
        </button>
        <button
            @click="executeCopyToStore()"
            :disabled="!copyForm.store_id || copying"
            class="flex items-center px-4 py-2 text-sm font-medium text-white bg-purple-600 rounded-lg hover:bg-purple-700 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
        >
            <span x-show="copying" x-html="$icon('spinner', 'w-4 h-4 mr-2')"></span>
            <span x-text="copying ? 'Copying...' : 'Copy Product'"></span>
        </button>
    </div>
</div>
{% endcall %}
{% endblock %}

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