{% extends "core/base.html" %}
{% block title %}Review Suggested Matches{% endblock %}

{% block content %}
<div class="container py-4">
    <h2 class="mb-4">🔍 Review Suggested Matches</h2>
    <p>
        Confirm the suggested scientific‐name replacements below.
        Only checked boxes (“Confirm”) will override the raw user input.
    </p>

    {# Display flash messages (error, success, etc.) #}
    {% with messages = get_flashed_messages(with_categories=true) %}
        {% if messages %}
            {% for category, message in messages %}
                <div
                    class="alert alert-{{ 'danger' if category == 'error' else category }} alert-dismissible fade show"
                    role="alert"
                >
                    {{ message }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            {% endfor %}
        {% endif %}
    {% endwith %}

    {% if review_list and review_list|length > 0 %}
        <form method="POST">
            {# Hidden CSRF token #}
            <input type="hidden" name="csrf_token" value="{{ csrf_token }}">

            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>Common Name</th>
                        <th>User Input (Scientific Name)</th>
                        <th>Suggested Match</th>
                        <th>Confirm</th>
                    </tr>
                </thead>
                <tbody>
                    {% for row in review_list %}
                        <tr>
                            <td>{{ row.name }}</td>
                            <td>{{ row.sci_name }}</td>
                            <td>{{ row.suggested or '-' }}</td>
                            <td>
                                {% if row.suggested %}
                                    <input
                                        type="checkbox"
                                        name="confirm_{{ row.uuid }}"
                                        aria-label="Confirm suggested match for {{ row.uuid }}"
                                    >
                                {% else %}
                                    &mdash;
                                {% endif %}
                            </td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>

            <button type="submit" class="btn btn-success">Confirm &amp; Import</button>
            <a href="{{ url_for('utility.upload') }}" class="btn btn-secondary ms-2">Cancel</a>
        </form>
    {% else %}
        <div class="alert alert-info">
            No rows to review. <a href="{{ url_for('utility.upload') }}">Upload another CSV?</a>
        </div>
    {% endif %}
</div>
{% endblock %}
