{# app/templates/store/email-templates.html #}
{% extends "store/base.html" %}
{% from 'shared/macros/headers.html' import page_header_flex %}
{% from 'shared/macros/alerts.html' import loading_state, error_state %}
{% from 'shared/macros/modals.html' import modal_dialog, confirm_modal %}

{% block title %}Email Templates{% endblock %}

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

{% block content %}
<!-- Page Header -->
{% call page_header_flex(title='Email Templates', subtitle='Customize email templates sent to your customers') %}
{% endcall %}

{{ loading_state('Loading email templates...') }}

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

<!-- Main Content -->
<div x-show="!loading && !error" class="space-y-6">
    <!-- Info Banner -->
    <div class="p-4 bg-blue-50 dark:bg-blue-900/20 rounded-lg border border-blue-200 dark:border-blue-800">
        <div class="flex items-start gap-3">
            <span x-html="$icon('information-circle', 'w-5 h-5 text-blue-600 dark:text-blue-400 flex-shrink-0 mt-0.5')"></span>
            <div>
                <p class="text-sm text-blue-800 dark:text-blue-300">
                    Customize how emails appear to your customers. Platform templates are used by default,
                    and you can override them with your own versions. Some templates (billing, subscriptions)
                    are platform-only and cannot be customized.
                </p>
            </div>
        </div>
    </div>

    <!-- Templates Table -->
    {# noqa: FE-005 - Table has custom header section and styling not compatible with table_wrapper #}
    <div class="bg-white rounded-lg shadow-xs dark:bg-gray-800 overflow-hidden">
        <div class="p-4 border-b dark:border-gray-700">
            <h3 class="text-lg font-semibold text-gray-800 dark:text-gray-200">Available Templates</h3>
            <p class="text-sm text-gray-500 dark:text-gray-400">Click a template to customize it</p>
        </div>

        <div class="overflow-x-auto">
            <table class="w-full">
                <thead class="bg-gray-50 dark:bg-gray-700">
                    <tr>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Template</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Category</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Languages</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Status</th>
                        <th class="px-4 py-3 text-right text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-200 dark:divide-gray-700">
                    <template x-for="template in templates" :key="template.code">
                        <tr class="hover:bg-gray-50 dark:hover:bg-gray-700/50">
                            <td class="px-4 py-4">
                                <p class="text-sm font-medium text-gray-800 dark:text-gray-200" x-text="template.name"></p>
                                <p class="text-xs text-gray-500 dark:text-gray-400 font-mono" x-text="template.code"></p>
                            </td>
                            <td class="px-4 py-4">
                                <span
                                    :class="getCategoryClass(template.category)"
                                    class="px-2 py-1 text-xs font-medium rounded-full"
                                    x-text="template.category"
                                ></span>
                            </td>
                            <td class="px-4 py-4">
                                <div class="flex flex-wrap gap-1">
                                    <template x-for="lang in supportedLanguages" :key="lang">
                                        <span
                                            :class="template.override_languages.includes(lang)
                                                ? 'bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400'
                                                : 'bg-gray-100 text-gray-600 dark:bg-gray-700 dark:text-gray-400'"
                                            class="px-2 py-0.5 text-xs font-medium rounded uppercase"
                                            x-text="lang"
                                        ></span>
                                    </template>
                                </div>
                            </td>
                            <td class="px-4 py-4">
                                <template x-if="template.has_override">
                                    <span class="inline-flex items-center gap-1 px-2 py-1 text-xs font-medium text-green-700 bg-green-100 rounded-full dark:bg-green-900/30 dark:text-green-400">
                                        <span x-html="$icon('check-circle', 'w-3 h-3')"></span>
                                        Customized
                                    </span>
                                </template>
                                <template x-if="!template.has_override">
                                    <span class="px-2 py-1 text-xs font-medium text-gray-600 bg-gray-100 rounded-full dark:bg-gray-700 dark:text-gray-400">
                                        Platform Default
                                    </span>
                                </template>
                            </td>
                            <td class="px-4 py-4 text-right">
                                <button
                                    @click="editTemplate(template)"
                                    class="px-3 py-1.5 text-sm font-medium text-purple-600 hover:text-purple-700 hover:bg-purple-50 rounded-lg dark:text-purple-400 dark:hover:bg-purple-900/20"
                                >
                                    Customize
                                </button>
                            </td>
                        </tr>
                    </template>
                </tbody>
            </table>
        </div>

        <template x-if="templates.length === 0">
            <div class="p-8 text-center">
                <span x-html="$icon('mail', 'w-12 h-12 mx-auto text-gray-400 dark:text-gray-500')"></span>
                <p class="mt-2 text-gray-500 dark:text-gray-400">No customizable templates available</p>
            </div>
        </template>
    </div>
</div>

<!-- Edit Template Modal -->
{% call modal_dialog(
    show_var="showEditModal",
    title_var="editingTemplate ? 'Customize: ' + editingTemplate.name : 'Edit Template'",
    size="4xl"
) %}
<template x-if="editingTemplate">
    <div class="space-y-6">
        <!-- Language Tabs -->
        <div class="border-b dark:border-gray-700">
            <div class="flex flex-wrap gap-1">
                <template x-for="lang in supportedLanguages" :key="lang">
                    <button
                        @click="editLanguage = lang; loadTemplateLanguage()"
                        :class="editLanguage === lang
                            ? 'border-purple-500 text-purple-600 dark:text-purple-400'
                            : 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300 dark:text-gray-400'"
                        class="px-4 py-2 text-sm font-medium border-b-2 uppercase"
                        x-text="lang"
                    ></button>
                </template>
            </div>
        </div>

        <!-- Loading -->
        <div x-show="loadingTemplate" class="flex items-center justify-center py-8">
            <span x-html="$icon('loading', 'w-8 h-8 animate-spin text-purple-600')"></span>
        </div>

        <!-- Edit Form -->
        <div x-show="!loadingTemplate" class="space-y-4">
            <!-- Source Indicator -->
            <div class="flex items-center gap-2 text-sm">
                <template x-if="templateSource === 'store_override'">
                    <span class="text-green-600 dark:text-green-400">Using your customized version</span>
                </template>
                <template x-if="templateSource === 'platform'">
                    <span class="text-gray-500 dark:text-gray-400">Using platform default - edit to create your version</span>
                </template>
            </div>

            <!-- Subject -->
            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                    Subject Line
                </label>
                <input
                    type="text"
                    x-model="editForm.subject"
                    class="w-full px-4 py-2 text-sm text-gray-700 bg-white border border-gray-200 rounded-lg dark:text-gray-300 dark:bg-gray-800 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
                    placeholder="Email subject..."
                />
            </div>

            <!-- Variables Info -->
            <div x-show="editingTemplate.variables?.length > 0" class="p-3 bg-gray-50 dark:bg-gray-700 rounded-lg">
                <p class="text-xs font-medium text-gray-700 dark:text-gray-300 mb-2">Available Variables:</p>
                <div class="flex flex-wrap gap-2">
                    <template x-for="variable in editingTemplate.variables" :key="variable">
                        <code class="px-2 py-0.5 text-xs bg-white dark:bg-gray-600 rounded border dark:border-gray-500" x-text="'{{ ' + variable + ' }}'"></code>
                    </template>
                </div>
            </div>

            <!-- HTML Body -->
            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                    HTML Content
                </label>
                <textarea
                    x-model="editForm.body_html"
                    rows="12"
                    class="w-full px-4 py-2 text-sm font-mono text-gray-700 bg-white border border-gray-200 rounded-lg dark:text-gray-300 dark:bg-gray-800 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
                    placeholder="<html>...</html>"
                ></textarea>
            </div>

            <!-- Plain Text Body -->
            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                    Plain Text (Optional)
                </label>
                <textarea
                    x-model="editForm.body_text"
                    rows="4"
                    class="w-full px-4 py-2 text-sm font-mono text-gray-700 bg-white border border-gray-200 rounded-lg dark:text-gray-300 dark:bg-gray-800 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
                    placeholder="Plain text fallback..."
                ></textarea>
            </div>
        </div>

        <!-- Actions -->
        <div class="flex items-center justify-between pt-4 border-t dark:border-gray-700">
            <div>
                <!-- Revert to Default Button -->
                <template x-if="templateSource === 'store_override'">
                    <button
                        @click="showRevertConfirm = true"
                        :disabled="reverting"
                        class="px-4 py-2 text-sm font-medium text-red-600 hover:text-red-700 hover:bg-red-50 rounded-lg dark:text-red-400 dark:hover:bg-red-900/20"
                    >
                        <span x-show="!reverting">Revert to Platform Default</span>
                        <span x-show="reverting">Reverting...</span>
                    </button>
                </template>
            </div>
            <div class="flex items-center gap-3">
                <button
                    @click="previewTemplate()"
                    class="px-4 py-2 text-sm font-medium text-gray-600 hover:text-gray-700 hover:bg-gray-100 rounded-lg dark:text-gray-400 dark:hover:bg-gray-700"
                >
                    Preview
                </button>
                <button
                    @click="sendTestEmail()"
                    class="px-4 py-2 text-sm font-medium text-gray-600 hover:text-gray-700 hover:bg-gray-100 rounded-lg dark:text-gray-400 dark:hover:bg-gray-700"
                >
                    Send Test
                </button>
                <button
                    @click="closeEditModal()"
                    class="px-4 py-2 text-sm font-medium text-gray-600 hover:text-gray-700 rounded-lg dark:text-gray-400"
                >
                    Cancel
                </button>
                <button
                    @click="saveTemplate()"
                    :disabled="saving"
                    class="px-4 py-2 text-sm font-medium text-white bg-purple-600 rounded-lg hover:bg-purple-700 disabled:opacity-50"
                >
                    <span x-show="!saving">Save Override</span>
                    <span x-show="saving">Saving...</span>
                </button>
            </div>
        </div>
    </div>
</template>
{% endcall %}

<!-- Preview Modal -->
{% call modal_dialog(
    show_var="showPreviewModal",
    title="Email Preview",
    size="4xl"
) %}
<template x-if="previewData">
    <div class="space-y-4">
        <div class="p-3 bg-gray-100 dark:bg-gray-700 rounded-lg">
            <p class="text-sm"><strong>Subject:</strong> <span x-text="previewData.subject"></span></p>
        </div>
        <div class="border dark:border-gray-700 rounded-lg overflow-hidden">
            <iframe
                :srcdoc="previewData.body_html"
                class="w-full h-96 bg-white"
                sandbox="allow-same-origin"
            ></iframe>
        </div>
        <div class="flex justify-end">
            <button
                @click="showPreviewModal = false"
                class="px-4 py-2 text-sm font-medium text-gray-600 hover:text-gray-700 rounded-lg dark:text-gray-400"
            >
                Close
            </button>
        </div>
    </div>
</template>
{% endcall %}

<!-- Test Email Modal -->
{% call modal_dialog(
    show_var="showTestEmailModal",
    title="Send Test Email",
    size="md"
) %}
<div class="space-y-4">
    <div>
        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
            Send test email to:
        </label>
        <input
            type="email"
            x-model="testEmailAddress"
            class="w-full px-4 py-2 text-sm text-gray-700 bg-white border border-gray-200 rounded-lg dark:text-gray-300 dark:bg-gray-800 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
            placeholder="your@email.com"
        />
    </div>
    <p class="text-sm text-gray-500 dark:text-gray-400">
        A test email will be sent using sample data for template variables.
    </p>
    <div class="flex justify-end gap-3">
        <button
            @click="showTestEmailModal = false"
            class="px-4 py-2 text-sm font-medium text-gray-600 hover:text-gray-700 rounded-lg dark:text-gray-400"
        >
            Cancel
        </button>
        <button
            @click="confirmSendTestEmail()"
            :disabled="sendingTest || !testEmailAddress"
            class="px-4 py-2 text-sm font-medium text-white bg-purple-600 rounded-lg hover:bg-purple-700 disabled:opacity-50"
        >
            <span x-show="!sendingTest">Send Test</span>
            <span x-show="sendingTest">Sending...</span>
        </button>
    </div>
</div>
{% endcall %}

<!-- Revert to Default Confirm Modal -->
{{ confirm_modal('revertConfirm', 'Revert to Platform Default', 'Are you sure you want to delete your customization and revert to the platform default template? This action cannot be undone.', 'revertToDefault()', 'showRevertConfirm', 'Revert', 'Cancel', 'danger') }}
{% endblock %}

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