<!--
  Familiar - Self-hosted AI Agent
  Copyright (c) 2026 George Scott Foley
  
  Licensed under the MIT License
  For commercial licensing, contact: licensing@familiar.ai
-->
{% extends "base.html" %}

{% block title %}Dashboard - Familiar Admin{% endblock %}

{% block content %}
<div class="space-y-6">
    <!-- Header -->
    <div class="flex justify-between items-center">
        <h1 class="text-2xl font-bold text-gray-800">Dashboard</h1>
        <span class="text-sm text-gray-500">Last updated: <span id="last-updated">just now</span></span>
    </div>
    
    <!-- Stats Cards -->
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
        <!-- Users -->
        <div class="bg-white rounded-lg shadow p-6">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm text-gray-500">Active Users</p>
                    <p class="text-3xl font-bold text-purple-600">{{ stats.active_users }}</p>
                </div>
                <div class="text-4xl">👥</div>
            </div>
            <p class="text-xs text-gray-400 mt-2">{{ stats.total_users }} total</p>
        </div>
        
        <!-- Messages -->
        <div class="bg-white rounded-lg shadow p-6">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm text-gray-500">Messages This Week</p>
                    <p class="text-3xl font-bold text-blue-600">{{ usage.messages_week }}</p>
                </div>
                <div class="text-4xl">💬</div>
            </div>
            <p class="text-xs text-gray-400 mt-2">{{ usage.messages_today }} today</p>
        </div>
        
        <!-- API Cost -->
        <div class="bg-white rounded-lg shadow p-6">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm text-gray-500">API Cost (MTD)</p>
                    <p class="text-3xl font-bold text-green-600">${{ "%.2f"|format(usage.api_cost_mtd) }}</p>
                </div>
                <div class="text-4xl">💰</div>
            </div>
            <p class="text-xs text-gray-400 mt-2">Budget: $60/month</p>
        </div>
        
        <!-- Uptime -->
        <div class="bg-white rounded-lg shadow p-6">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm text-gray-500">Uptime (30d)</p>
                    <p class="text-3xl font-bold text-emerald-600">{{ usage.uptime_percent }}%</p>
                </div>
                <div class="text-4xl">✅</div>
            </div>
            <p class="text-xs text-gray-400 mt-2">Target: 99.5%</p>
        </div>
    </div>
    
    <!-- Two Column Layout -->
    <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
        <!-- Recent Activity -->
        <div class="bg-white rounded-lg shadow">
            <div class="px-6 py-4 border-b border-gray-200 flex justify-between items-center">
                <h2 class="text-lg font-semibold text-gray-800">Recent Activity</h2>
                <a href="{{ url_for('admin.activity') }}" class="text-sm text-purple-600 hover:text-purple-800">View all</a>
            </div>
            <div class="p-6" id="activity-list" hx-get="{{ url_for('admin.api_activity') }}" hx-trigger="load, every 30s">
                {% include "_activity_list.html" %}
            </div>
        </div>
        
        <!-- Users by Role -->
        <div class="bg-white rounded-lg shadow">
            <div class="px-6 py-4 border-b border-gray-200 flex justify-between items-center">
                <h2 class="text-lg font-semibold text-gray-800">Users by Role</h2>
                {% if user.role.value == 'admin' %}
                <a href="{{ url_for('admin.users') }}" class="text-sm text-purple-600 hover:text-purple-800">Manage</a>
                {% endif %}
            </div>
            <div class="p-6">
                <div class="space-y-4">
                    {% for role, count in stats.by_role.items() %}
                    <div class="flex items-center justify-between">
                        <div class="flex items-center space-x-3">
                            <span class="w-3 h-3 rounded-full 
                                {% if role == 'admin' %}bg-purple-500
                                {% elif role == 'staff' %}bg-blue-500
                                {% else %}bg-gray-400{% endif %}"></span>
                            <span class="text-gray-700 capitalize">{{ role }}</span>
                        </div>
                        <span class="font-semibold text-gray-800">{{ count }}</span>
                    </div>
                    {% endfor %}
                </div>
                
                <!-- Simple bar chart -->
                <div class="mt-6 space-y-2">
                    {% set total = stats.total_users or 1 %}
                    {% for role, count in stats.by_role.items() %}
                    <div class="flex items-center space-x-2">
                        <span class="text-xs text-gray-500 w-16 capitalize">{{ role }}</span>
                        <div class="flex-1 bg-gray-200 rounded-full h-2">
                            <div class="h-2 rounded-full 
                                {% if role == 'admin' %}bg-purple-500
                                {% elif role == 'staff' %}bg-blue-500
                                {% else %}bg-gray-400{% endif %}"
                                style="width: {{ (count / total * 100)|int }}%"></div>
                        </div>
                        <span class="text-xs text-gray-500 w-8">{{ count }}</span>
                    </div>
                    {% endfor %}
                </div>
            </div>
        </div>
    </div>
    
    <!-- Quick Actions -->
    <div class="bg-white rounded-lg shadow p-6">
        <h2 class="text-lg font-semibold text-gray-800 mb-4">Quick Actions</h2>
        <div class="flex flex-wrap gap-3">
            {% if user.role.value == 'admin' %}
            <a href="{{ url_for('admin.invite_user') }}" 
               class="px-4 py-2 bg-purple-600 text-white rounded-lg hover:bg-purple-700 transition">
                ➕ Invite User
            </a>
            <a href="{{ url_for('admin.settings') }}" 
               class="px-4 py-2 bg-gray-600 text-white rounded-lg hover:bg-gray-700 transition">
                ⚙️ System Settings
            </a>
            {% endif %}
            <a href="#" 
               class="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition">
                📊 Export Report
            </a>
            <a href="#" 
               class="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition">
                📖 Documentation
            </a>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
    // Update timestamp
    setInterval(() => {
        document.getElementById('last-updated').textContent = 'just now';
    }, 30000);
</script>
{% endblock %}
