{# app/templates/admin/platform-detail.html #}
{% extends "admin/base.html" %}
{% from 'shared/macros/alerts.html' import loading_state, error_state %}
{% from 'shared/macros/headers.html' import page_header %}

{% block title %}Platform Details{% endblock %}

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

{% block content %}
<!-- Header with Back Button -->
<div class="flex items-center justify-between my-6">
    <div class="flex items-center">
        <a href="/admin/platforms" class="mr-4 p-2 rounded-lg hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors">
            <span x-html="$icon('arrow-left', 'w-5 h-5 text-gray-600 dark:text-gray-400')"></span>
        </a>
        <div>
            <div class="flex items-center">
                <span x-html="$icon(getPlatformIcon(platformCode), 'w-8 h-8 text-purple-600 dark:text-purple-400 mr-3')"></span>
                <h2 class="text-2xl font-semibold text-gray-700 dark:text-gray-200" x-text="platform?.name || 'Loading...'"></h2>
                <code class="ml-3 text-sm bg-gray-100 dark:bg-gray-700 px-2 py-1 rounded" x-text="platformCode"></code>
            </div>
            <p class="text-sm text-gray-600 dark:text-gray-400 mt-1" x-text="platform?.description || ''"></p>
        </div>
    </div>
    <div class="flex items-center gap-2">
        <!-- Status Badges -->
        <span
            x-show="platform?.is_active"
            class="inline-flex items-center px-3 py-1 text-xs font-semibold rounded-full bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200">
            Active
        </span>
        <span
            x-show="!platform?.is_active"
            class="inline-flex items-center px-3 py-1 text-xs font-semibold rounded-full bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-200">
            Inactive
        </span>
    </div>
</div>

{{ loading_state('Loading platform details...') }}
{{ error_state('Error loading platform') }}

<!-- Main Content -->
<div x-show="!loading && platform" class="space-y-6">

    <!-- Quick Actions -->
    <div class="bg-white dark:bg-gray-800 rounded-lg shadow-md p-6">
        <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4">Quick Actions</h3>
        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <!-- Edit Settings -->
            <a :href="`/admin/platforms/${platformCode}/edit`"
               class="flex items-center p-4 bg-purple-50 dark:bg-purple-900/20 rounded-lg hover:bg-purple-100 dark:hover:bg-purple-900/40 transition-colors">
                <span x-html="$icon('cog', 'w-8 h-8 text-purple-600 dark:text-purple-400')"></span>
                <div class="ml-3">
                    <p class="font-semibold text-gray-900 dark:text-white">Edit Settings</p>
                    <p class="text-sm text-gray-600 dark:text-gray-400">Branding, domain, languages</p>
                </div>
            </a>

            <!-- View Platform -->
            <a :href="getPlatformUrl()" target="_blank"
               class="flex items-center p-4 bg-gray-50 dark:bg-gray-700/50 rounded-lg hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors">
                <span x-html="$icon('external-link', 'w-8 h-8 text-gray-600 dark:text-gray-400')"></span>
                <div class="ml-3">
                    <p class="font-semibold text-gray-900 dark:text-white">View Platform</p>
                    <p class="text-sm text-gray-600 dark:text-gray-400">Open in new tab</p>
                </div>
            </a>
        </div>
    </div>

    <!-- Super Admin Actions (Menu Configuration) -->
    <div x-show="isSuperAdmin" class="bg-white dark:bg-gray-800 rounded-lg shadow-md p-6">
        <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4">
            <span class="inline-flex items-center">
                Super Admin
                <span class="ml-2 px-2 py-0.5 text-xs font-medium rounded bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-200">Admin Only</span>
            </span>
        </h3>
        <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
            <!-- Module Configuration -->
            <a :href="`/admin/platforms/${platformCode}/modules`"
               class="flex items-center p-4 bg-green-50 dark:bg-green-900/20 rounded-lg hover:bg-green-100 dark:hover:bg-green-900/40 transition-colors">
                <span x-html="$icon('puzzle-piece', 'w-8 h-8 text-green-600 dark:text-green-400')"></span>
                <div class="ml-3">
                    <p class="font-semibold text-gray-900 dark:text-white">Module Configuration</p>
                    <p class="text-sm text-gray-600 dark:text-gray-400">Enable/disable features</p>
                </div>
            </a>

            <!-- Menu Configuration -->
            <a :href="`/admin/platforms/${platformCode}/menu-config`"
               class="flex items-center p-4 bg-amber-50 dark:bg-amber-900/20 rounded-lg hover:bg-amber-100 dark:hover:bg-amber-900/40 transition-colors">
                <span x-html="$icon('view-grid', 'w-8 h-8 text-amber-600 dark:text-amber-400')"></span>
                <div class="ml-3">
                    <p class="font-semibold text-gray-900 dark:text-white">Menu Configuration</p>
                    <p class="text-sm text-gray-600 dark:text-gray-400">Admin & store menus</p>
                </div>
            </a>
        </div>
    </div>

    <!-- Stats Grid -->
    <div class="grid grid-cols-1 md:grid-cols-4 gap-6">
        <!-- Stores -->
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow-md p-6">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm text-gray-500 dark:text-gray-400">Stores</p>
                    <p class="text-3xl font-bold text-purple-600 dark:text-purple-400" x-text="platform?.store_count || 0"></p>
                </div>
                <div class="p-3 bg-purple-100 dark:bg-purple-900/50 rounded-full">
                    <span x-html="$icon('store', 'w-6 h-6 text-purple-600 dark:text-purple-400')"></span>
                </div>
            </div>
        </div>

        <!-- Languages -->
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow-md p-6">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm text-gray-500 dark:text-gray-400">Languages</p>
                    <p class="text-3xl font-bold text-blue-600 dark:text-blue-400" x-text="(platform?.supported_languages || []).length"></p>
                </div>
                <div class="p-3 bg-blue-100 dark:bg-blue-900/50 rounded-full">
                    <span x-html="$icon('translate', 'w-6 h-6 text-blue-600 dark:text-blue-400')"></span>
                </div>
            </div>
        </div>

        <!-- Active Status -->
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow-md p-6">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm text-gray-500 dark:text-gray-400">Status</p>
                    <p class="text-xl font-bold" :class="platform?.is_active ? 'text-green-600 dark:text-green-400' : 'text-red-600 dark:text-red-400'" x-text="platform?.is_active ? 'Active' : 'Inactive'"></p>
                </div>
                <div class="p-3 rounded-full" :class="platform?.is_active ? 'bg-green-100 dark:bg-green-900/50' : 'bg-red-100 dark:bg-red-900/50'">
                    <span x-html="$icon(platform?.is_active ? 'check-circle' : 'x-circle', 'w-6 h-6 ' + (platform?.is_active ? 'text-green-600 dark:text-green-400' : 'text-red-600 dark:text-red-400'))"></span>
                </div>
            </div>
        </div>

        <!-- Default Language -->
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow-md p-6">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm text-gray-500 dark:text-gray-400">Default Language</p>
                    <p class="text-3xl font-bold text-gray-700 dark:text-gray-300" x-text="platform?.default_language?.toUpperCase() || '—'"></p>
                </div>
                <div class="p-3 bg-gray-100 dark:bg-gray-700 rounded-full">
                    <span x-html="$icon('translate', 'w-6 h-6 text-gray-600 dark:text-gray-400')"></span>
                </div>
            </div>
        </div>
    </div>

    <!-- Platform Configuration -->
    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
        <!-- Routing Info -->
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow-md p-6">
            <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4">Routing Configuration</h3>
            <div class="space-y-3">
                <div class="flex justify-between items-center py-2 border-b border-gray-200 dark:border-gray-700">
                    <span class="text-gray-600 dark:text-gray-400">Production Domain</span>
                    <code x-show="platform?.domain" class="text-sm bg-gray-100 dark:bg-gray-700 px-2 py-1 rounded" x-text="platform?.domain"></code>
                    <span x-show="!platform?.domain" class="text-gray-400 dark:text-gray-500 text-sm">Not configured</span>
                </div>
                <div class="flex justify-between items-center py-2 border-b border-gray-200 dark:border-gray-700">
                    <span class="text-gray-600 dark:text-gray-400">Dev Path Prefix</span>
                    <code x-show="platform?.path_prefix" class="text-sm bg-gray-100 dark:bg-gray-700 px-2 py-1 rounded" x-text="`/platforms/${platform?.path_prefix}/`"></code>
                    <span x-show="!platform?.path_prefix" class="text-gray-400 dark:text-gray-500 text-sm">Root path</span>
                </div>
                <div class="flex justify-between items-center py-2">
                    <span class="text-gray-600 dark:text-gray-400">Supported Languages</span>
                    <div class="flex gap-1">
                        <template x-for="lang in (platform?.supported_languages || [])" :key="lang">
                            <span class="text-xs bg-gray-100 dark:bg-gray-700 px-2 py-1 rounded" x-text="lang.toUpperCase()"></span>
                        </template>
                    </div>
                </div>
            </div>
        </div>

        <!-- Branding -->
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow-md p-6">
            <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4">Branding</h3>
            <div class="space-y-3">
                <div class="flex justify-between items-center py-2 border-b border-gray-200 dark:border-gray-700">
                    <span class="text-gray-600 dark:text-gray-400">Logo</span>
                    <template x-if="platform?.logo">
                        <img :src="platform.logo" alt="Logo" class="h-8 max-w-32 object-contain">
                    </template>
                    <span x-show="!platform?.logo" class="text-gray-400 dark:text-gray-500 text-sm">Not configured</span>
                </div>
                <div class="flex justify-between items-center py-2 border-b border-gray-200 dark:border-gray-700">
                    <span class="text-gray-600 dark:text-gray-400">Logo (Dark)</span>
                    <template x-if="platform?.logo_dark">
                        <img :src="platform.logo_dark" alt="Logo Dark" class="h-8 max-w-32 object-contain bg-gray-800 rounded p-1">
                    </template>
                    <span x-show="!platform?.logo_dark" class="text-gray-400 dark:text-gray-500 text-sm">Not configured</span>
                </div>
                <div class="flex justify-between items-center py-2">
                    <span class="text-gray-600 dark:text-gray-400">Favicon</span>
                    <template x-if="platform?.favicon">
                        <img :src="platform.favicon" alt="Favicon" class="h-6 w-6 object-contain">
                    </template>
                    <span x-show="!platform?.favicon" class="text-gray-400 dark:text-gray-500 text-sm">Not configured</span>
                </div>
            </div>
        </div>
    </div>

    <!-- Timestamps -->
    <div class="text-sm text-gray-500 dark:text-gray-400">
        <p>Created: <span x-text="formatDate(platform?.created_at)"></span></p>
        <p>Last Updated: <span x-text="formatDate(platform?.updated_at)"></span></p>
    </div>
</div>
{% endblock %}

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