{% extends "admin/base.html" %}
{% block title %}Dashboard{% endblock %}
{% block content %}
<h2 class="mb-4"><i class="bi bi-shield-fill-check text-danger me-2"></i>Site Admin Dashboard</h2>

<div class="row g-3 mb-4">
    <div class="col-md-3">
        <div class="card border-0 bg-secondary bg-opacity-25">
            <div class="card-body text-center">
                <div class="fs-2 fw-bold text-danger">{{ stats.group_count }}</div>
                <div class="text-muted">Groups</div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card border-0 bg-secondary bg-opacity-25">
            <div class="card-body text-center">
                <div class="fs-2 fw-bold text-warning">{{ stats.user_count }}</div>
                <div class="text-muted">Users</div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card border-0 bg-secondary bg-opacity-25">
            <div class="card-body text-center">
                <div class="fs-2 fw-bold text-success">{{ stats.db_configured }}</div>
                <div class="text-muted">DBs configured</div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card border-0 bg-secondary bg-opacity-25">
            <div class="card-body text-center">
                <div class="fs-2 fw-bold text-info">{{ stats.admin_count }}</div>
                <div class="text-muted">Site Admins</div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card border-0 bg-secondary bg-opacity-25">
            <div class="card-body text-center">
                <div class="fs-2 fw-bold {{ 'text-success' if stats.mail_configured else 'text-secondary' }}">{{ stats.mail_configured }}</div>
                <div class="text-muted">Mail configured</div>
            </div>
        </div>
    </div>
</div>

<div class="row g-3">
    <div class="col-md-6">
        <div class="card border-secondary">
            <div class="card-header d-flex justify-content-between align-items-center">
                <span><i class="bi bi-collection-fill me-2"></i>Groups</span>
                <a href="{{ url_for('site_admin.group_new') }}" class="btn btn-sm btn-success">
                    <i class="bi bi-plus-lg"></i> New
                </a>
            </div>
            <div class="card-body p-0">
                <table class="table table-hover mb-0">
                    <thead><tr><th>Name</th><th>Members</th><th>DB</th><th></th></tr></thead>
                    <tbody>
                    {% for g in groups %}
                    <tr>
                        <td>{{ g.name }}</td>
                        <td>{{ g.member_count }}</td>
                        <td>
                            {% if g.has_db %}
                            <span class="badge bg-success">Configured</span>
                            {% else %}
                            <span class="badge bg-secondary">None</span>
                            {% endif %}
                        </td>
                        <td class="text-end">
                            <form method="post" action="{{ url_for('site_admin.view_group', group_id=g.id) }}" class="d-inline">
                                <input type="hidden" name="_csrf_token" value="{{ csrf_token() }}">
                                <button type="submit" class="btn btn-sm btn-outline-info" title="Browse">
                                    <i class="bi bi-eye"></i>
                                </button>
                            </form>
                            <a href="{{ url_for('site_admin.group_edit', group_id=g.id) }}" class="btn btn-sm btn-outline-secondary" title="Edit">
                                <i class="bi bi-pencil"></i>
                            </a>
                        </td>
                    </tr>
                    {% else %}
                    <tr><td colspan="4" class="text-muted text-center py-3">No groups yet</td></tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
            <div class="card-footer text-end">
                <a href="{{ url_for('site_admin.groups') }}" class="text-muted small">All groups →</a>
            </div>
        </div>
    </div>

    <div class="col-md-6">
        <div class="card border-secondary">
            <div class="card-header d-flex justify-content-between align-items-center">
                <span><i class="bi bi-people-fill me-2"></i>Users</span>
                <a href="{{ url_for('site_admin.user_new') }}" class="btn btn-sm btn-success">
                    <i class="bi bi-plus-lg"></i> New
                </a>
            </div>
            <div class="card-body p-0">
                <table class="table table-hover mb-0">
                    <thead><tr><th>User</th><th>Groups</th><th>Admin</th><th></th></tr></thead>
                    <tbody>
                    {% for u in users %}
                    <tr>
                        <td>{{ u.username }}</td>
                        <td>{{ u.group_count }}</td>
                        <td>{% if u.is_site_admin %}<span class="badge bg-danger"><i class="bi bi-shield-fill"></i></span>{% endif %}</td>
                        <td class="text-end">
                            <a href="{{ url_for('site_admin.user_edit', user_id=u.id) }}" class="btn btn-sm btn-outline-secondary" title="Edit">
                                <i class="bi bi-pencil"></i>
                            </a>
                        </td>
                    </tr>
                    {% else %}
                    <tr><td colspan="4" class="text-muted text-center py-3">No users yet</td></tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
            <div class="card-footer text-end">
                <a href="{{ url_for('site_admin.users') }}" class="text-muted small">All users →</a>
            </div>
        </div>
    </div>
</div>

