{% extends 'mastery/base.html' %}
{% load i18n %}
{% load humanize %}
{% load static %}

{% block details %}
<div class="container mt-4">
    <div class="d-flex flex-wrap justify-content-between align-items-start gap-3 mb-4">
        <div>
            <h2 class="mb-1">{% translate "Manage Skill Plans" %}</h2>
            <p class="text-muted mb-0">{% translate "Doctrine-level overview and initialization status." %}</p>
        </div>
    </div>

    <div class="row g-3 mb-4">
        <div class="col-12 col-md-6">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-body">
                    <div class="small text-muted mb-1">{% translate "Doctrines" %}</div>
                    <div class="display-6 fw-semibold">{{ doctrines|length }}</div>
                </div>
            </div>
        </div>
        <div class="col-12 col-md-6">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-body">
                    <div class="small text-muted mb-1">{% translate "Workflow" %}</div>
                    <div class="fw-semibold">{% translate "Initialize missing plans, then sync doctrine after updates." %}</div>
                </div>
            </div>
        </div>
    </div>

    <div class="card border-0 shadow-sm">
        <div class="card-header d-flex justify-content-between align-items-center py-3">
            <div class="fw-semibold">{% translate "Doctrines" %}</div>
            <span class="badge bg-secondary">{% blocktranslate with count=doctrines|length %}{{ count }} total{% endblocktranslate %}</span>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-sm align-middle mb-0">
                    <thead class="mastery-table-head">
                    <tr>
                        <th>{% translate "Name" %}</th>
                        <th>{% translate "Priority" %}</th>
                        <th>{% translate "Default mastery" %}</th>
                        <th>{% translate "Coverage" %}</th>
                        <th>{% translate "Status" %}</th>
                        <th>{% translate "Actions" %}</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for d in doctrines %}
                    <tr>
                        <td>
                            <a class="text-decoration-none" href="{% url 'mastery:doctrine_detail' d.id %}">
                                <div class="d-flex align-items-center gap-2">
                                    <img src="{{ d.icon_url }}" class="rounded" height="40" width="40" alt="{{ d.name }}">
                                    <div>
                                        <div class="fw-semibold text-body">{{ d.name }}</div>
                                        <div class="small text-muted">{{ d.total }} fitting{{ d.total|pluralize }}</div>
                                    </div>
                                </div>
                            </a>
                        </td>
                        <td>
                            {% include 'mastery/partials/_priority_badge.html' with priority=d.priority %}
                            {% if d.priority == 0 %}
                            <span class="text-muted small">P0</span>
                            {% endif %}
                        </td>
                        <td>
                            {% if d.default_mastery_label %}
                            <span class="badge bg-info text-dark">{{ d.default_mastery_label }}</span>
                            {% else %}
                            <span class="text-muted">{% translate "Not set" %}</span>
                            {% endif %}
                        </td>
                        <td>
                            <div class="small text-muted mb-1">{{ d.configured }}/{{ d.total }} configured</div>
                            {% if d.total %}
                            <div class="progress" style="height: 8px;">
                                <div class="progress-bar {% if d.configured == d.total %}bg-success{% else %}bg-warning{% endif %}" role="progressbar" style="width: {% widthratio d.configured d.total 100 %}%"></div>
                            </div>
                            {% endif %}
                        </td>
                        <td>
                            {% if d.initialized %}
                            <span class="badge bg-success">{% translate "Initialized" %}</span>
                            {% else %}
                            <span class="badge bg-warning text-dark">{% translate "Not initialized" %}</span>
                            {% endif %}
                        </td>
                        <td>
                            <div class="d-flex gap-2 flex-wrap">
                                <a class="btn btn-sm btn-outline-secondary" href="{% url 'mastery:doctrine_detail' d.id %}">{% translate "Open" %}</a>
                                {% if not d.initialized %}
                                <form method="POST" action="{% url 'mastery:generate_doctrine' d.id %}">
                                    {% csrf_token %}
                                    <button type="submit" class="btn btn-sm btn-primary">{% translate "Initialize" %}</button>
                                </form>
                                {% else %}
                                <form method="POST" action="{% url 'mastery:sync_doctrine' d.id %}">
                                    {% csrf_token %}
                                    <button type="submit" class="btn btn-sm btn-primary">{% translate "Sync" %}</button>
                                </form>
                                {% endif %}
                            </div>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="6" class="text-muted">{% translate "No doctrines found." %}</td>
                    </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<script>
    $(document).ready(() => {
        $('[data-bs-tooltip="aa-mastery"]').tooltip();
    });
</script>

{% endblock %}