{% extends "main.html" %}

{% block content %}
<div class="container mt-4">
    <h4>Review CARES Import – <small class="text-muted">Archive #{{ import_archive.pk }}</small></h4>

    <!-- Summary statistics -->
    <div class="row mb-3">
        <div class="col">
            <div class="card text-center">
                <div class="card-body p-2">
                    <h5 class="card-title mb-0">{{ summary.new }}</h5>
                    <p class="card-text text-muted small">New</p>
                </div>
            </div>
        </div>
        <div class="col">
            <div class="card text-center">
                <div class="card-body p-2">
                    <h5 class="card-title mb-0">{{ summary.update }}</h5>
                    <p class="card-text text-muted small">Updates</p>
                </div>
            </div>
        </div>
        <div class="col">
            <div class="card text-center">
                <div class="card-body p-2">
                    <h5 class="card-title mb-0">{{ summary.conflict }}</h5>
                    <p class="card-text text-muted small">Conflicts</p>
                </div>
            </div>
        </div>
        <div class="col">
            <div class="card text-center">
                <div class="card-body p-2">
                    <h5 class="card-title mb-0">{{ summary.skip }}</h5>
                    <p class="card-text text-muted small">Unchanged</p>
                </div>
            </div>
        </div>
        <div class="col">
            <div class="card text-center">
                <div class="card-body p-2">
                    <h5 class="card-title mb-0">{{ summary.approved }}</h5>
                    <p class="card-text text-muted small">Approved</p>
                </div>
            </div>
        </div>
        <div class="col">
            <div class="card text-center">
                <div class="card-body p-2">
                    <h5 class="card-title mb-0">{{ summary.rejected }}</h5>
                    <p class="card-text text-muted small">Rejected</p>
                </div>
            </div>
        </div>
        <div class="col">
            <div class="card text-center">
                <div class="card-body p-2">
                    <h5 class="card-title mb-0">{{ summary.pending }}</h5>
                    <p class="card-text text-muted small">Pending</p>
                </div>
            </div>
        </div>
    </div>

    <!-- Filter controls -->
    <form method="GET" class="row g-2 mb-3">
        <div class="col-auto">
            <select name="action" class="form-select form-select-sm">
                <option value="">All Actions</option>
                {% for val, label in action_choices %}
                    <option value="{{ val }}" {% if action_filter == val %}selected{% endif %}>{{ label }}</option>
                {% endfor %}
            </select>
        </div>
        <div class="col-auto">
            <select name="status" class="form-select form-select-sm">
                <option value="">All Statuses</option>
                {% for val, label in status_choices %}
                    <option value="{{ val }}" {% if status_filter == val %}selected{% endif %}>{{ label }}</option>
                {% endfor %}
            </select>
        </div>
        <div class="col-auto">
            <button type="submit" class="btn btn-sm btn-outline-secondary">Filter</button>
            <a href="{% url 'reviewSpeciesImport' import_archive.pk %}" class="btn btn-sm btn-outline-secondary">Clear</a>
        </div>
    </form>

    <!-- Bulk action buttons -->
    <div class="mb-3 d-flex gap-2 flex-wrap">
        <form method="POST" action="{% url 'approveSpeciesImportBatch' import_archive.pk %}">
            {% csrf_token %}
            {% if action_filter %}<input type="hidden" name="action_filter" value="{{ action_filter }}">{% endif %}
            <button type="submit" class="btn btn-success btn-sm">Approve All Pending</button>
        </form>
        <form method="POST" action="{% url 'rejectSpeciesImportBatch' import_archive.pk %}">
            {% csrf_token %}
            {% if action_filter %}<input type="hidden" name="action_filter" value="{{ action_filter }}">{% endif %}
            <button type="submit" class="btn btn-warning btn-sm">Reject All Pending</button>
        </form>
        <a href="{% url 'commitSpeciesImport' import_archive.pk %}" class="btn btn-primary btn-sm">
            Commit Approved ({{ summary.approved }})
        </a>
        <a href="{% url 'importArchiveResults' import_archive.pk %}" class="btn btn-outline-secondary btn-sm">
            Import Log
        </a>
    </div>

    <!-- Staging records table -->
    {% if staging_records %}
    <div class="table-responsive">
        <table class="table table-striped table-sm">
            <thead>
                <tr>
                    <th>Row</th>
                    <th>Species Name</th>
                    <th>Action</th>
                    <th>Review Status</th>
                    <th>Changed Fields</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
            {% for record in staging_records %}
                <tr class="{% if record.action == 'CONFLICT' %}table-danger{% elif record.action == 'NEW' %}table-success{% elif record.action == 'UPDATE' %}table-warning{% endif %}">
                    <td>{{ record.import_row_number }}</td>
                    <td>
                        <em>{{ record.new_name }}</em>
                        {% if record.existing_species %}
                            <br><small class="text-muted">→ <a href="{% url 'caresSpecies' record.existing_species.pk %}">existing</a></small>
                        {% endif %}
                    </td>
                    <td><span class="badge bg-secondary">{{ record.get_action_display }}</span></td>
                    <td>
                        {% if record.review_status == 'APPROVED' %}
                            <span class="badge bg-success">{{ record.get_review_status_display }}</span>
                        {% elif record.review_status == 'OVERRIDE' %}
                            <span class="badge bg-warning text-dark">{{ record.get_review_status_display }}</span>
                        {% elif record.review_status == 'REJECTED' %}
                            <span class="badge bg-danger">{{ record.get_review_status_display }}</span>
                        {% else %}
                            <span class="badge bg-secondary">{{ record.get_review_status_display }}</span>
                        {% endif %}
                    </td>
                    <td>
                        {% if record.changed_fields %}
                            <small>{{ record.changed_fields.keys|join:", " }}</small>
                        {% else %}
                            <small class="text-muted">—</small>
                        {% endif %}
                    </td>
                    <td>
                        <a href="{% url 'reviewSpeciesImportDetail' record.pk %}" class="btn btn-xs btn-outline-primary btn-sm">Review</a>
                    </td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>
    {% else %}
        <div class="alert alert-info">No staging records match the current filter.</div>
    {% endif %}
</div>
{% endblock content %}
