{% extends "admin/base_site.html" %}
{% load static %}

{% block content %}
<div class="px-6 py-4">
    <!-- Page Header -->
    <div class="mb-8">
        <h1 class="text-3xl font-bold text-gray-900 dark:text-white mb-2">IGNY8 Dashboard</h1>
        <p class="text-gray-600 dark:text-gray-400">Operational metrics and system health</p>
    </div>

    <!-- Alerts Section -->
    {% if alerts %}
    <div class="mb-8 space-y-3">
        {% for alert in alerts %}
        <div class="{% if alert.level == 'error' %}bg-red-50 border-l-4 border-red-500{% elif alert.level == 'warning' %}bg-yellow-50 border-l-4 border-yellow-500{% else %}bg-blue-50 border-l-4 border-blue-500{% endif %} p-4 rounded-r-lg">
            <div class="flex items-center justify-between">
                <div class="flex items-center">
                    <span class="{% if alert.level == 'error' %}text-red-800{% elif alert.level == 'warning' %}text-yellow-800{% else %}text-blue-800{% endif %} font-medium">
                        {{ alert.message }}
                    </span>
                </div>
                <a href="{{ alert.url }}" class="{% if alert.level == 'error' %}text-red-600 hover:text-red-800{% elif alert.level == 'warning' %}text-yellow-600 hover:text-yellow-800{% else %}text-blue-600 hover:text-blue-800{% endif %} font-medium underline">
                    {{ alert.action }} →
                </a>
            </div>
        </div>
        {% endfor %}
    </div>
    {% endif %}

    <!-- Metrics Grid -->
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
        <!-- Accounts Overview -->
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
            <div class="flex items-center justify-between mb-4">
                <h3 class="text-lg font-semibold text-gray-900 dark:text-white">Accounts</h3>
                <svg class="w-8 h-8 text-blue-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"></path>
                </svg>
            </div>
            <div class="space-y-2">
                <div class="flex justify-between">
                    <span class="text-gray-600 dark:text-gray-400">Total</span>
                    <span class="font-bold text-gray-900 dark:text-white">{{ total_accounts }}</span>
                </div>
                <div class="flex justify-between">
                    <span class="text-gray-600 dark:text-gray-400">Active</span>
                    <span class="font-bold text-green-600">{{ active_accounts }}</span>
                </div>
                {% if critical_credit_accounts > 0 %}
                <div class="flex justify-between">
                    <span class="text-gray-600 dark:text-gray-400">Critical Credits</span>
                    <span class="font-bold text-red-600">{{ critical_credit_accounts }}</span>
                </div>
                {% endif %}
                {% if low_credit_accounts > 0 %}
                <div class="flex justify-between">
                    <span class="text-gray-600 dark:text-gray-400">Low Credits</span>
                    <span class="font-bold text-yellow-600">{{ low_credit_accounts }}</span>
                </div>
                {% endif %}
            </div>
            <a href="/admin/igny8_core_auth/account/" class="mt-4 inline-block text-blue-600 hover:text-blue-800 font-medium text-sm">View All →</a>
        </div>

        <!-- Content Production -->
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
            <div class="flex items-center justify-between mb-4">
                <h3 class="text-lg font-semibold text-gray-900 dark:text-white">Content</h3>
                <svg class="w-8 h-8 text-green-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
                </svg>
            </div>
            <div class="space-y-2">
                <div class="flex justify-between">
                    <span class="text-gray-600 dark:text-gray-400">This Week</span>
                    <span class="font-bold text-gray-900 dark:text-white">{{ content_this_week }}</span>
                </div>
                <div class="flex justify-between">
                    <span class="text-gray-600 dark:text-gray-400">This Month</span>
                    <span class="font-bold text-gray-900 dark:text-white">{{ content_this_month }}</span>
                </div>
                <div class="flex justify-between">
                    <span class="text-gray-600 dark:text-gray-400">Tasks Pending</span>
                    <span class="font-bold text-yellow-600">{{ tasks_pending }}</span>
                </div>
                <div class="flex justify-between">
                    <span class="text-gray-600 dark:text-gray-400">In Progress</span>
                    <span class="font-bold text-blue-600">{{ tasks_in_progress }}</span>
                </div>
            </div>
            <a href="/admin/writer/content/" class="mt-4 inline-block text-blue-600 hover:text-blue-800 font-medium text-sm">View All →</a>
        </div>

        <!-- Billing Overview -->
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
            <div class="flex items-center justify-between mb-4">
                <h3 class="text-lg font-semibold text-gray-900 dark:text-white">Billing</h3>
                <svg class="w-8 h-8 text-yellow-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                </svg>
            </div>
            <div class="space-y-2">
                {% if pending_payments > 0 %}
                <div class="flex justify-between">
                    <span class="text-gray-600 dark:text-gray-400">Pending Approval</span>
                    <span class="font-bold text-yellow-600">{{ pending_payments }}</span>
                </div>
                {% endif %}
                <div class="flex justify-between">
                    <span class="text-gray-600 dark:text-gray-400">Revenue (30d)</span>
                    <span class="font-bold text-green-600">${{ payments_this_month|floatformat:2 }}</span>
                </div>
                <div class="flex justify-between">
                    <span class="text-gray-600 dark:text-gray-400">Credits Used (30d)</span>
                    <span class="font-bold text-gray-900 dark:text-white">{{ credit_usage_this_month|floatformat:0 }}</span>
                </div>
            </div>
            <a href="/admin/billing/payment/" class="mt-4 inline-block text-blue-600 hover:text-blue-800 font-medium text-sm">View Payments →</a>
        </div>

        <!-- Automation Status -->
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
            <div class="flex items-center justify-between mb-4">
                <h3 class="text-lg font-semibold text-gray-900 dark:text-white">Automation</h3>
                <svg class="w-8 h-8 text-purple-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"></path>
                </svg>
            </div>
            <div class="space-y-2">
                {% if automation_running > 0 %}
                <div class="flex justify-between">
                    <span class="text-gray-600 dark:text-gray-400">Running Now</span>
                    <span class="font-bold text-blue-600">{{ automation_running }}</span>
                </div>
                {% endif %}
                <div class="flex justify-between">
                    <span class="text-gray-600 dark:text-gray-400">Success Rate (7d)</span>
                    <span class="font-bold {% if automation_success_rate >= 90 %}text-green-600{% elif automation_success_rate >= 70 %}text-yellow-600{% else %}text-red-600{% endif %}">{{ automation_success_rate }}%</span>
                </div>
                {% if automation_failed > 0 %}
                <div class="flex justify-between">
                    <span class="text-gray-600 dark:text-gray-400">Failed (7d)</span>
                    <span class="font-bold text-red-600">{{ automation_failed }}</span>
                </div>
                {% endif %}
            </div>
            <a href="/admin/automation/automationrun/" class="mt-4 inline-block text-blue-600 hover:text-blue-800 font-medium text-sm">View Runs →</a>
        </div>
    </div>

    <!-- Secondary Metrics Grid -->
    <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
        <!-- Integration Health -->
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
            <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4">Integration Health</h3>
            <div class="space-y-3">
                <div>
                    <div class="flex justify-between mb-1">
                        <span class="text-sm text-gray-600 dark:text-gray-400">Syncs Today</span>
                        <span class="text-sm font-medium text-gray-900 dark:text-white">{{ sync_success_today }} success</span>
                    </div>
                    {% if sync_failed_today > 0 %}
                    <div class="flex justify-between">
                        <span class="text-sm text-gray-600 dark:text-gray-400"></span>
                        <span class="text-sm font-medium text-red-600">{{ sync_failed_today }} failed</span>
                    </div>
                    {% endif %}
                </div>
            </div>
            <a href="/admin/integration/syncevent/" class="mt-4 inline-block text-blue-600 hover:text-blue-800 font-medium text-sm">View Sync Events →</a>
        </div>

        <!-- Celery Tasks -->
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
            <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4">Background Tasks</h3>
            <div class="space-y-2">
                {% if celery_pending > 0 %}
                <div class="flex justify-between">
                    <span class="text-gray-600 dark:text-gray-400">Pending</span>
                    <span class="font-bold text-yellow-600">{{ celery_pending }}</span>
                </div>
                {% endif %}
                {% if celery_failed > 0 %}
                <div class="flex justify-between">
                    <span class="text-gray-600 dark:text-gray-400">Failed (7d)</span>
                    <span class="font-bold text-red-600">{{ celery_failed }}</span>
                </div>
                {% endif %}
                {% if celery_pending == 0 and celery_failed == 0 %}
                <div class="text-center py-4">
                    <span class="text-green-600 font-medium">✓ All tasks healthy</span>
                </div>
                {% endif %}
            </div>
            <a href="/admin/django_celery_results/taskresult/" class="mt-4 inline-block text-blue-600 hover:text-blue-800 font-medium text-sm">View Tasks →</a>
        </div>

        <!-- Sites Overview -->
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
            <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4">Sites</h3>
            <div class="space-y-2">
                <div class="flex justify-between">
                    <span class="text-gray-600 dark:text-gray-400">Total Sites</span>
                    <span class="font-bold text-gray-900 dark:text-white">{{ total_sites }}</span>
                </div>
                <div class="flex justify-between">
                    <span class="text-gray-600 dark:text-gray-400">Active Sites</span>
                    <span class="font-bold text-green-600">{{ active_sites }}</span>
                </div>
            </div>
            <a href="/admin/igny8_core_auth/site/" class="mt-4 inline-block text-blue-600 hover:text-blue-800 font-medium text-sm">View Sites →</a>
        </div>
    </div>
</div>
{% endblock %}
