{% extends "admin/base_site.html" %}
{% load i18n jazzmin log %}

{% block bodyclass %}{{ block.super }} dashboard crypto-admin-index{% endblock %}

{% block content_title %}{% trans "Control Center" %}{% endblock %}

{% block breadcrumbs %}
<ol class="breadcrumb">
    <li class="breadcrumb-item"><a href="{% url 'admin:index' %}">{% trans "Home" %}</a></li>
    <li class="breadcrumb-item active">{% trans "Control Center" %}</li>
</ol>
{% endblock %}

{% block content %}
{% get_side_menu using="app_list" as dashboard_list %}
{% get_admin_log 8 as admin_log for_user user %}
{% get_pending_verification_count as pending_verification_count %}
{% get_pending_verification_requests 5 as pending_verification_requests %}
{% get_first_pending_verification_request as first_pending_verification_user %}

<div class="col-12">
    <section class="card crypto-admin-hero mb-4">
        <div class="card-body">
            <div>
                <div class="crypto-admin-eyebrow">
                    <i class="fas fa-shield-alt"></i>
                    <span>CryptoSignalsPro Admin</span>
                </div>
                <h2>{% trans "Professional operations dashboard for your platform" %}</h2>
                <p>{% trans "Manage users, deposits, wallets, referrals, and trading signals from a cleaner control room that matches your frontend product styling." %}</p>
            </div>
            <div class="crypto-admin-hero-actions">
                <a href="#crypto-admin-grid" class="btn btn-primary">{% trans "Browse Modules" %}</a>
                <a href="#crypto-admin-recent" class="btn btn-outline-primary">{% trans "Recent Activity" %}</a>
            </div>
        </div>
    </section>
</div>

{% if pending_verification_count %}
<div class="col-12">
    <section class="card crypto-admin-notice mb-4">
        <div class="card-body">
            <div>
                <div class="crypto-admin-eyebrow">
                    <i class="fas fa-bell"></i>
                    <span>{% trans "Verification Alert" %}</span>
                </div>
                <h5 class="m-0">{{ pending_verification_count }} {% trans "user verification request(s) waiting for review" %}</h5>
                <p class="mb-0 custom-text-muted">{% trans "Users have uploaded their ID card and selfie. Review them from the admin user panel." %}</p>
            </div>
            <div class="crypto-admin-hero-actions">
                <a href="{% if first_pending_verification_user %}{% url 'admin:accounts_user_change' first_pending_verification_user.id %}#id_is_verified{% else %}{% url 'admin:accounts_user_changelist' %}?verification_status=pending{% endif %}" class="btn btn-primary">
                    {% trans "Review Pending Requests" %}
                </a>
            </div>
        </div>
    </section>
</div>
{% endif %}

<div class="col-xl-8 col-12">
    {% if dashboard_list %}
    <div class="crypto-admin-grid" id="crypto-admin-grid">
        {% for app in dashboard_list %}
        <section class="card crypto-admin-card">
            <div class="card-header">
                <div class="crypto-admin-app-meta">
                    <span class="crypto-admin-icon">
                        <i class="{{ app.icon|default:'fas fa-layer-group' }}"></i>
                    </span>
                    <div>
                        <h5 class="m-0">{{ app.name }}</h5>
                        <small>{{ app.models|length }} {% trans "modules" %}</small>
                    </div>
                </div>
            </div>
            <div class="card-body">
                <ul class="crypto-admin-model-list">
                    {% for model in app.models %}
                    <li class="crypto-admin-model-row">
                        <div>
                            <div class="crypto-admin-model-name">
                                <i class="{{ model.icon|default:'far fa-circle' }}"></i>
                                {% if model.url %}
                                <a href="{{ model.url }}">{{ model.name }}</a>
                                {% else %}
                                <span>{{ model.name }}</span>
                                {% endif %}
                            </div>
                            {% if model.view_only %}
                            <small>{% trans "Read-only access" %}</small>
                            {% else %}
                            <small>{% trans "Manage records and workflows" %}</small>
                            {% endif %}
                        </div>
                        <div class="btn-group">
                            {% if model.add_url %}
                            <a href="{{ model.add_url }}" class="btn btn-sm btn-primary">{% trans "Add" %}</a>
                            {% endif %}
                            {% if model.url %}
                            <a href="{{ model.url }}" class="btn btn-sm btn-outline-primary">
                                {% if model.view_only %}{% trans "View" %}{% else %}{% trans "Open" %}{% endif %}
                            </a>
                            {% endif %}
                        </div>
                    </li>
                    {% endfor %}
                </ul>
            </div>
        </section>
        {% endfor %}
    </div>
    {% else %}
    <section class="card crypto-admin-card">
        <div class="card-body">
            <p class="crypto-admin-empty mb-0">{% trans "No admin modules are available for this account." %}</p>
        </div>
    </section>
    {% endif %}
