{% extends "superadmin/base_superadmin.html" %}

{% block page_title %}System Overview{% endblock %}
{% block page_description %}Complete system statistics and monitoring{% endblock %}

{% block superadmin_content %}
<!-- Stats Overview -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
    <!-- Total Members -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
        <div class="flex items-center">
            <div class="flex-shrink-0">
                <div class="w-12 h-12 bg-blue-100 rounded-xl flex items-center justify-center">
                    <i class="fas fa-users text-blue-600 text-xl"></i>
                </div>
            </div>
            <div class="ml-4">
                <p class="text-sm font-medium text-gray-500">Total Members</p>
                <p class="text-2xl font-bold text-gray-900">{{ total_members }}</p>
            </div>
        </div>
        <div class="mt-4 flex items-center">
            <span class="text-sm text-gray-500">Active users in system</span>
        </div>
    </div>

    <!-- Total Admins -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
        <div class="flex items-center">
            <div class="flex-shrink-0">
                <div class="w-12 h-12 bg-purple-100 rounded-xl flex items-center justify-center">
                    <i class="fas fa-user-shield text-purple-600 text-xl"></i>
                </div>
            </div>
            <div class="ml-4">
                <p class="text-sm font-medium text-gray-500">Administrators</p>
                <p class="text-2xl font-bold text-gray-900">{{ total_admins }}</p>
            </div>
        </div>
        <div class="mt-4 flex items-center">
            <span class="text-sm text-gray-500">{{ total_superadmins }} superadmin(s)</span>
        </div>
    </div>

    <!-- Total Contributions -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
        <div class="flex items-center">
            <div class="flex-shrink-0">
                <div class="w-12 h-12 bg-green-100 rounded-xl flex items-center justify-center">
                    <i class="fas fa-money-bill-wave text-green-600 text-xl"></i>
                </div>
            </div>
            <div class="ml-4">
                <p class="text-sm font-medium text-gray-500">Total Savings</p>
                <p class="text-2xl font-bold text-gray-900">৳{{ "{:,.0f}".format(total_contributions) }}</p>
            </div>
        </div>
        <div class="mt-4 flex items-center">
            <span class="text-sm text-gray-500">{{ total_contribution_count }} transactions</span>
        </div>
    </div>

    <!-- Active Events -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
        <div class="flex items-center">
            <div class="flex-shrink-0">
                <div class="w-12 h-12 bg-orange-100 rounded-xl flex items-center justify-center">
                    <i class="fas fa-calendar-alt text-orange-600 text-xl"></i>
                </div>
            </div>
            <div class="ml-4">
                <p class="text-sm font-medium text-gray-500">Active Events</p>
                <p class="text-2xl font-bold text-gray-900">{{ active_events }}</p>
            </div>
        </div>
        <div class="mt-4 flex items-center">
            <span class="text-sm text-gray-500">{{ events_count }} total events</span>
        </div>
    </div>
</div>

<!-- System Status Cards -->
<div class="grid grid-cols-1 lg:grid-cols-3 gap-6 mb-8">
    <!-- Database Statistics -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
        <div class="flex items-center justify-between mb-4">
            <h3 class="text-lg font-semibold text-gray-900">Database Status</h3>
            <div class="w-3 h-3 bg-green-500 rounded-full animate-pulse"></div>
        </div>
        <div class="space-y-3">
            <div class="flex justify-between items-center">
                <span class="text-sm text-gray-600">Members Table</span>
                <span class="text-sm font-semibold text-gray-900">{{ total_members }} records</span>
            </div>
            <div class="flex justify-between items-center">
                <span class="text-sm text-gray-600">Contributions</span>
                <span class="text-sm font-semibold text-gray-900">{{ total_contribution_count }} records</span>
            </div>
            <div class="flex justify-between items-center">
                <span class="text-sm text-gray-600">Events</span>
                <span class="text-sm font-semibold text-gray-900">{{ events_count }} records</span>
            </div>
            <div class="flex justify-between items-center">
                <span class="text-sm text-gray-600">Currencies</span>
                <span class="text-sm font-semibold text-gray-900">{{ currencies_count }} records</span>
            </div>
        </div>
    </div>

    <!-- Monthly Performance -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
        <div class="flex items-center justify-between mb-4">
            <h3 class="text-lg font-semibold text-gray-900">This Month</h3>
            <i class="fas fa-chart-line text-green-500"></i>
        </div>
        <div class="space-y-3">
            <div>
                <div class="flex justify-between items-center mb-1">
                    <span class="text-sm text-gray-600">Contributions</span>
                    <span class="text-sm font-semibold text-gray-900">৳{{ "{:,.0f}".format(monthly_contributions) }}</span>
                </div>
                <div class="w-full bg-gray-200 rounded-full h-2">
                    <div class="bg-green-500 h-2 rounded-full" style="width: {{ (monthly_contributions / total_contributions * 100) if total_contributions > 0 else 0 }}%"></div>
                </div>
            </div>
            <div class="pt-2">
                <span class="text-xs text-gray-500">Performance compared to total</span>
            </div>
        </div>
    </div>

    <!-- Quick Actions -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
        <h3 class="text-lg font-semibold text-gray-900 mb-4">Quick Actions</h3>
        <div class="space-y-3">
            <a href="{{ url_for('superadmin_users') }}" 
               class="flex items-center p-3 rounded-lg border border-gray-200 hover:bg-gray-50 transition-colors duration-200">
                <i class="fas fa-users-cog text-blue-500 mr-3"></i>
                <span class="text-sm font-medium text-gray-700">Manage Users</span>
            </a>
            <a href="{{ url_for('superadmin_system') }}" 
               class="flex items-center p-3 rounded-lg border border-gray-200 hover:bg-gray-50 transition-colors duration-200">
                <i class="fas fa-server text-purple-500 mr-3"></i>
                <span class="text-sm font-medium text-gray-700">System Settings</span>
            </a>
            <a href="{{ url_for('superadmin_analytics') }}" 
               class="flex items-center p-3 rounded-lg border border-gray-200 hover:bg-gray-50 transition-colors duration-200">
                <i class="fas fa-chart-bar text-green-500 mr-3"></i>
                <span class="text-sm font-medium text-gray-700">View Analytics</span>
            </a>
        </div>
    </div>
