{# app/templates/admin/logs.html #}
{% extends "admin/base.html" %}
{% from 'shared/macros/pagination.html' import pagination %}
{% from 'shared/macros/alerts.html' import alert_dynamic, error_state %}
{% from 'shared/macros/headers.html' import page_header_refresh %}
{% from 'shared/macros/tabs.html' import tabs_nav, tab_button %}

{% block title %}Application Logs{% endblock %}

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

{% block content %}
{{ page_header_refresh('Application Logs') }}

{{ alert_dynamic(type='success', title='Success', message_var='successMessage', show_condition='successMessage') }}

{{ error_state('Error', show_condition='error') }}

<!-- Statistics Cards -->
<div x-show="stats" class="grid gap-6 mb-8 md:grid-cols-2 xl:grid-cols-4">
    <!-- Total Logs -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-blue-500 bg-blue-100 rounded-full dark:text-blue-100 dark:bg-blue-500">
            <span x-html="$icon('document-text', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">Total Logs (7d)</p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.total_count">0</p>
        </div>
    </div>

    <!-- Warnings -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-yellow-500 bg-yellow-100 rounded-full dark:text-yellow-100 dark:bg-yellow-500">
            <span x-html="$icon('exclamation', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">Warnings</p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.warning_count">0</p>
        </div>
    </div>

    <!-- Errors -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-red-500 bg-red-100 rounded-full dark:text-white dark:bg-red-600">
            <span x-html="$icon('x-circle', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">Errors</p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.error_count">0</p>
        </div>
    </div>

    <!-- Critical -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-purple-500 bg-purple-100 rounded-full dark:text-purple-100 dark:bg-purple-500">
            <span x-html="$icon('lightning-bolt', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">Critical</p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.critical_count">0</p>
        </div>
    </div>
</div>

<!-- Log Source Tabs -->
{% call tabs_nav() %}
    {{ tab_button('database', 'Database Logs', tab_var='logSource', icon='database', onclick="logSource = 'database'; loadLogs()") }}
    {{ tab_button('file', 'File Logs', tab_var='logSource', icon='document', onclick="logSource = 'file'; loadFileLogs()") }}
{% endcall %}

<!-- Database Logs Section -->
<div x-show="logSource === 'database'" x-transition>
    <!-- Filters -->
    <div class="bg-white dark:bg-gray-800 rounded-lg shadow-md p-6 mb-6">
        <div class="flex items-center justify-between mb-4">
            <h3 class="text-lg font-semibold text-gray-700 dark:text-gray-200">Filters</h3>
            <button
                @click="resetFilters()"
                class="text-sm text-purple-600 hover:text-purple-700 dark:text-purple-400"
            >
                Reset Filters
            </button>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
            <!-- Log Level Filter -->
            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Log Level</label>
                <select
                    x-model="filters.level"
                    @change="loadLogs()"
                    class="block w-full px-3 py-2 text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-purple-600"
                >
                    <option value="">All Levels</option>
                    <option value="WARNING">WARNING</option>
                    <option value="ERROR">ERROR</option>
                    <option value="CRITICAL">CRITICAL</option>
                </select>
            </div>

            <!-- Module Filter -->
            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Module</label>
                <input
                    type="text"
                    x-model="filters.module"
                    @keyup.enter="loadLogs()"
                    placeholder="Filter by module..."
                    class="block w-full px-3 py-2 text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-purple-600"
                />
            </div>

            <!-- Search -->
            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Search</label>
                <input
                    type="text"
                    x-model="filters.search"
                    @keyup.enter="loadLogs()"
                    placeholder="Search in messages..."
                    class="block w-full px-3 py-2 text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-purple-600"
                />
            </div>
        </div>
    </div>

    <!-- Database Logs Table -->
    <div class="bg-white dark:bg-gray-800 rounded-lg shadow-md overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full whitespace-no-wrap">
                <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">Timestamp</th>
                        <th class="px-4 py-3">Level</th>
                        <th class="px-4 py-3">Module</th>
                        <th class="px-4 py-3">Message</th>
                        <th class="px-4 py-3">Actions</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-800">
                    <template x-if="loading">
                        <tr>
                            <td colspan="5" class="px-4 py-8 text-center">
                                <span x-html="$icon('spinner', 'inline w-6 h-6 text-purple-600')"></span>
                                <p class="mt-2 text-sm text-gray-600 dark:text-gray-400">Loading logs...</p>
                            </td>
                        </tr>
                    </template>

                    <template x-if="!loading && logs.length === 0">
                        <tr>
                            <td colspan="5" class="px-4 py-8 text-center text-gray-500 dark:text-gray-400">
                                No logs found
                            </td>
                        </tr>
                    </template>

                    <template x-for="log in logs" :key="log.id">
                        <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-sm">
                                <span x-text="formatTimestamp(log.timestamp)"></span>
                            </td>
                            <td class="px-4 py-3 text-xs">
                                <span
                                    :class="{
                                        'bg-yellow-100 text-yellow-800 dark:bg-yellow-800 dark:text-yellow-100': log.level === 'WARNING',
                                        'bg-red-100 text-red-800 dark:bg-red-800 dark:text-red-100': log.level === 'ERROR',
                                        'bg-purple-100 text-purple-800 dark:bg-purple-800 dark:text-purple-100': log.level === 'CRITICAL'
                                    }"
                                    class="px-2 py-1 font-semibold leading-tight rounded-full"
                                    x-text="log.level"
                                ></span>
                            </td>
                            <td class="px-4 py-3 text-sm">
                                <span x-text="log.module || '-'"></span>
                            </td>
                            <td class="px-4 py-3 text-sm">
                                <div class="max-w-2xl truncate" x-text="log.message"></div>
                            </td>
                            <td class="px-4 py-3 text-sm">
                                <button
                                    @click="showLogDetail(log)"
                                    class="text-purple-600 hover:text-purple-700 dark:text-purple-400"
                                >
                                    <span x-html="$icon('eye', 'w-5 h-5')"></span>
                                </button>
                            </td>
                        </tr>
                    </template>
                </tbody>
            </table>
        </div>

        {{ pagination(show_condition="!loading && logs.length > 0") }}
    </div>
