{# app/modules/dev_tools/templates/dev_tools/admin/translation-editor.html #}
{% extends "admin/base.html" %}
{% from 'shared/macros/headers.html' import page_header %}
{% from 'shared/macros/alerts.html' import loading_state, error_state %}
{% from 'shared/macros/tables.html' import table_wrapper, table_header, table_empty_state %}
{% from 'shared/macros/pagination.html' import pagination %}

{% block title %}Translation Editor{% endblock %}

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

{% block content %}
{{ page_header('Translation Editor', back_url='/admin/dashboard', back_label='Back to Dashboard') }}

{{ loading_state('Loading translations...') }}
{{ error_state('Error loading translations') }}

<div x-show="!loading && !error" class="space-y-4">
    <!-- Top filter bar -->
    <div class="bg-white dark:bg-gray-800 rounded-lg shadow-xs p-4">
        <div class="flex flex-wrap items-center gap-3">
            <!-- Module filter -->
            <div class="flex items-center gap-2">
                <label class="text-sm font-medium text-gray-600 dark:text-gray-400">Module</label>
                <select x-model="selectedModule" @change="pagination.page = 1"
                        class="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="all">All Modules</option>
                    <template x-for="mod in modules" :key="mod.code">
                        <option :value="mod.code" :selected="selectedModule === mod.code"
                                x-text="`${mod.label} (${mod.key_count})`"></option>
                    </template>
                </select>
            </div>

            <!-- Search -->
            <div class="flex-1 min-w-[200px]">
                <div class="relative">
                    <span class="absolute inset-y-0 left-0 flex items-center pl-3">
                        <span x-html="$icon('search', 'w-5 h-5 text-gray-400')"></span>
                    </span>
                    <input type="text" x-model="searchQuery" @input.debounce.200ms="pagination.page = 1"
                           placeholder="Search keys and values..."
                           class="w-full pl-10 pr-4 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded-lg focus:border-purple-400 focus:outline-none dark:bg-gray-700 dark:text-gray-300">
                </div>
            </div>

            <!-- Filter mode -->
            <div class="flex rounded-lg border border-gray-300 dark:border-gray-600 overflow-hidden">
                <button @click="filterMode = 'all'; pagination.page = 1"
                        :class="filterMode === 'all' ? 'bg-purple-600 text-white' : 'bg-white dark:bg-gray-700 text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-600'"
                        class="px-3 py-1.5 text-sm font-medium transition-colors">All</button>
                <button @click="filterMode = 'missing'; pagination.page = 1"
                        :class="filterMode === 'missing' ? 'bg-amber-600 text-white' : 'bg-white dark:bg-gray-700 text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-600'"
                        class="px-3 py-1.5 text-sm font-medium border-l border-gray-300 dark:border-gray-600 transition-colors">Missing</button>
                <button @click="filterMode = 'has_variables'; pagination.page = 1"
                        :class="filterMode === 'has_variables' ? 'bg-purple-600 text-white' : 'bg-white dark:bg-gray-700 text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-600'"
                        class="px-3 py-1.5 text-sm font-medium border-l border-gray-300 dark:border-gray-600 transition-colors">Variables</button>
            </div>

            <!-- Stats badges -->
            <div class="flex items-center gap-2">
                <template x-for="lang in languages" :key="lang">
                    <span x-show="stats.missing && stats.missing[lang] > 0"
                          class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium"
                          :class="stats.missing[lang] > 50 ? 'bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-300' : 'bg-amber-100 text-amber-800 dark:bg-amber-900/30 dark:text-amber-300'">
                        <span x-text="lang.toUpperCase()"></span>:
                        <span x-text="stats.missing[lang]" class="ml-0.5"></span>
                    </span>
                </template>
            </div>
        </div>
    </div>

    <!-- Results info -->
    <div class="flex items-center justify-between text-sm text-gray-600 dark:text-gray-400">
        <span>
            Showing <span x-text="filteredKeys.length" class="font-medium"></span>
            of <span x-text="keys.length" class="font-medium"></span> keys
        </span>
        <span x-show="stats.total_keys">
            Total: <span x-text="stats.total_keys" class="font-medium"></span> keys across
            <span x-text="modules.length" class="font-medium"></span> modules
        </span>
    </div>

    <!-- Translation table -->
    {% call table_wrapper() %}
        <thead>
            <tr class="text-xs font-semibold tracking-wide text-left text-gray-500 uppercase border-b dark:border-gray-700 bg-gray-50 dark:text-gray-400 dark:bg-gray-800">
                <th class="px-4 py-3 w-28">Module</th>
                <th class="px-4 py-3 w-64">Key Path</th>
                <template x-for="lang in languages" :key="lang">
                    <th class="px-4 py-3" x-text="lang.toUpperCase()"></th>
                </template>
            </tr>
        </thead>
        <tbody class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-800">
            {{ table_empty_state(6, title='No translation keys found', icon='globe', show_condition='filteredKeys.length === 0') }}
            <template x-for="entry in paginatedKeys" :key="entry.module + '.' + entry.path">
                <tr class="text-gray-700 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700">
                    <td class="px-4 py-3 text-xs font-mono text-gray-500 dark:text-gray-400 whitespace-nowrap">
                        <span x-text="entry.module"></span>
                    </td>
                    <td class="px-4 py-3 text-sm font-mono text-gray-900 dark:text-gray-100 whitespace-nowrap">
                        <span x-text="entry.path"></span>
                        <span x-show="entry.has_variables"
                              class="ml-1 inline-flex items-center px-1.5 py-0.5 rounded text-[10px] font-medium bg-purple-100 text-purple-700 dark:bg-purple-900/30 dark:text-purple-300"
                              :title="'Variables: ' + (entry.variables || []).join(', ')">
                            { }
                        </span>
                    </td>
                    <template x-for="lang in languages" :key="lang">
                        <td class="px-4 py-2 text-sm max-w-xs relative group"
                            @click="startEdit(entry, lang)"
                            :class="entry.values[lang] === null ? 'bg-red-50/50 dark:bg-red-900/10' : ''">

                            <!-- Display mode -->
                            <div x-show="!isEditing(entry, lang)"
                                 class="cursor-pointer min-h-[28px] flex items-center rounded px-1 -mx-1 group-hover:bg-gray-100 dark:group-hover:bg-gray-700 transition-colors">
                                <span x-show="entry.values[lang] !== null"
                                      class="text-gray-900 dark:text-gray-100 truncate"
                                      :title="entry.values[lang]"
                                      x-text="entry.values[lang]"></span>
                                <span x-show="entry.values[lang] === null"
                                      class="text-red-400 dark:text-red-500 italic text-xs">missing</span>
                            </div>

                            <!-- Edit mode -->
                            <div x-show="isEditing(entry, lang)" @click.stop>
                                <textarea x-ref="editInput"
                                          x-model="editValue"
                                          @keydown.enter.prevent="saveEdit()"
                                          @keydown.tab.prevent="saveAndNext(entry, lang)"
                                          @keydown.escape="cancelEdit()"
                                          @blur="saveEdit()"
                                          rows="2"
                                          class="w-full text-sm rounded border-purple-400 dark:border-purple-500 dark:bg-gray-700 dark:text-gray-100 focus:ring-purple-500 focus:border-purple-500 resize-y p-1"></textarea>
                                <!-- Variable warning -->
                                <div x-show="entry.has_variables && editValue && getMissingVars(entry, editValue).length > 0"
                                     class="mt-0.5 text-[10px] text-amber-600 dark:text-amber-400">
                                    Missing: <span x-text="getMissingVars(entry, editValue).map(v => '{' + v + '}').join(', ')"></span>
                                </div>
                            </div>
                        </td>
                    </template>
                </tr>
            </template>
        </tbody>
    {% endcall %}
    {{ pagination(show_condition='filteredKeys.length > 0') }}

    <!-- Save indicator -->
    <div x-show="saving" x-transition
         class="fixed bottom-6 right-6 bg-purple-600 text-white px-4 py-2 rounded-lg shadow-lg text-sm flex items-center gap-2">
        <span x-html="$icon('spinner', 'w-4 h-4')"></span>
        Saving...
    </div>
</div>
{% endblock %}

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