{% extends "base.html" %}

{% block content %}
<div class="px-4 py-5 my-5 text-center">
    <img class="d-block mx-auto mb-4" src="{{ url_for('static', filename='FHIRFLARE.png') }}" alt="FHIRFLARE Ig Toolkit" width="192" height="192">
    <h1 class="display-5 fw-bold text-body-emphasis">Manage & Process FHIR Packages</h1>
    <div class="col-lg-6 mx-auto">
        <p class="lead mb-4">
            This is the starting Point for your Journey through the IG's
        </p>
          <!-----------------------------------------------------------------remove the buttons-----------------------------------------------------
        <div class="d-grid gap-2 d-sm-flex justify-content-sm-center">
            <a href="{{ url_for('search_and_import') }}" class="btn btn-primary btn-lg px-4 gap-3">Import IGs</a>
            <a href="{{ url_for('view_igs') }}" class="btn btn-outline-secondary btn-lg px-4 disabled">Manage FHIR Packages</a>
            <a href="{{ url_for('push_igs') }}" class="btn btn-outline-secondary btn-lg px-4">Upload IG's</a>
        </div>
        ------------------------------------------------------------------------------------------------------------------------------------------------->
    </div>
</div>

<div class="container mt-4">
    <style>
        .badge.bg-warning { background-color: #ffc107 !important; }
        .badge.bg-info { background-color: #0dcaf0 !important; }
        .badge.bg-secondary { background-color: #6c757d !important; }
        .badge.bg-danger { background-color: #dc3545 !important; }
        .table tr.bg-warning { background-color: #ffc107 !important; }
        .table tr.bg-info { background-color: #0dcaf0 !important; }
        .table tr.bg-secondary { background-color: #6c757d !important; }
        .table tr.bg-danger { background-color: #dc3545 !important; }
        .table-custom-bg { --bs-table-bg: transparent !important; }
    </style>

    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><i class="bi bi-journal-arrow-down me-2"></i>Manage FHIR Packages</h2>
        <div>
            <a href="{{ url_for('search_and_import') }}" class="btn btn-success"><i class="bi bi-download me-1"></i> Import More IGs</a>
        </div>
    </div>

    <div class="row g-4">
        <!-- LEFT: Downloaded Packages -->
        <div class="col-md-5">
            <div class="card h-100">
                <div class="card-header"><i class="bi bi-folder-symlink me-1"></i> Downloaded Packages ({{ packages|length }})</div>
                <div class="card-body">
                    {% if packages %}
                        <div class="table-responsive">
                            <p class="mb-2"><small><span class="badge bg-danger text-light border me-1">Risk:</span> = Duplicate Dependencies</small></p>
                            <table class="table table-sm table-hover table-custom-bg">
                                <thead>
                                    <tr><th>Package Name</th><th>Version</th><th>Actions</th></tr>
                                </thead>
                                <tbody>
                                    {% for pkg in packages %}
                                        {% set is_processed = (pkg.name, pkg.version) in processed_ids %}
                                        {% set is_duplicate = pkg.name in duplicate_groups %}
                                        {% set group_color = group_colors[pkg.name] if (is_duplicate and pkg.name in group_colors) else 'bg-warning' if is_duplicate else '' %}
                                        <tr class="{% if group_color %}{{ group_color }} text-dark{% endif %}">
                                            <td>
                                                <code>{{ pkg.name }}</code>
                                                {% if is_duplicate %}
                                                    <span class="badge bg-danger text-light ms-1">Duplicate</span>
                                                {% endif %}
                                            </td>
                                            <td>{{ pkg.version }}</td>
                                            <td>
                                                <div class="btn-group-vertical btn-group-sm w-80">
                                                    {% if is_processed %}
                                                        <span class="btn btn-success disabled"><i class="bi bi-check-lg"></i> Processed</span>
                                                    {% else %}
                                                        <form method="POST" action="{{ url_for('process_ig') }}" style="display:inline;">
                                                            {{ form.csrf_token }}
                                                            <input type="hidden" name="filename" value="{{ pkg.filename }}">
                                                            <button type="submit" class="btn btn-outline-primary w-80"><i class="bi bi-gear"></i> Process</button>
                                                        </form>
                                                    {% endif %}
                                                    <form method="POST" action="{{ url_for('delete_ig') }}" style="display:inline;">
                                                        {{ form.csrf_token }}
                                                        <input type="hidden" name="filename" value="{{ pkg.filename }}">
                                                        <button type="submit" class="btn btn-outline-danger w-80" onclick="return confirm('Are you sure you want to delete {{ pkg.name }}#{{ pkg.version }}?');"><i class="bi bi-trash"></i> Delete</button>
                                                    </form>
                                                </div>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                        {% if duplicate_groups %}
                            <p class="mt-2 small text-muted">Duplicate dependencies detected:
                                {% for name, versions in duplicate_groups.items() %}
                                    {% set group_color = group_colors[name] if name in group_colors else 'bg-warning' %}
                                    <span class="badge {{ group_color }} text-dark">{{ name }} ({{ versions|join(', ') }})</span>
                                {% endfor %}
                            </p>
                        {% else %}
                            <p class="mt-2 small text-muted">No duplicates detected.</p>
                        {% endif %}
                    {% else %}
                        <p class="text-muted">No downloaded FHIR packages found.</p>
                    {% endif %}
                </div>
            </div>
        </div>

        <!-- RIGHT: Processed Packages -->
        <div class="col-md-7">
            <div class="card h-100">
                <div class="card-header"><i class="bi bi-check-circle me-1"></i> Processed Packages ({{ processed_list|length }})</div>
                <div class="card-body">
                    {% if processed_list %}
                        <p class="mb-2"><small>
                            <span class="badge bg-warning text-dark border me-1">MS</span> = Contains Must Support Elements<br>
                            <span class="badge bg-info text-dark border me-1">Optional MS Ext</span> = Optional Extension with Must Support Sub-Elements
                        </small></p>
                        <p class="mb-2 small text-muted">Resource Types in the list will be both Profile and Base Type:</p>
                        <div class="table-responsive">
                            <table class="table table-sm table-hover">
                                <thead>
                                    <tr><th>Package Name</th><th>Version</th><th>Resource Types</th><th>Actions</th></tr>
                                </thead>
                                <tbody>
                                    {% for processed_ig in processed_list %}
                                        <tr>
                                            <td><code>{{ processed_ig.package_name }}</code></td>
                                            <td>{{ processed_ig.version }}</td>
                                            <td>
                                                {% set types_info = processed_ig.resource_types_info %}
                                                {% if types_info %}
                                                    <div class="d-flex flex-wrap gap-1">
                                                        {% for type_info in types_info %}
                                                            <span class="badge {% if type_info.must_support %}{% if type_info.optional_usage %}bg-info{% else %}bg-warning{% endif %} text-dark{% else %}bg-light text-dark{% endif %} border"
                                                                  title="{% if type_info.must_support %}{% if type_info.optional_usage %}Optional Extension with Must Support{% else %}Has Must Support{% endif %}{% endif %}">{{ type_info.name }}</span>
                                                        {% endfor %}
                                                    </div>
                                                {% else %}
                                                    <small class="text-muted">N/A</small>
                                                {% endif %}
                                            </td>
                                            <td>
                                                <div class="btn-group-vertical btn-group-sm w-80">
                                                    <a href="{{ url_for('view_ig', processed_ig_id=processed_ig.id) }}" class="btn btn-outline-info w-80" title="View Details"><i class="bi bi-search"></i> View</a>
                                                    <form method="POST" action="{{ url_for('unload_ig') }}" style="display:inline;">
                                                        {{ form.csrf_token }}
                                                        <input type="hidden" name="ig_id" value="{{ processed_ig.id }}">
                                                        <button type="submit" class="btn btn-outline-warning w-80 mt-1" onclick="return confirm('Are you sure you want to unload {{ processed_ig.package_name }}#{{ processed_ig.version }}?');"><i class="bi bi-x-lg"></i> Unload</button>
                                                    </form>
                                                </div>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    {% else %}
                        <p class="text-muted">No packages recorded as processed yet.</p>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
{{ super() }}
{% endblock %}