</div>

<!-- File Logs Section -->
<div x-show="logSource === 'file'" x-transition>
    <!-- File Selection -->
    <div class="bg-white dark:bg-gray-800 rounded-lg shadow-md p-6 mb-6">
        <div class="flex items-center justify-between mb-4">
            <h3 class="text-lg font-semibold text-gray-700 dark:text-gray-200">Log Files</h3>
            <button
                @click="loadFileLogs()"
                class="text-sm text-purple-600 hover:text-purple-700 dark:text-purple-400"
            >
                Refresh List
            </button>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Select Log File</label>
                <select
                    x-model="selectedFile"
                    @change="loadFileContent()"
                    class="block w-full px-3 py-2 text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-purple-600"
                >
                    <option value="">Select a file...</option>
                    <template x-for="file in logFiles" :key="file.filename">
                        <option :value="file.filename" x-text="`${file.filename} (${file.size_mb} MB)`"></option>
                    </template>
                </select>
            </div>

            <div class="flex items-end">
                <button
                    @click="downloadLogFile()"
                    :disabled="!selectedFile"
                    class="px-4 py-2 text-sm font-medium leading-5 text-white transition-colors duration-150 bg-green-600 border border-transparent rounded-lg hover:bg-green-700 focus:outline-none focus:shadow-outline-green disabled:opacity-50 disabled:cursor-not-allowed"
                >
                    <span x-html="$icon('download', 'inline w-4 h-4 mr-2')"></span>
                    Download File
                </button>
            </div>
        </div>
    </div>

    <!-- File Content -->
    <div x-show="fileContent" class="bg-white dark:bg-gray-800 rounded-lg shadow-md overflow-hidden">
        <div class="p-4 border-b dark:border-gray-700 flex items-center justify-between">
            <div>
                <h3 class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="selectedFile"></h3>
                <p class="text-sm text-gray-600 dark:text-gray-400">
                    Showing last <span x-text="fileContent?.lines?.length || 0"></span> lines of <span x-text="fileContent?.total_lines || 0"></span> total
                </p>
            </div>
        </div>
        <div class="p-4 bg-gray-900 overflow-x-auto">
            <pre class="text-xs text-green-400 font-mono"><code x-text="fileContent?.lines?.join('\n')"></code></pre>
        </div>
    </div>
</div>

