{% extends "controlpanel/base.html" %}
{% load lucide %}

{% block panel_title %}Jobs{% endblock panel_title %}

{% block breadcrumb %}
    <span class="text-ink">jobs</span>
    <span class="text-edge">|</span>
    <span>{{ jobs|length }} scheduled</span>
{% endblock breadcrumb %}

{% block panel %}
    <div class="rounded border border-info-border bg-info-bg px-4 py-3 text-sm text-info-text">
        {% lucide "info" size=16 class="inline -mt-0.5 mr-1" %}
        These run on Celery Beat's own schedule (see <span class="font-mono">rosterchief/settings.py</span>) — there is no "run now" here, but each job can be paused individually below. A run that never appears at its scheduled time (and isn't paused) is the signal something's wrong with <span class="font-mono">worker</span>/<span class="font-mono">beat</span>, not with this page.
    </div>

    <div class="grid grid-cols-1 gap-4 lg:grid-cols-2 xl:grid-cols-3">
        {% for job in jobs %}
            <div class="card flex flex-col {% if not job.enabled %}opacity-70{% endif %}">
                <div class="flex items-start justify-between gap-3 border-b border-line px-4 py-3">
                    <div>
                        <div class="font-display text-sm font-extrabold tracking-[.08em] text-ink uppercase">{{ job.label }}</div>
                        <div class="mt-0.5 font-mono text-[11px] text-muted">{{ job.name }}</div>
                    </div>
                    <div class="flex shrink-0 flex-col items-end gap-1">
                        {% if not job.enabled %}
                            <span class="badge badge-warning shrink-0">paused</span>
                        {% elif job.latest %}
                            {% if job.latest.status == "success" %}
                                <span class="badge badge-success shrink-0">ok</span>
                            {% elif job.latest.status == "failure" %}
                                <span class="badge badge-error shrink-0">error</span>
                            {% else %}
                                <span class="badge badge-info shrink-0">running</span>
                            {% endif %}
                        {% else %}
                            <span class="badge shrink-0">never run</span>
                        {% endif %}
                    </div>
                </div>
                <div class="flex flex-1 flex-col gap-2 px-4 py-3 text-sm">
                    {# flex-1 here so the paragraph absorbs whatever space a shorter description leaves, keeping the schedule pill pinned to the bottom of the content block across every card in the row instead of drifting with description length. #}
                    <p class="flex-1 text-slate">{{ job.description }}</p>
                    <div class="badge flex items-center gap-1.5 font-mono text-[11px] text-muted">
                        {% lucide "clock" size=12 %} {{ job.schedule }}
                    </div>
                    {% if job.latest %}
                        <div class="mt-1 font-mono text-[11px] text-muted">
                            last run {{ job.latest.started_at|date:"j M Y, H:i" }}
                            {% if job.latest.duration %}· {{ job.latest.duration.total_seconds|floatformat:1 }}s{% endif %}
                        </div>
                        {% if job.latest.status == "success" and job.latest.detail %}
                            <div class="rounded bg-subhead px-2.5 py-2 font-mono text-xs text-slate">{{ job.latest.detail }}</div>
                        {% elif job.latest.status == "failure" and job.latest.error %}
                            <div class="rounded border border-danger-border bg-danger-bg px-2.5 py-2 font-mono text-xs text-club-dark">{{ job.latest.error }}</div>
                        {% endif %}
                    {% endif %}
                </div>

                {% if job.runs|length > 1 %}
                    <div class="flex flex-col gap-1.5 border-t border-rule px-4 py-3">
                        <div class="font-mono text-[10px] tracking-[.06em] text-dim uppercase">Recent runs</div>
                        {% for run in job.runs|slice:":5" %}
                            <div class="flex items-center gap-2 font-mono text-[11px]">
                                <span class="text-dim">{{ run.started_at|date:"d/m H:i" }}</span>
                                {% if run.status == "success" %}
                                    <span class="text-ok-text">ok</span>
                                {% elif run.status == "failure" %}
                                    <span class="text-club-dark">error</span>
                                {% else %}
                                    <span class="text-info-text">running</span>
                                {% endif %}
                            </div>
                        {% endfor %}
                    </div>
                {% endif %}

                <div class="mt-auto border-t border-rule px-4 py-3">
                    <form method="post" action="{% url 'controlpanel:job_toggle' job.name %}">
                        {% csrf_token %}
                        {% if job.enabled %}
                            <button type="submit" class="btn btn-outline btn-sm btn-error w-full gap-2">{% lucide "pause" size=14 %} Pause job</button>
                        {% else %}
                            <button type="submit" class="btn btn-outline btn-sm btn-success w-full gap-2">{% lucide "play" size=14 %} Resume job</button>
                        {% endif %}
                    </form>
                </div>
            </div>
        {% endfor %}
    </div>
{% endblock panel %}