</div>

<!-- Recent Activity -->
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
    <!-- Recent Members -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
        <div class="flex items-center justify-between mb-4">
            <h3 class="text-lg font-semibold text-gray-900">Recent Members</h3>
            <a href="{{ url_for('superadmin_users') }}" class="text-sm text-blue-600 hover:text-blue-800">View all</a>
        </div>
        <div class="space-y-3">
            {% for member in recent_members %}
            <div class="flex items-center space-x-3 p-3 rounded-lg border border-gray-100">
                <div class="w-10 h-10 bg-blue-100 rounded-full flex items-center justify-center">
                    <span class="text-blue-600 font-semibold text-sm">{{ member.full_name[0] if member.full_name else 'M' }}</span>
                </div>
                <div class="flex-1 min-w-0">
                    <p class="text-sm font-medium text-gray-900 truncate">{{ member.full_name }}</p>
                    <p class="text-xs text-gray-500">{{ member.custom_id or 'No ID' }} • {{ member.email }}</p>
                </div>
                <div class="flex items-center space-x-2">
                    {% if member.is_superadmin %}
                        <span class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-red-100 text-red-800">
                            <i class="fas fa-crown mr-1"></i>Super
                        </span>
                    {% elif member.is_admin %}
                        <span class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-purple-100 text-purple-800">
                            <i class="fas fa-shield-alt mr-1"></i>Admin
                        </span>
                    {% else %}
                        <span class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-blue-100 text-blue-800">
                            <i class="fas fa-user mr-1"></i>Member
                        </span>
                    {% endif %}
                </div>
            </div>
            {% endfor %}
        </div>
    </div>

    <!-- Recent Contributions -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
        <div class="flex items-center justify-between mb-4">
            <h3 class="text-lg font-semibold text-gray-900">Recent Contributions</h3>
            <a href="{{ url_for('admin_contributions') }}" class="text-sm text-blue-600 hover:text-blue-800">View all</a>
        </div>
        <div class="space-y-3">
            {% for contribution in recent_contributions %}
            <div class="flex items-center justify-between p-3 rounded-lg border border-gray-100">
                <div class="flex items-center space-x-3">
                    <div class="w-8 h-8 bg-green-100 rounded-full flex items-center justify-center">
                        <i class="fas fa-coins text-green-600 text-sm"></i>
                    </div>
                    <div>
                        <p class="text-sm font-medium text-gray-900">{{ contribution.member.full_name }}</p>
                        <p class="text-xs text-gray-500">{{ contribution.contribution_type.title() }}</p>
                    </div>
                </div>
                <div class="text-right">
                    <p class="text-sm font-semibold text-gray-900">৳{{ "{:,.0f}".format(contribution.amount) }}</p>
                    <p class="text-xs text-gray-500">{{ contribution.created_at.strftime('%b %d') }}</p>
                </div>
            </div>
            {% endfor %}
        </div>
    </div>
</div>
{% endblock %}