{% extends "pi_fyad/base.html" %}
{% load pi_fyad_tags %}

{% block pi_content %}

<!-- Summary Cards -->
<div class="row mb-4">
    <div class="col-md-3">
        <div class="card pi-card">
            <div class="card-body text-center">
                <h5 class="card-title text-muted">Characters</h5>
                <h2>{{ total_characters }}</h2>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card pi-card">
            <div class="card-body text-center">
                <h5 class="card-title text-muted">Total Planets</h5>
                <h2>
                    {% with total=0 %}
                    {% for cd in character_data %}
                        {{ cd.total_planets|add:total }}
                    {% endfor %}
                    {% endwith %}
                </h2>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card pi-card">
            <div class="card-body text-center">
                <h5 class="card-title text-muted">Active Extractors</h5>
                <h2 class="text-success">
                    {% for cd in character_data %}{{ cd.active_extractors }}{% endfor %}
                </h2>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card pi-card {% if critical_planets %}border-danger{% endif %}">
            <div class="card-body text-center">
                <h5 class="card-title text-muted">Critical Alerts</h5>
                <h2 class="{% if critical_planets %}text-danger{% endif %}">
                    {{ critical_planets|length }}
                </h2>
            </div>
        </div>
    </div>
</div>

<!-- Critical Alerts -->
{% if critical_planets %}
<div class="alert alert-danger mb-4">
    <h5><i class="fas fa-exclamation-triangle me-2"></i>Critical Planets</h5>
    <ul class="mb-0">
        {% for planet in critical_planets %}
        <li>
            <strong>{{ planet.owner.character.character_name }}</strong> —
            {{ planet.planet_name }} ({{ planet.solar_system_name }})
            {% with summary=planet.extractor_summary %}
            {% if summary.expired > 0 %}
                <span class="badge bg-danger">{{ summary.expired }} expired extractor{{ summary.expired|pluralize }}</span>
            {% endif %}
            {% endwith %}
        </li>
        {% endfor %}
    </ul>
</div>
{% endif %}

<!-- Character Table -->
<div class="card">
    <div class="card-header">
        <h5 class="mb-0"><i class="fas fa-users me-2"></i>Characters</h5>
    </div>
    <div class="card-body p-0">
        <table class="table table-hover table-striped mb-0">
            <thead>
                <tr>
                    <th>Character</th>
                    <th class="text-center">Planets</th>
                    <th class="text-center">Extractors</th>
                    <th class="text-center">Active</th>
                    <th class="text-center">Expired</th>
                    <th class="text-center">Last Sync</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                {% for cd in character_data %}
                <tr>
                    <td>
                        {% eve_character_portrait cd.character.character_id 32 %}
                        <span class="ms-2">{{ cd.character.character_name }}</span>
                    </td>
                    <td class="text-center">{{ cd.total_planets }}</td>
                    <td class="text-center">{{ cd.total_extractors }}</td>
                    <td class="text-center text-success">{{ cd.active_extractors }}</td>
                    <td class="text-center {% if cd.expired_extractors > 0 %}text-danger fw-bold{% endif %}">
                        {{ cd.expired_extractors }}
                    </td>
                    <td class="text-center text-muted">
                        {% if cd.owner.last_planet_sync %}
                            {{ cd.owner.last_planet_sync|timesince }} ago
                        {% else %}
                            Never
                        {% endif %}
                    </td>
                    <td class="text-end">
                        <a href="{% url 'pi_fyad:character_planets' cd.character.pk %}"
                           class="btn btn-sm btn-outline-primary">
                            <i class="fas fa-eye me-1"></i>View
                        </a>
                    </td>
                </tr>
                {% empty %}
                <tr>
                    <td colspan="7" class="text-center text-muted py-4">
                        No characters with PI data. Sync will begin automatically.
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
</div>

{% endblock %}
