{% extends "base.html" %}
{% block title %}{{ jobset.name }} - circus{% endblock %}
{% block auth %}
    {% if !auth_name.is_empty() %}
        <span class="auth-user">{{ auth_name }}</span>
        <form method="POST" action="/logout">
            <button class="btn-ghost" type="submit">Logout</button>
        </form>
    {% else %}
        <a class="btn btn-secondary" href="/login">Login</a>
    {% endif %}
{% endblock %}
{% block breadcrumbs %}
    <nav class="breadcrumbs">
        <a href="/projects">Projects</a>
        <span class="sep">/</span>
        <a href="/project/{{ project.id }}">{{ project.name }}</a>
        <span class="sep">/</span>
        <a href="/project/{{ project.id }}#jobsets">Jobsets</a>
        <span class="sep">/</span>
        <span class="current">{{ jobset.name }}</span>
    </nav>
{% endblock %}
{% block content %}
    <h1>{{ jobset.name }}</h1>

    <nav class="tab-nav">
        <a class="active" href="/jobset/{{ jobset.id }}">Evaluations</a>
        <a href="/jobset/{{ jobset.id }}/jobs">Jobs</a>
    </nav>

    {% if is_admin %}
        <div class="page-actions">
            <form class="inline"
                  method="POST"
                  action="/jobset/{{ jobset.id }}/delete"
                  onsubmit="return confirm('Delete this jobset and all of its evaluations/builds?')">
                <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                <button class="btn btn-danger btn-small" type="submit">Delete Jobset</button>
            </form>
        </div>
    {% endif %}

    <dl class="detail-grid">
        <dt>Expression</dt>
        <dd>
            <code>{{ jobset.nix_expression }}</code>
        </dd>
        <dt>Flake mode</dt>
        <dd>
            {% if jobset.flake_mode %}
                Yes
            {% else %}
                No
            {% endif %}
        </dd>
        <dt>State</dt>
        <dd>
            {% match jobset.state %}
                {% when circus_common::models::JobsetState::Disabled %}
                    <span class="badge badge-cancelled">Disabled</span>
                {% when circus_common::models::JobsetState::Enabled %}
                    <span class="badge badge-completed">Enabled</span>
                {% when circus_common::models::JobsetState::OneShot %}
                    <span class="badge badge-pending">One-Shot</span>
                {% when circus_common::models::JobsetState::OneAtATime %}
                    <span class="badge badge-running">One-at-a-Time</span>
            {% endmatch %}
        </dd>
        <dt>Trigger mode</dt>
        <dd>
            {% match jobset.trigger_mode %}
                {% when circus_common::models::JobsetTriggerMode::SourceChange %}
                    Source change
                {% when circus_common::models::JobsetTriggerMode::Interval %}
                    Interval rebuild
            {% endmatch %}
        </dd>
        <dt>Check interval</dt>
        <dd>
            {{ jobset.check_interval }}s
        </dd>
        <dt>Last checked</dt>
        <dd>
            {% if let Some(t) = jobset.last_checked_at %}
                {{ t.format("%Y-%m-%d %H:%M:%S") }}
            {% else %}
                Never
            {% endif %}
        </dd>
    </dl>

    {% if !eval_summaries.is_empty() %}
        <h2>Latest Evaluation</h2>
        {% let latest = eval_summaries[0] %}
        <div class="stats-grid">
            <div class="stat-card">
                <div class="stat-value">{{ latest.succeeded }}</div>
                <div class="stat-label">Succeeded</div>
            </div>
            <div class="stat-card">
                <div class="stat-value">{{ latest.failed }}</div>
                <div class="stat-label">Failed</div>
            </div>
            <div class="stat-card">
                <div class="stat-value">{{ latest.pending }}</div>
                <div class="stat-label">Pending</div>
            </div>
        </div>
    {% endif %}

    <h2>Recent Evaluations</h2>
    {% if eval_summaries.is_empty() %}
        <div class="empty">
            <div class="empty-title">No evaluations yet</div>
            <div class="empty-hint">
                The evaluator will schedule this jobset based on its trigger mode and check interval.
            </div>
        </div>
    {% else %}
        <div class="table-wrap">
            <table>
                <thead>
                    <tr>
                        <th>Commit</th>
                        <th>Status</th>
                        <th>Succeeded</th>
                        <th>Failed</th>
                        <th>Pending</th>
                        <th>Time</th>
                        {% if is_admin %}<th>Actions</th>{% endif %}
                    </tr>
                </thead>
                <tbody>
                    {% for e in eval_summaries %}
                        <tr>
                            <td>
                                <a href="/evaluation/{{ e.id }}">{{ e.commit_short }}</a>
                            </td>
                            <td>
                                <span class="badge badge-{{ e.status_class }}">{{ e.status_text }}</span>
                                {% if e.hidden %}<span class="badge badge-skipped">Hidden</span>{% endif %}
                            </td>
                            <td>{{ e.succeeded }}</td>
                            <td>{{ e.failed }}</td>
                            <td>{{ e.pending }}</td>
                            <td>{{ e.time }}</td>
                            {% if is_admin %}
                                <td>
                                    <form class="inline" method="POST" action="/evaluation/{{ e.id }}/visibility">
                                        <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                                        <input type="hidden" name="return_to" value="/jobset/{{ jobset.id }}">
                                        {% if e.hidden %}
                                            <input type="hidden" name="hidden" value="false">
                                            <button class="btn btn-small btn-secondary" type="submit">Unhide</button>
                                        {% else %}
                                            <input type="hidden" name="hidden" value="true">
                                            <button class="btn btn-small btn-secondary" type="submit">Hide</button>
                                        {% endif %}
                                    </form>
                                </td>
                            {% endif %}
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    {% endif %}
{% endblock %}