<!-- ── Recent Audit Activity ──────────────────────────────── -->
<div class="row g-3 mt-1">
    <div class="col-12">
        <div class="card border-secondary">
            <div class="card-header d-flex justify-content-between align-items-center">
                <span><i class="bi bi-journal-text me-2"></i>Recent Audit Activity</span>
                <div class="d-flex gap-2 align-items-center">
                    {% if retention_days > 0 %}
                    <span class="badge bg-secondary small">
                        <i class="bi bi-clock-history me-1"></i>Retention: {{ retention_days }}d
                    </span>
                    {% endif %}
                    <a href="{{ url_for('site_admin.audit_log') }}" class="btn btn-sm btn-outline-secondary">
                        <i class="bi bi-arrow-right-circle me-1"></i>Full log
                    </a>
                </div>
            </div>
            <div class="card-body p-0">
                <table class="table table-dark table-hover table-sm align-middle mb-0">
                    <thead class="table-secondary text-dark">
                        <tr>
                            <th style="width:155px">Timestamp (UTC)</th>
                            <th style="width:130px">Actor</th>
                            <th style="width:180px">Action</th>
                            <th style="width:120px">Group</th>
                            <th>Details</th>
                            <th style="width:110px">IP Address</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for row in recent_audit %}
                        {% set action_class = {
                            'user.login':                'badge bg-success',
                            'user.login_failed':         'badge bg-danger',
                            'user.password_changed':     'badge bg-warning text-dark',
                            'session.logout':            'badge bg-secondary',
                            'admin.login':               'badge bg-warning text-dark',
                            'admin.login_failed':        'badge bg-danger',
                            'admin.view_users':          'badge bg-dark border border-secondary',
                            'admin.view_user':           'badge bg-dark border border-secondary',
                            'admin.view_group':          'badge bg-dark border border-secondary',
                            'admin.view_group_members':  'badge bg-dark border border-secondary',
                            'admin.view_audit_log':      'badge bg-dark border border-secondary',
                            'invite.created':            'badge bg-primary',
                            'invite.accepted':           'badge bg-success',
                            'invite.revoked':            'badge bg-secondary',
                            'invite.resent':             'badge bg-info text-dark',
                            'member.added':              'badge bg-primary',
                            'member.removed':            'badge bg-danger',
                            'member.role_changed':       'badge bg-warning text-dark',
                            'group.created':             'badge bg-success',
                            'group.updated':             'badge bg-secondary',
                            'group.deleted':             'badge bg-danger',
                            'db.credentials_changed':    'badge bg-warning text-dark',
                            'db.credentials_deleted':    'badge bg-danger',
                            'user.updated':              'badge bg-secondary',
                            'user.deleted':              'badge bg-danger',
                            'mail.settings_saved':       'badge bg-info text-dark',
                            'mail.settings_deleted':     'badge bg-danger',
                            'consent.given':             'badge bg-success',
                            'consent.declined':          'badge bg-warning text-dark',
                            'audit.purged':              'badge bg-danger',                            'panel.view_players':        'badge bg-dark border border-info',
                            'panel.view_player':         'badge bg-info text-dark',
                            'panel.view_sessions':       'badge bg-dark border border-info',
                            'panel.view_chat':           'badge bg-dark border border-info',
                            'panel.view_commands':       'badge bg-dark border border-info',
                            'panel.view_deaths':         'badge bg-dark border border-info',
                            'panel.view_blocks':         'badge bg-dark border border-info',
                            'panel.view_proxy':          'badge bg-dark border border-info',
                            'panel.view_server_events':  'badge bg-dark border border-info',
                            'panel.view_perms':          'badge bg-dark border border-info',                        } %}
                        <tr>
                            <td class="text-muted small">{{ row.created_at | fmt_dt }}</td>
                            <td>
                                {% if row.actor_username %}
                                <span class="text-info">{{ row.actor_username }}</span>
                                {% else %}
                                <span class="text-muted">—</span>
                                {% endif %}
                            </td>
                            <td>
                                <span class="{{ action_class.get(row.action, 'badge bg-secondary') }} font-monospace" style="font-size:.75em">
                                    {{ row.action }}
                                </span>
                            </td>
                            <td class="small">
                                {% if row.group_name %}
                                <span class="badge bg-dark border border-secondary">{{ row.group_name }}</span>
                                {% else %}
                                <span class="text-muted">—</span>
                                {% endif %}
                            </td>
                            <td class="small text-muted font-monospace">
                                {% if row.details %}
                                    {% set d = row.details if row.details is mapping else {} %}
                                    {% for k, v in d.items() %}
                                    <span class="me-2"><strong>{{ k }}:</strong> {{ v }}</span>
                                    {% endfor %}
                                {% else %}—{% endif %}
                            </td>
                            <td class="text-muted small font-monospace">{{ row.ip_address or '—' }}</td>
                        </tr>
                        {% else %}
                        <tr>
                            <td colspan="6" class="text-center text-muted py-3">
                                <i class="bi bi-journal-x me-2"></i>No audit events yet.
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
{% endblock %}
