{% extends "industry_reforged/base.html" %}
{% load i18n humanize %}

{% block details %}
<div class="container-fluid">
    <div class="row mb-3">
        <div class="col-12 d-flex justify-content-between align-items-center">
            <h3><i class="fas fa-search-location"></i> {% trans "Discover Corporate Hangars" %}</h3>
            <a href="{% url 'industry_reforged:director_config' %}" class="btn btn-primary"><i class="fas fa-arrow-left"></i> Back to Config</a>
        </div>
        <p class="text-muted">This tool scans the first few pages of your corporate assets to find active Location IDs and Flag IDs (hangars) so you can easily configure them for inventory tracking.</p>
    </div>

    <!-- Discovered Hangars Table -->
    <div class="row">
        <div class="col-12">
            <div class="card border-0 mb-4">
                <div class="card-header">
                    <h3 class="card-title mb-0"><i class="fas fa-radar"></i> {% trans "Found Hangar Locations" %}</h3>
                </div>
                <div class="card-body p-0">
                    <div class="alert alert-info m-3">
                        <i class="fas fa-info-circle text-info"></i> <strong class="text-white">How it works:</strong> This tool only looks at items directly located in a hangar to identify the Location ID (Station/Structure) and the Division Flag ID. It does not scan inside containers or ships during this discovery phase. Once you configure a hangar here, the background synchronization task will automatically dive deep and scan the entire inventory including all containers and ships.
                        <br><br>
                        <i class="fas fa-exclamation-triangle text-warning"></i> <strong class="text-white">Structure Names Unknown?</strong> If your player structures show up as "Unknown Structure", you need to grant the <code>esi-universe.read_structures.v1</code> permission. Go back to the Director Config page and click "Grant Corp Token (Assets, Jobs & Structures)" to add this scope.
                    </div>
                    <table class="table table-striped table-hover mb-0">
                        <thead>
                            <tr>
                                <th>Location (Station/Structure)</th>
                                <th>Flag ID (Division)</th>
                                <th>Items Found (approx)</th>
                                <th>Status</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for hangar in discovered_hangars %}
                            <tr>
                                <td>
                                    <strong class="text-white">{{ hangar.location_name }}</strong><br>
                                    <small class="text-muted">ID: {{ hangar.location_id }}</small>
                                </td>
                                <td><code class="text-warning">{{ hangar.flag_id }}</code></td>
                                <td class="numeric">{{ hangar.item_count|intcomma }}</td>
                                <td>
                                    {% if hangar.is_configured %}
                                    <span class="badge bg-primary"><i class="fas fa-check"></i> Configured</span>
                                    {% else %}
                                    <span class="badge bg-secondary">Not Configured</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if not hangar.is_configured %}
                                    <form method="POST" class="d-inline show-loader">
                                        {% csrf_token %}
                                        <input type="hidden" name="location_id" value="{{ hangar.location_id }}">
                                        <input type="hidden" name="flag_id" value="{{ hangar.flag_id }}">
                                        <input type="hidden" name="description" value="Hangar {{ hangar.flag_id }}">
                                        <button type="submit" class="btn btn-sm btn-success"><i class="fas fa-plus"></i> Add to Config</button>
                                    </form>
                                    {% else %}
                                    <button class="btn btn-sm btn-primary" disabled>Already Added</button>
                                    {% endif %}
                                </td>
                            </tr>
                            {% empty %}
                            <tr><td colspan="5" class="text-center text-muted">No corporate hangars could be discovered. Please ensure your ESI token is valid and your corporation has assets.</td></tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
