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

{% block title %}Platforms{% endblock %}

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

{% block content %}
{{ page_header('Platforms', subtitle='Manage platform configurations for OMS, Loyalty, and other business offerings') }}

<!-- Create Platform Button -->
<div class="flex justify-end mb-6">
    <a href="/admin/platforms/create"
       class="inline-flex items-center px-4 py-2 text-sm font-medium text-white bg-purple-600 rounded-lg hover:bg-purple-700 transition-colors">
        <span x-html="$icon('plus', 'w-5 h-5 mr-2')"></span>
        Create Platform
    </a>
</div>

{{ loading_state('Loading platforms...') }}

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

<!-- Platforms Grid -->
<div x-show="!loading && platforms.length > 0" class="grid gap-6 mb-8 md:grid-cols-2 xl:grid-cols-3">
    <template x-for="platform in platforms" :key="platform.id">
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow-md overflow-hidden hover:shadow-lg transition-shadow">
            <!-- Platform Header -->
            <div class="p-6 border-b border-gray-200 dark:border-gray-700">
                <div class="flex items-center justify-between">
                    <div class="flex items-center">
                        <!-- Platform Icon -->
                        <div class="flex items-center justify-center w-12 h-12 rounded-lg bg-purple-100 dark:bg-purple-900/50">
                            <span x-html="$icon(getPlatformIcon(platform.code), 'w-6 h-6 text-purple-600 dark:text-purple-400')"></span>
                        </div>
                        <div class="ml-4">
                            <h3 class="text-lg font-semibold text-gray-900 dark:text-white" x-text="platform.name"></h3>
                            <code class="text-xs bg-gray-100 dark:bg-gray-700 px-2 py-0.5 rounded" x-text="platform.code"></code>
                        </div>
                    </div>
                    <!-- Status Badge -->
                    <span
                        class="px-3 py-1 text-xs font-semibold rounded-full"
                        :class="platform.is_active ? 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200' : 'bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300'"
                        x-text="platform.is_active ? 'Active' : 'Inactive'"
                    ></span>
                </div>
                <p class="mt-3 text-sm text-gray-600 dark:text-gray-400" x-text="platform.description || 'No description'"></p>
            </div>

            <!-- Platform Stats -->
            <div class="px-6 py-4 bg-gray-50 dark:bg-gray-800/50">
                <div class="grid grid-cols-3 gap-4 text-center">
                    <div>
                        <p class="text-2xl font-bold text-purple-600 dark:text-purple-400" x-text="platform.store_count"></p>
                        <p class="text-xs text-gray-500 dark:text-gray-400">Stores</p>
                    </div>
                    <div>
                        <p class="text-2xl font-bold text-blue-600 dark:text-blue-400" x-text="(platform.supported_languages || []).length"></p>
                        <p class="text-xs text-gray-500 dark:text-gray-400">Languages</p>
                    </div>
                    <div>
                        <p class="text-2xl font-bold" :class="platform.domain ? 'text-green-600 dark:text-green-400' : 'text-gray-400 dark:text-gray-500'" x-text="platform.domain ? '✓' : '—'"></p>
                        <p class="text-xs text-gray-500 dark:text-gray-400">Domain</p>
                    </div>
                </div>
            </div>

            <!-- Platform Info -->
            <div class="px-6 py-4 border-t border-gray-200 dark:border-gray-700">
                <div class="space-y-2 text-sm">
                    <div class="flex justify-between" x-show="platform.domain">
                        <span class="text-gray-500 dark:text-gray-400">Domain:</span>
                        <span class="text-gray-900 dark:text-white" x-text="platform.domain"></span>
                    </div>
                    <div class="flex justify-between" x-show="platform.path_prefix">
                        <span class="text-gray-500 dark:text-gray-400">Path Prefix:</span>
                        <code class="text-xs bg-gray-100 dark:bg-gray-700 px-2 py-0.5 rounded" x-text="platform.path_prefix"></code>
                    </div>
                    <div class="flex justify-between">
                        <span class="text-gray-500 dark:text-gray-400">Language:</span>
                        <span class="text-gray-900 dark:text-white" x-text="platform.default_language.toUpperCase()"></span>
                    </div>
                </div>
            </div>

            <!-- Platform Actions -->
            <div class="px-6 py-4 border-t border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-800/50">
                <div class="flex justify-between items-center">
                    <div class="flex items-center space-x-3">
                        <a
                            :href="`/admin/platforms/${platform.code}`"
                            class="inline-flex items-center text-sm font-medium text-purple-600 hover:text-purple-700 dark:text-purple-400 dark:hover:text-purple-300"
                        >
                            <span x-html="$icon('eye', 'w-4 h-4 mr-1')"></span>
                            View Details
                        </a>
                        <a
                            :href="`/admin/platforms/${platform.code}/edit`"
                            class="inline-flex items-center text-sm font-medium text-gray-600 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-300"
                        >
                            <span x-html="$icon('pencil', 'w-4 h-4 mr-1')"></span>
                            Edit
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </template>
</div>

<!-- Empty State -->
<div x-show="!loading && platforms.length === 0" class="text-center py-12 bg-white dark:bg-gray-800 rounded-lg shadow-md">
    <span x-html="$icon('globe-alt', 'inline w-16 h-16 text-gray-400 mb-4')"></span>
    <h3 class="text-lg font-semibold text-gray-700 dark:text-gray-300 mb-2">No platforms found</h3>
    <p class="text-gray-500 dark:text-gray-400 mb-4">
        No platforms have been configured yet.
    </p>
</div>

{% endblock %}

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