{# noqa: FE-004 - Log detail modal with dynamic show variable and custom content layout #}
<!-- Log Detail Modal -->
<div x-show="selectedLog" x-transition class="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-50" @click.self="selectedLog = null">
    <div class="bg-white dark:bg-gray-800 rounded-lg shadow-xl max-w-4xl w-full mx-4 max-h-[90vh] overflow-hidden">
        {# Modal Header with Level Badge #}
        <div class="px-6 py-4 border-b border-gray-200 dark:border-gray-700">
            <div class="flex items-center justify-between">
                <div class="flex items-center gap-3">
                    <div :class="{
                        'bg-yellow-100 dark:bg-yellow-900/30': selectedLog?.level === 'WARNING',
                        'bg-red-100 dark:bg-red-900/30': selectedLog?.level === 'ERROR',
                        'bg-purple-100 dark:bg-purple-900/30': selectedLog?.level === 'CRITICAL',
                        'bg-blue-100 dark:bg-blue-900/30': selectedLog?.level === 'INFO' || selectedLog?.level === 'DEBUG'
                    }" class="p-2 rounded-lg">
                        <span :class="{
                            'text-yellow-600 dark:text-yellow-400': selectedLog?.level === 'WARNING',
                            'text-red-600 dark:text-red-400': selectedLog?.level === 'ERROR',
                            'text-purple-600 dark:text-purple-400': selectedLog?.level === 'CRITICAL',
                            'text-blue-600 dark:text-blue-400': selectedLog?.level === 'INFO' || selectedLog?.level === 'DEBUG'
                        }" x-html="$icon(selectedLog?.level === 'WARNING' ? 'exclamation' : selectedLog?.level === 'CRITICAL' ? 'lightning-bolt' : selectedLog?.level === 'ERROR' ? 'x-circle' : 'information-circle', 'w-6 h-6')"></span>
                    </div>
                    <div>
                        <h3 class="text-lg font-semibold text-gray-900 dark:text-gray-100">Log Entry Details</h3>
                        <p class="text-sm text-gray-500 dark:text-gray-400">ID: <span x-text="selectedLog?.id"></span></p>
                    </div>
                </div>
                <div class="flex items-center gap-3">
                    <span :class="{
                        'bg-yellow-100 text-yellow-800 dark:bg-yellow-800 dark:text-yellow-100': selectedLog?.level === 'WARNING',
                        'bg-red-100 text-red-800 dark:bg-red-800 dark:text-red-100': selectedLog?.level === 'ERROR',
                        'bg-purple-100 text-purple-800 dark:bg-purple-800 dark:text-purple-100': selectedLog?.level === 'CRITICAL',
                        'bg-blue-100 text-blue-800 dark:bg-blue-800 dark:text-blue-100': selectedLog?.level === 'INFO' || selectedLog?.level === 'DEBUG'
                    }" class="px-3 py-1 text-sm font-semibold rounded-full" x-text="selectedLog?.level"></span>
                    <button @click="selectedLog = null" class="p-1 text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 transition-colors">
                        <span x-html="$icon('close', 'w-6 h-6')"></span>
                    </button>
                </div>
            </div>
        </div>

        {# Modal Body #}
        <div class="p-6 overflow-y-auto max-h-[calc(90vh-140px)]">
            <template x-if="selectedLog">
                <div class="space-y-6">
                    {# Details Table #}
                    <div class="overflow-hidden border border-gray-200 dark:border-gray-700 rounded-lg">
                        <table class="min-w-full divide-y divide-gray-200 dark:divide-gray-700">
                            <tbody class="divide-y divide-gray-200 dark:divide-gray-700">
                                <tr>
                                    <td class="px-4 py-3 text-sm font-medium text-gray-600 dark:text-gray-400 bg-gray-50 dark:bg-gray-700/50 w-1/4">
                                        <div class="flex items-center gap-2">
                                            <span x-html="$icon('clock', 'w-4 h-4')"></span>
                                            Timestamp
                                        </div>
                                    </td>
                                    <td class="px-4 py-3 text-sm text-gray-900 dark:text-gray-100" x-text="formatTimestamp(selectedLog.timestamp)"></td>
                                </tr>
                                <tr>
                                    <td class="px-4 py-3 text-sm font-medium text-gray-600 dark:text-gray-400 bg-gray-50 dark:bg-gray-700/50">
                                        <div class="flex items-center gap-2">
                                            <span x-html="$icon('tag', 'w-4 h-4')"></span>
                                            Logger
                                        </div>
                                    </td>
                                    <td class="px-4 py-3 text-sm text-gray-900 dark:text-gray-100">
                                        <code class="px-2 py-1 bg-gray-100 dark:bg-gray-700 rounded text-xs" x-text="selectedLog.logger_name || '-'"></code>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="px-4 py-3 text-sm font-medium text-gray-600 dark:text-gray-400 bg-gray-50 dark:bg-gray-700/50">
                                        <div class="flex items-center gap-2">
                                            <span x-html="$icon('cube', 'w-4 h-4')"></span>
                                            Module
                                        </div>
                                    </td>
                                    <td class="px-4 py-3 text-sm text-gray-900 dark:text-gray-100">
                                        <code class="px-2 py-1 bg-gray-100 dark:bg-gray-700 rounded text-xs" x-text="selectedLog.module || '-'"></code>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>

                    {# Message Section #}
                    <div>
                        <div class="flex items-center gap-2 mb-2">
                            <span class="text-gray-500 dark:text-gray-400" x-html="$icon('chat-alt', 'w-4 h-4')"></span>
                            <h4 class="text-sm font-semibold text-gray-700 dark:text-gray-300">Message</h4>
                        </div>
                        <div class="bg-gray-50 dark:bg-gray-700/50 border border-gray-200 dark:border-gray-600 rounded-lg p-4">
                            <p class="text-sm text-gray-800 dark:text-gray-200 whitespace-pre-wrap break-words" x-text="selectedLog.message"></p>
                        </div>
                    </div>

                    {# Exception Section (conditional) #}
                    <div x-show="selectedLog.exception_message" x-transition>
                        <div class="flex items-center gap-2 mb-2">
                            <span class="text-red-500 dark:text-red-400" x-html="$icon('exclamation-circle', 'w-4 h-4')"></span>
                            <h4 class="text-sm font-semibold text-red-700 dark:text-red-300">Exception</h4>
                        </div>
                        <div class="bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg p-4">
                            <div class="flex items-start gap-3">
                                <div class="flex-shrink-0 p-1.5 bg-red-100 dark:bg-red-900/50 rounded">
                                    <span class="text-red-600 dark:text-red-400" x-html="$icon('x-circle', 'w-5 h-5')"></span>
                                </div>
                                <div class="flex-1 min-w-0">
                                    <p class="text-sm font-medium text-red-800 dark:text-red-200" x-text="selectedLog.exception_type"></p>
                                    <p class="text-sm text-red-600 dark:text-red-300 mt-1 break-words" x-text="selectedLog.exception_message"></p>
                                </div>
                            </div>
                        </div>
                    </div>

                    {# Stack Trace Section (conditional) #}
                    <div x-show="selectedLog.stack_trace" x-transition>
                        <div class="flex items-center justify-between mb-2">
                            <div class="flex items-center gap-2">
                                <span class="text-gray-500 dark:text-gray-400" x-html="$icon('code', 'w-4 h-4')"></span>
                                <h4 class="text-sm font-semibold text-gray-700 dark:text-gray-300">Stack Trace</h4>
                            </div>
                            <button
                                @click="navigator.clipboard.writeText(selectedLog.stack_trace); $dispatch('notify', {message: 'Stack trace copied!', type: 'success'})"
                                class="text-xs text-purple-600 hover:text-purple-700 dark:text-purple-400 dark:hover:text-purple-300 flex items-center gap-1"
                            >
                                <span x-html="$icon('clipboard-copy', 'w-4 h-4')"></span>
                                Copy
                            </button>
                        </div>
                        <div class="bg-gray-900 dark:bg-gray-950 border border-gray-700 rounded-lg overflow-hidden">
                            <pre class="p-4 text-xs text-green-400 font-mono overflow-x-auto max-h-64 overflow-y-auto"><code x-text="selectedLog.stack_trace"></code></pre>
                        </div>
                    </div>
                </div>
            </template>
        </div>

        {# Modal Footer #}
        <div class="px-6 py-4 border-t border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-700/30">
            <div class="flex justify-end">
                <button
                    @click="selectedLog = null"
                    class="px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-600 focus:outline-none focus:ring-2 focus:ring-purple-500 transition-colors"
                >
                    Close
                </button>
            </div>
        </div>
    </div>
</div>

{% endblock %}

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