{% extends "indy_hub/base.html" %}
{% load i18n %}
{% load static %}

{% block page_title %}{% trans "Token Management" %}{% endblock page_title %}

{% block extra_css %}
<link rel="stylesheet" href="{% static 'indy_hub/css/components.css' %}">
<link rel="stylesheet" href="{% static 'indy_hub/css/index.css' %}">
<link rel="stylesheet" href="{% static 'indy_hub/css/token_management.css' %}">
{% endblock extra_css %}

{% block content %}
<div class="container-fluid py-4">
    {% include "indy_hub/includes/popup.html" %}

    <section class="hero-section mb-4">
        <div class="row g-4 align-items-center">
            <div class="col-xl-7">
                <span class="hero-badge text-uppercase">{% trans "ESI tokens" %}</span>
                <h1 class="hero-title d-flex align-items-center">
                    <span class="hero-icon me-3">
                        <i class="fas fa-key"></i>
                    </span>
                    {% trans "Token Management" %}
                </h1>
                <p class="hero-lead mb-0">
                    {% trans "Keep your characters and corporations synchronized with the scopes Indy Hub needs." %}
                </p>
            </div>
        </div>
    </section>

    <section class="dashboard-section mb-4">
        <header class="section-heading">
            <div>
                <h2>{% trans "ESI overview" %}</h2>
                <p class="text-muted small mb-0">{% trans "Check what is authorized and add the missing scopes." %}</p>
            </div>
        </header>
        <div class="card focus-card">
            <div class="card-body p-4">
                <div class="row g-4 align-items-center">
                    <div class="col-lg-4">
                        <div class="stat-chip">
                            <span class="stat-chip-label">{% trans "Blueprint tokens" %}</span>
                            <span class="stat-chip-value">{{ blueprint_token_count|default:0 }}</span>
                        </div>
                    </div>
                    <div class="col-lg-4">
                        <div class="stat-chip">
                            <span class="stat-chip-label">{% trans "Industry tokens" %}</span>
                            <span class="stat-chip-value">{{ jobs_token_count|default:0 }}</span>
                        </div>
                    </div>
                    <div class="col-lg-4">
                        <div class="stat-chip">
                            <span class="stat-chip-label">{% trans "Characters connected" %}</span>
                            <span class="stat-chip-value">{{ characters|length }}</span>
                        </div>
                    </div>
                </div>
                {% with total_chars=characters|length blueprint_active=blueprint_token_count|default:0 jobs_active=jobs_token_count|default:0 %}
                <div class="d-flex flex-wrap gap-2 mt-3">
                    {% if total_chars %}
                        {% if blueprint_active == total_chars %}
                        <span class="badge rounded-pill d-inline-flex align-items-center gap-2 bg-success-subtle text-success">
                            <i class="fas fa-check-circle"></i>{% trans "All characters share blueprint scope" %}
                        </span>
                        {% elif blueprint_active %}
                        <span class="badge rounded-pill d-inline-flex align-items-center gap-2 bg-warning-subtle text-warning">
                            <i class="fas fa-exclamation-triangle"></i>
                            {% blocktrans with authorized=blueprint_active total=total_chars %}{{ authorized }} of {{ total }} characters share blueprints{% endblocktrans %}
                        </span>
                        {% else %}
                        <span class="badge rounded-pill d-inline-flex align-items-center gap-2 bg-danger-subtle text-danger">
                            <i class="fas fa-exclamation-circle"></i>{% trans "No blueprint scopes authorized yet" %}
                        </span>
                        {% endif %}
                        {% if jobs_active == total_chars %}
                        <span class="badge rounded-pill d-inline-flex align-items-center gap-2 bg-success-subtle text-success">
                            <i class="fas fa-check-circle"></i>{% trans "All characters share industry scope" %}
                        </span>
                        {% elif jobs_active %}
                        <span class="badge rounded-pill d-inline-flex align-items-center gap-2 bg-warning-subtle text-warning">
                            <i class="fas fa-exclamation-triangle"></i>
                            {% blocktrans with authorized=jobs_active total=total_chars %}{{ authorized }} of {{ total }} characters share industry data{% endblocktrans %}
                        </span>
                        {% else %}
                        <span class="badge rounded-pill d-inline-flex align-items-center gap-2 bg-danger-subtle text-danger">
                            <i class="fas fa-exclamation-circle"></i>{% trans "No industry scopes authorized yet" %}
                        </span>
                        {% endif %}
                    {% else %}
                    <span class="badge rounded-pill d-inline-flex align-items-center gap-2 bg-secondary-subtle text-secondary">
                        <i class="fas fa-user-slash"></i>{% trans "No characters connected yet" %}
                    </span>
                    {% endif %}
                    {% if can_manage_corporate_assets %}
                        {% with corp_total=corp_count|default:0 corp_bp=corp_blueprint_scope_count|default:0 corp_jobs=corp_jobs_scope_count|default:0 %}
                            {% if corp_total %}
                                {% if corp_bp == corp_total %}
                                <span class="badge rounded-pill d-inline-flex align-items-center gap-2 bg-info-subtle text-info">
                                    <i class="fas fa-building"></i>{% trans "All corporations share blueprints" %}
                                </span>
                                {% elif corp_bp %}
                                <span class="badge rounded-pill d-inline-flex align-items-center gap-2 bg-warning-subtle text-warning">
                                    <i class="fas fa-exclamation-triangle"></i>
                                    {% blocktrans with authorized=corp_bp total=corp_total %}{{ authorized }} of {{ total }} corporations share blueprints{% endblocktrans %}
                                </span>
                                {% else %}
                                <span class="badge rounded-pill d-inline-flex align-items-center gap-2 bg-danger-subtle text-danger">
                                    <i class="fas fa-exclamation-circle"></i>{% trans "No corporation blueprint scopes active" %}
                                </span>
                                {% endif %}
                                {% if corp_jobs == corp_total %}
                                <span class="badge rounded-pill d-inline-flex align-items-center gap-2 bg-info-subtle text-info">
                                    <i class="fas fa-industry"></i>{% trans "All corporations share industry data" %}
                                </span>
                                {% elif corp_jobs %}
                                <span class="badge rounded-pill d-inline-flex align-items-center gap-2 bg-warning-subtle text-warning">
                                    <i class="fas fa-exclamation-triangle"></i>
                                    {% blocktrans with authorized=corp_jobs total=corp_total %}{{ authorized }} of {{ total }} corporations share jobs{% endblocktrans %}
                                </span>
                                {% else %}
                                <span class="badge rounded-pill d-inline-flex align-items-center gap-2 bg-danger-subtle text-danger">
                                    <i class="fas fa-exclamation-circle"></i>{% trans "No corporation industry scopes active" %}
                                </span>
                                {% endif %}
                            {% else %}
                            <span class="badge rounded-pill d-inline-flex align-items-center gap-2 bg-secondary-subtle text-secondary">
                                <i class="fas fa-building"></i>{% trans "No corporation access configured" %}
                            </span>
                            {% endif %}
                        {% endwith %}
                    {% endif %}
                </div>
                {% endwith %}
                <hr class="my-4">
                <div class="row g-4 align-items-stretch">
                    <div class="col-xl-6">
                        <div class="p-3 bg-body-tertiary rounded-3 h-100">
                            <div class="d-flex align-items-center justify-content-between mb-2">
                                <h6 class="text-uppercase text-muted fw-bold mb-0">{% trans "Character scopes" %}</h6>
                                <span class="badge bg-primary-subtle text-primary">{{ characters|length }}</span>
                            </div>
                            <p class="small text-muted mb-3">{% trans "Authorize or refresh personal access for blueprints and industry jobs." %}</p>
                            <div class="d-flex flex-wrap gap-2">
                                {% if blueprint_auth_url %}
                                <a href="{{ blueprint_auth_url }}" class="btn btn-outline-primary btn-slim">
                                    <i class="fas fa-scroll me-1"></i>{% trans "Authorize blueprint scope" %}
                                </a>
                                {% endif %}
                                {% if jobs_auth_url %}
                                <a href="{{ jobs_auth_url }}" class="btn btn-outline-success btn-slim">
                                    <i class="fas fa-industry me-1"></i>{% trans "Authorize industry scope" %}
                                </a>
                                {% endif %}
                                <a href="{% url 'indy_hub:authorize_all' %}" class="btn btn-accent btn-slim">
                                    <i class="fas fa-key me-1"></i>{% trans "Authorize all scopes" %}
                                </a>
                            </div>
                        </div>
                    </div>
                    {% if can_manage_corporate_assets %}
                    <div class="col-xl-6">
                        <div class="p-3 bg-body-tertiary rounded-3 h-100">
                            <div class="d-flex align-items-center justify-content-between mb-2">
                                <h6 class="text-uppercase text-muted fw-bold mb-0">{% trans "Corporation scopes" %}</h6>
                                <span class="badge bg-info-subtle text-info">{{ corp_count|default:0 }}</span>
                            </div>
                            <p class="small text-muted mb-3">{% trans "Directors can share corporate blueprints and jobs from a single place." %}</p>
                            <div class="d-flex flex-wrap gap-2">
                                {% if corp_blueprint_auth_url %}
                                <a href="{{ corp_blueprint_auth_url }}" class="btn btn-outline-primary btn-slim">
                                    <i class="fas fa-scroll me-1"></i>{% trans "Authorize corp blueprints" %}
                                </a>
                                {% endif %}
                                {% if corp_jobs_auth_url %}
                                <a href="{{ corp_jobs_auth_url }}" class="btn btn-outline-success btn-slim">
                                    <i class="fas fa-industry me-1"></i>{% trans "Authorize corp jobs" %}
                                </a>
                                {% endif %}
                                {% if corp_all_auth_url %}
                                <a href="{{ corp_all_auth_url }}" class="btn btn-accent btn-slim">
                                    <i class="fas fa-building me-1"></i>{% trans "Authorize all corp scopes" %}</a>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </section>

    <section class="dashboard-section mb-5">
        <header class="section-heading">
            <div>
                <h2>{% trans "Character access" %}</h2>
                <p class="text-muted small mb-0">{% trans "Blueprint and industry tokens per pilot." %}</p>
            </div>
        </header>
        <div class="card focus-card">
            <div class="card-body p-0">
                <div class="px-4 py-3 border-bottom bg-body-tertiary">
                    <p class="small text-muted mb-0">{% trans "Badges mark scopes per pilot; use the overview actions above to resolve missing access." %}</p>
                </div>
                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0">
                        <thead class="table-light">
                            <tr>
                                <th style="width: 80px;"></th>
                                <th>{% trans "Character" %}</th>
                                <th class="text-center">{% trans "Blueprint sync" %}</th>
                                <th class="text-center">{% trans "Jobs sync" %}</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for char in characters %}
                            <tr>
                                <td>
                                    <img
                                        src="https://images.evetech.net/characters/{{ char.character_id }}/portrait?size=64"
                                        alt="{{ char.name }}"
                                        class="rounded-circle"
                                        style="width: 48px; height: 48px; object-fit: cover;"
                                        loading="lazy"
                                        onerror="this.style.display='none';">
                                </td>
                                <td class="align-middle">
                                    <span class="fw-semibold">{{ char.name }}</span>
                                </td>
                                <td class="text-center align-middle">
                                    <span class="badge rounded-pill {{ char.bp_enabled|yesno:'bg-success,bg-danger' }} text-white px-3 py-1">
                                        <i class="fas fa-{{ char.bp_enabled|yesno:'check,times' }}"></i>
                                    </span>
                                </td>
                                <td class="text-center align-middle">
                                    <span class="badge rounded-pill {{ char.jobs_enabled|yesno:'bg-success,bg-danger' }} text-white px-3 py-1">
                                        <i class="fas fa-{{ char.jobs_enabled|yesno:'check,times' }}"></i>
                                    </span>
                                </td>
                            </tr>
                            {% empty %}
                            <tr>
                                <td colspan="4" class="text-center text-muted py-4">{% trans "No characters found." %}</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </section>

    {% if can_manage_corporate_assets %}
    <section class="dashboard-section mb-5">
        <header class="section-heading">
            <div>
                <h2>{% trans "Corporation sharing" %}</h2>
                <p class="text-muted small mb-0">{% trans "Directors keep corp blueprints and jobs available for the team." %}</p>
            </div>
        </header>
        <div class="row g-4 align-items-stretch">
            <div class="col-xxl-5 col-xl-6 col-lg-12">
                <div class="card focus-card h-100">
                    <div class="card-body p-4 d-flex flex-column">
                        <div class="d-flex justify-content-between align-items-start mb-3">
                            <div>
                                <h5 class="card-title mb-1">{% trans "Who should authorize?" %}</h5>
                                <p class="text-muted small mb-0">{% trans "Use trusted directors with the required roles because corporate tokens expose hangar blueprints and job data to Indy Hub." %}</p>
                            </div>
                            <span class="badge rounded-pill bg-info-subtle text-info">{{ corp_count|default:0 }}</span>
                        </div>
                        {% if corporation_sharing %}
                        <div class="stat-pair mb-4">
                            <div class="stat-chip">
                                <span class="stat-chip-label">{% trans "Blueprints synced" %}</span>
                                <span class="stat-chip-value">{{ corporation_sharing.total_blueprints|default:0 }}</span>
                            </div>
                            <div class="stat-chip">
                                <span class="stat-chip-label">{% trans "Jobs tracked" %}</span>
                                <span class="stat-chip-value">{{ corporation_sharing.total_jobs|default:0 }}</span>
                            </div>
                        </div>
                        {% endif %}
                        <h6 class="text-uppercase text-muted fw-bold mb-2">{% trans "Required roles" %}</h6>
                        <ul class="list-unstyled small text-muted mb-4">
                            {% if corporation_sharing %}
                            <li class="text-muted"><i class="fas fa-user-shield me-2"></i>{% trans "Director or station manager roles" %}</li>
                            <li class="text-muted"><i class="fas fa-scroll me-2"></i><code>{{ corporation_sharing.scopes.blueprints }}</code></li>
                            <li class="text-muted"><i class="fas fa-industry me-2"></i><code>{{ corporation_sharing.scopes.jobs }}</code></li>
                            {% if corporation_sharing.scopes.structures %}
                            <li class="text-muted"><i class="fas fa-key me-2"></i><code>{{ corporation_sharing.scopes.structures }}</code> {% trans "(optional - structure names)" %}</li>
                            {% endif %}
                            {% else %}
                            <li class="text-muted">{% trans "Grant director-level access to enable corporate syncing." %}</li>
                            {% endif %}
                        </ul>
                        <p class="text-muted small mt-auto mb-0">{% trans "Use the corporation scopes above to grant or refresh access." %}</p>
                    </div>
                </div>
            </div>
            <div class="col-xxl-7 col-xl-6 col-lg-12">
                <div class="card focus-card h-100">
                    <div class="card-body p-4 d-flex flex-column">
                        <div class="d-flex justify-content-between align-items-start mb-3">
                            <div>
                                <h5 class="card-title mb-1">{% trans "Sync overview" %}</h5>
                                <p class="text-muted small mb-0">{% trans "Track which corporations are covered and who authorised them." %}</p>
                            </div>
                            {% if corporation_sharing and not corporation_sharing.has_authorised_characters %}
                            <span class="badge bg-warning text-dark">
                                <i class="fas fa-exclamation-triangle me-1"></i>{% trans "No authorized pilots yet" %}
                            </span>
                            {% endif %}
                        </div>
                        <div class="stat-pair mb-3">
                            <div class="stat-chip">
                                <span class="stat-chip-label">{% trans "Blueprint tokens" %}</span>
                                <span class="stat-chip-value">{{ corp_blueprint_scope_count|default:0 }}/{{ corp_count|default:0 }}</span>
                            </div>
                            <div class="stat-chip">
                                <span class="stat-chip-label">{% trans "Industry tokens" %}</span>
                                <span class="stat-chip-value">{{ corp_jobs_scope_count|default:0 }}/{{ corp_count|default:0 }}</span>
                            </div>
                        </div>
                        <p class="small text-muted mb-3">{% trans "Missing scopes stay highlighted until a director refreshes access from the overview actions." %}</p>
                        <div class="table-responsive">
                            <table class="table table-sm align-middle mb-0">
                                <thead class="table-light">
                                    <tr>
                                        <th scope="col">{% trans "Corporation" %}</th>
                                        <th scope="col" class="text-center">{% trans "Blueprints" %}</th>
                                        <th scope="col" class="text-center">{% trans "Industry" %}</th>
                                        <th scope="col">{% trans "Updated" %}</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% if corporation_sharing %}
                                        {% for corp in corporation_sharing.corporations %}
                                        <tr>
                                            <td>
                                                <div class="fw-semibold">{{ corp.name }}</div>
                                                <div class="small text-muted">ID {{ corp.corporation_id }}</div>
                                                {% if corp.authorization.restricted %}
                                                <div class="small text-muted">
                                                    <i class="fas fa-user-lock me-1"></i>
                                                    {% if corp.authorization.characters %}
                                                        {% trans "Whitelisted" %}:
                                                        {% for pilot in corp.authorization.characters %}
                                                            {{ pilot.name|default:pilot.id }}{% if not forloop.last %}, {% endif %}
                                                        {% endfor %}
                                                    {% else %}
                                                        {% trans "Whitelisted" %}: {% trans "no pilots selected" %}
                                                    {% endif %}
                                                </div>
                                                {% endif %}
                                            </td>
                                            <td class="text-center">
                                                {% if corp.blueprints.token.has_scope %}
                                                <span class="badge bg-success-subtle text-success d-inline-flex align-items-center gap-1">
                                                    <i class="fas fa-scroll"></i>{{ corp.blueprints.token.character_name|default:_("Authorised") }}
                                                </span>
                                                <div class="small text-muted mt-1">
                                                    {% blocktrans with total=corp.blueprints.total %}{{ total }} blueprints{% endblocktrans %}
                                                </div>
                                                {% else %}
                                                <span class="badge bg-danger-subtle text-danger">{% trans "Missing" %}</span>
                                                {% endif %}
                                            </td>
                                            <td class="text-center">
                                                {% if corp.jobs.token.has_scope %}
                                                <span class="badge bg-success-subtle text-success d-inline-flex align-items-center gap-1">
                                                    <i class="fas fa-industry"></i>{{ corp.jobs.token.character_name|default:_("Authorised") }}
                                                </span>
                                                <div class="small text-muted mt-1">
                                                    {% blocktrans with total=corp.jobs.total %}{{ total }} jobs{% endblocktrans %}
                                                </div>
                                                {% else %}
                                                <span class="badge bg-danger-subtle text-danger">{% trans "Missing" %}</span>
                                                {% endif %}
                                            </td>
                                            <td>
                                                <div class="small text-muted">
                                                    {% trans "Blueprints" %}: {{ corp.blueprints.last_sync|date:"SHORT_DATETIME_FORMAT"|default:"-" }}
                                                </div>
                                                <div class="small text-muted">
                                                    {% trans "Jobs" %}: {{ corp.jobs.last_sync|date:"SHORT_DATETIME_FORMAT"|default:"-" }}
                                                </div>
                                            </td>
                                        </tr>
                                        {% empty %}
                                        <tr>
                                            <td colspan="4" class="text-center text-muted py-4">
                                                <i class="fas fa-info-circle me-1"></i>{% trans "No corporation data detected yet. Authorize a director or factory manager to begin syncing." %}
                                            </td>
                                        </tr>
                                        {% endfor %}
                                    {% else %}
                                        <tr>
                                            <td colspan="4" class="text-center text-muted py-4">
                                                <i class="fas fa-info-circle me-1"></i>{% trans "No corporation data detected yet. Authorize a director or factory manager to begin syncing." %}
                                            </td>
                                        </tr>
                                    {% endif %}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
    {% endif %}

    <section class="dashboard-section mb-4">
            <header class="section-heading">
                <div>
                    <h2>{% trans "Help & resources" %}</h2>
                    <p class="text-muted small mb-0">{% trans "Understand how ESI authorization fits into Indy Hub." %}</p>
                </div>
            </header>
            <div class="card focus-card">
                <div class="card-body p-4">
                    <div class="row g-4 mb-4">
                        <div class="col-md-4">
                            <div class="text-center h-100 d-flex flex-column align-items-center">
                                <div class="feature-icon d-inline-flex align-items-center justify-content-center mb-3 text-white" style="background: var(--bs-primary);">
                                    <i class="fas fa-shield-alt fa-lg"></i>
                                </div>
                                <h6 class="mt-1">{% trans "Secure authorization" %}</h6>
                                <p class="text-muted small mb-0">
                                    {% trans "All authorization is handled securely through CCP's official ESI API using OAuth 2.0 standards." %}
                                </p>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="text-center h-100 d-flex flex-column align-items-center">
                                <div class="feature-icon d-inline-flex align-items-center justify-content-center mb-3 text-white" style="background: var(--bs-success);">
                                    <i class="fas fa-sync fa-lg"></i>
                                </div>
                                <h6 class="mt-1">{% trans "Automatic updates" %}</h6>
                                <p class="text-muted small mb-0">
                                    {% trans "Your data is automatically synchronized with EVE Online, ensuring you always have the latest information." %}
                                </p>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="text-center h-100 d-flex flex-column align-items-center">
                                <div class="feature-icon d-inline-flex align-items-center justify-content-center mb-3 text-white" style="background: var(--bs-warning);">
                                    <i class="fas fa-user-shield fa-lg"></i>
                                </div>
                                <h6 class="mt-1">{% trans "Privacy protected" %}</h6>
                                <p class="text-muted small mb-0">
                                    {% trans "Only the data you specifically authorize will be accessed. You can revoke access at any time." %}
                                </p>
                            </div>
                        </div>
                    </div>
                    <div class="alert alert-light border-0 mb-0">
                        <h6 class="alert-heading">
                            <i class="fas fa-lightbulb text-warning me-2"></i>{% trans "Getting started" %}
                        </h6>
                        <p class="mb-2">{% trans "To use Indy Hub effectively, authorize both blueprint and industry jobs access:" %}</p>
                        <ol class="mb-0">
                            <li>{% trans "Use the buttons above to choose the scopes you need" %}</li>
                            <li>{% trans "Log in via EVE Online's secure portal" %}</li>
                            <li>{% trans "Select the characters or corporations to authorize" %}</li>
                            <li>{% trans "Return here to confirm that sync is active" %}</li>
                        </ol>
                    </div>
                </div>
            </div>
        </section>
</div>


{% block extra_javascript %}
<script src="{% static 'indy_hub/js/index.js' %}"></script>
<script src="{% static 'indy_hub/js/token_management.js' %}"></script>
<script>
// Configure text for the JavaScript module
window.redirectingText = '{% trans "Redirecting..." %}';
{% if corp_role_warning_payload_json %}
document.addEventListener('DOMContentLoaded', function() {
    try {
        var corpRoleWarnings = JSON.parse('{{ corp_role_warning_payload_json|escapejs }}');
        if (Array.isArray(corpRoleWarnings)) {
            corpRoleWarnings.forEach(function(payload) {
                if (!payload || !payload.message) {
                    return;
                }
                var tone = payload.level || 'warning';
                if (typeof showIndyHubPopup === 'function') {
                    showIndyHubPopup(payload.message, tone);
                }
            });
        }
    } catch (err) {
        console.error('Unable to display corporation role warnings:', err);
    }
});
{% endif %}
</script>
{% endblock extra_javascript %}
{% endblock content %}
