{% extends "superadmin/base.html" %}
{% load humanize %}

{% block superadmin_content %}
<div class="flex justify-between items-center mb-6">
    <div>
        <h2 style="font-size: var(--font-size-xl); font-weight: 700;">Platform Analytics & Leaderboards</h2>
        <p class="text-sm text-secondary">Top performing Ghanaian businesses and flash sale products</p>
    </div>
</div>

<div class="grid grid--2 gap-6 mb-8">
    <!-- Top Businesses Leaderboard -->
    <div class="dash-card">
        <div class="dash-card__header"><h3 class="dash-card__title">Top Businesses by Gross Sales Revenue</h3></div>
        <div class="dash-card__body">
            {% if top_businesses %}
            <div class="flex flex-col gap-4">
                {% for biz in top_businesses %}
                <div class="card card--flat flex items-center justify-between">
                    <div class="flex items-center gap-3">
                        <span class="font-bold text-lg" style="color:var(--color-primary);min-width:24px;">#{{ forloop.counter }}</span>
                        <div>
                            <a href="{% url 'superadmin:business_detail' biz.id %}" class="font-bold text-base" style="color:var(--color-text);">{{ biz.name }}</a>
                            <p class="text-xs text-muted">{{ biz.sales_count }} Sales Run &bull; {{ biz.paid_orders }} Paid Orders</p>
                        </div>
                    </div>
                    <div class="text-right">
                        <strong style="font-size:var(--font-size-lg);color:var(--color-primary);">GHS {{ biz.total_revenue|default:0|floatformat:2|intcomma }}</strong>
                        <span class="text-xs text-muted block">Gross Revenue</span>
                    </div>
                </div>
                {% endfor %}
            </div>
            {% else %}
            <p class="text-sm text-muted">No business data yet.</p>
            {% endif %}
        </div>
    </div>

    <!-- Top Selling Products -->
    <div class="dash-card">
        <div class="dash-card__header"><h3 class="dash-card__title">Top Selling Products Across Flash Sales</h3></div>
        <div class="dash-card__body">
            {% if top_products %}
            <div class="flex flex-col gap-4">
                {% for prod in top_products %}
                <div class="card card--flat flex items-center justify-between">
                    <div class="flex items-center gap-3">
                        {% if prod.primary_image %}
                        <img src="{{ prod.primary_image.image.url }}" alt="" style="width:48px;height:48px;border-radius:var(--radius-sm);object-fit:cover;">
                        {% else %}
                        <div style="width:48px;height:48px;border-radius:var(--radius-sm);background:var(--color-border-light);display:flex;align-items:center;justify-content:center;color:var(--color-text-muted);font-size:11px;">No img</div>
                        {% endif %}
                        <div>
                            <h4 class="font-bold text-sm" style="color:var(--color-text);">{{ prod.name }}</h4>
                            <p class="text-xs text-muted">By {{ prod.business.name }}</p>
                        </div>
                    </div>
                    <div class="text-right">
                        <strong style="font-size:var(--font-size-base);color:var(--color-primary);">GHS {{ prod.gross_revenue|default:0|floatformat:2|intcomma }}</strong>
                        <span class="text-xs text-muted block">{{ prod.units_sold }} units sold</span>
                    </div>
                </div>
                {% endfor %}
            </div>
            {% else %}
            <p class="text-sm text-muted">No product sales data yet.</p>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