</div>

<div class="col-xl-4 col-12">
    <section class="card crypto-admin-sidebar-card mb-4">
        <div class="card-header">
            <h5 class="m-0">{% trans "Pending Verifications" %}</h5>
        </div>
        <div class="card-body">
            {% if pending_verification_requests %}
            <div class="crypto-admin-verification-list">
                {% for pending_user in pending_verification_requests %}
                <div class="crypto-admin-verification-item">
                    <div>
                        <div class="crypto-admin-timeline-title mb-1">
                            {{ pending_user.get_full_name|default:pending_user.username }}
                        </div>
                        <small class="crypto-admin-empty">
                            {{ pending_user.username }}
                            {% if pending_user.email %} . {{ pending_user.email }}{% endif %}
                        </small>
                    </div>
                    <a href="{% url 'admin:accounts_user_change' pending_user.id %}#id_is_verified" class="btn btn-sm btn-outline-primary">
                        {% trans "Review" %}
                    </a>
                </div>
                {% endfor %}
            </div>
            <a href="{% if first_pending_verification_user %}{% url 'admin:accounts_user_change' first_pending_verification_user.id %}#id_is_verified{% else %}{% url 'admin:accounts_user_changelist' %}?verification_status=pending{% endif %}" class="btn btn-sm btn-primary mt-3">
                {% trans "View All Pending" %}
            </a>
            {% else %}
            <p class="crypto-admin-empty mb-0">{% trans "No pending verification requests right now." %}</p>
            {% endif %}
        </div>
    </section>

    <section class="card crypto-admin-sidebar-card mb-4">
        <div class="card-header">
            <h5 class="m-0">{% trans "Workspace Snapshot" %}</h5>
        </div>
        <div class="card-body">
            <div class="crypto-admin-summary">
                <div class="crypto-admin-summary-item">
                    <span>{% trans "Admin Sections" %}</span>
                    <strong>{{ dashboard_list|length }}</strong>
                </div>
                <div class="crypto-admin-summary-item">
                    <span>{% trans "Theme" %}</span>
                    <strong>{% trans "Crypto dashboard style" %}</strong>
                </div>
                <div class="crypto-admin-summary-item">
                    <span>{% trans "Recent Actions" %}</span>
                    <strong>{{ admin_log|length }} {% trans "tracked events" %}</strong>
                </div>
            </div>
        </div>
    </section>

    <section class="card crypto-admin-sidebar-card" id="crypto-admin-recent">
        <div class="card-header">
            <h5 class="m-0">{% trans "Recent Actions" %}</h5>
        </div>
        <div class="card-body">
            {% if admin_log %}
            <div class="crypto-admin-timeline">
                {% for entry in admin_log %}
                <div class="crypto-admin-timeline-item">
                    <span class="crypto-admin-timeline-badge">
                        {% if entry.is_change %}
                        <i class="fas fa-pen"></i>
                        {% elif entry.is_deletion %}
                        <i class="fas fa-trash"></i>
                        {% elif entry.is_addition %}
                        <i class="fas fa-plus"></i>
                        {% else %}
                        <i class="fas fa-clock"></i>
                        {% endif %}
                    </span>
                    <div>
                        <div class="crypto-admin-timeline-time">{{ entry.action_time|timesince }} {% trans "ago" %}</div>
                        <div class="crypto-admin-timeline-title">
                            {% if entry.is_deletion or not entry.get_admin_url %}
                            {{ entry.object_repr }}
                            {% else %}
                            <a href="{{ entry.get_admin_url }}">{{ entry.object_repr }}</a>
                            {% endif %}
                        </div>
                        <small class="crypto-admin-empty">
                            {% if entry.model %}
                            {{ entry.model|capfirst }}
                            {% else %}
                            {% trans "Admin event" %}
                            {% endif %}
                        </small>
                    </div>
                </div>
                {% endfor %}
            </div>
            {% else %}
            <p class="crypto-admin-empty mb-0">{% trans "No recent admin actions yet." %}</p>
            {% endif %}
        </div>
    </section>
</div>
{% endblock %}
