{# app/templates/admin/platform-create.html #}
{% extends "admin/base.html" %}
{% from 'shared/macros/alerts.html' import loading_state %}
{% from 'shared/macros/headers.html' import edit_page_header %}

{% block title %}Create Platform{% endblock %}

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

{% block content %}
{% call edit_page_header('Create Platform', '/admin/platforms', back_label='Back to Platforms') %}
    New Platform
{% endcall %}

<!-- Error State -->
<div x-show="error" class="mb-6 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg dark:bg-red-900/50 dark:border-red-600 dark:text-red-200">
    <div class="flex items-center">
        <span x-html="$icon('exclamation-circle', 'w-5 h-5 mr-2')"></span>
        <span x-text="error"></span>
    </div>
</div>

<!-- Create Form -->
<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>

            <!-- Platform Code -->
            <label class="block mb-4 text-sm">
                <span class="text-gray-700 dark:text-gray-400">
                    Platform Code <span class="text-red-600">*</span>
                </span>
                <input
                    type="text"
                    x-model="formData.code"
                    required
                    maxlength="50"
                    pattern="^[a-z][a-z0-9_-]*$"
                    placeholder="e.g., marketplace"
                    :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.code }"
                >
                <span class="text-xs text-gray-600 dark:text-gray-400 mt-1">
                    Lowercase letters, numbers, hyphens, and underscores. Cannot be changed later.
                </span>
                <span x-show="errors.code" class="text-xs text-red-600 dark:text-red-400 mt-1" x-text="errors.code"></span>
            </label>

            <!-- Name -->
            <label class="block mb-4 text-sm">
                <span class="text-gray-700 dark:text-gray-400">
                    Platform Name <span class="text-red-600">*</span>
                </span>
                <input
                    type="text"
                    x-model="formData.name"
                    required
                    maxlength="100"
                    placeholder="e.g., Marketplace"
                    :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>

            <!-- Description (multilingual) -->
            <div class="block mb-4 text-sm">
                <span class="text-gray-700 dark:text-gray-400">
                    Description
                </span>

                <!-- Language Tabs -->
                <div class="mt-2 border-b border-gray-200 dark:border-gray-700">
                    <nav class="flex -mb-px space-x-2">
                        <template x-for="lang in formData.supported_languages" :key="lang">
                            <button
                                type="button"
                                @click="currentLang = lang"
                                :class="currentLang === lang
                                    ? 'border-purple-500 text-purple-600 dark:text-purple-400'
                                    : 'border-transparent text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-300'"
                                class="py-2 px-3 border-b-2 font-medium text-xs transition-colors"
                            >
                                <span x-text="languageNames[lang] || lang.toUpperCase()"></span>
                                <span x-show="lang === formData.default_language" class="ml-1 text-xs text-gray-400">(default)</span>
                            </button>
                        </template>
                    </nav>
                </div>

                <!-- Per-language textarea -->
                <textarea
                    x-model="formData.description_translations[currentLang]"
                    rows="3"
                    maxlength="500"
                    :disabled="saving"
                    :placeholder="'Description in ' + (languageNames[currentLang] || currentLang)"
                    class="block w-full mt-2 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>
            </div>

            <!-- Default Language -->
            <label class="block mb-4 text-sm">
                <span class="text-gray-700 dark:text-gray-400">
                    Default Language
                </span>
                <select
                    x-model="formData.default_language"
                    :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-select"
                >
                    <template x-for="lang in availableLanguages" :key="lang.code">
                        <option :value="lang.code" x-text="lang.name"></option>
                    </template>
                </select>
            </label>

            <!-- Supported Languages -->
            <label class="block mb-4 text-sm">
                <span class="text-gray-700 dark:text-gray-400">
                    Supported Languages
                </span>
                <div class="mt-2 flex flex-wrap gap-2">
                    <template x-for="lang in availableLanguages" :key="lang.code">
                        <button
                            type="button"
                            @click="toggleLanguage(lang.code)"
                            :disabled="saving"
                            class="px-3 py-1 text-sm rounded-full transition-colors"
                            :class="isLanguageSupported(lang.code)
                                ? 'bg-purple-600 text-white'
                                : 'bg-gray-200 text-gray-700 dark:bg-gray-700 dark:text-gray-300'"
                        >
                            <span x-text="lang.name"></span>
                        </button>
                    </template>
                </div>
            </label>
        </div>

        <!-- Right Column: Routing -->
        <div>
            <h3 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
                Routing
            </h3>

            <!-- Domain -->
            <label class="block mb-4 text-sm">
                <span class="text-gray-700 dark:text-gray-400">
                    Production Domain
                </span>
                <input
                    type="text"
                    x-model="formData.domain"
                    placeholder="e.g., marketplace.lu"
                    :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"
                >
                <span class="text-xs text-gray-600 dark:text-gray-400 mt-1">
                    Domain used in production for this platform
                </span>
            </label>

            <!-- Path Prefix -->
            <label class="block mb-4 text-sm">
                <span class="text-gray-700 dark:text-gray-400">
                    Development Path Prefix
                </span>
                <input
                    type="text"
                    x-model="formData.path_prefix"
                    placeholder="e.g., marketplace"
                    :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"
                >
                <span class="text-xs text-gray-600 dark:text-gray-400 mt-1">
                    Used for /platforms/{prefix}/ routing in development
                </span>
            </label>

            <!-- Status Options -->
            <h3 class="mb-4 mt-8 text-lg font-semibold text-gray-700 dark:text-gray-200">
                Status
            </h3>

            <div class="flex flex-wrap gap-4">
                <label class="flex items-center text-sm">
                    <input
                        type="checkbox"
                        x-model="formData.is_active"
                        :disabled="saving"
                        class="text-purple-600 form-checkbox focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray"
                    >
                    <span class="ml-2 text-gray-700 dark:text-gray-400">Active</span>
                </label>
                <label class="flex items-center text-sm">
                    <input
                        type="checkbox"
                        x-model="formData.is_public"
                        :disabled="saving"
                        class="text-purple-600 form-checkbox focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray"
                    >
                    <span class="ml-2 text-gray-700 dark:text-gray-400">Public</span>
                </label>
            </div>
        </div>
    </div>

    <!-- Submit Button -->
    <div class="flex justify-end gap-3">
        <a
            href="/admin/platforms"
            class="px-4 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-300 rounded-lg hover:bg-gray-50 dark:bg-gray-700 dark:text-gray-300 dark:border-gray-600 dark:hover:bg-gray-600"
        >
            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 active:bg-purple-600 hover:bg-purple-700 focus:outline-none focus:shadow-outline-purple disabled:opacity-50"
        >
            <span x-show="saving" x-html="$icon('refresh', 'w-4 h-4 mr-2 animate-spin')"></span>
            <span x-text="saving ? 'Creating...' : 'Create Platform'"></span>
        </button>
    </div>
</form>
{% endblock %}

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