{% extends "base.html" %}
{% block content %}
{% set from_page = request.query_params.get("from", "") %}
{% set from_labels = {"mrs": "Merge Requests", "issues": "Issues", "pipelines": "Pipelines", "vulns": "Vulnerabilities", "events": "Events"} %}
<ul class="breadcrumb">
    <li><a href="/">Dashboard</a></li>
    {% if from_page in from_labels %}
    <li><a href="/{{ from_page }}">{{ from_labels[from_page] }}</a></li>
    {% endif %}
    <li>{{ repo_name }}</li>
</ul>

{% if repo %}
<hgroup>
    <h2>{{ repo.name }}</h2>
    <p>
        <code>{{ repo.branch or "detached" }}</code>
        {% if repo.stale %}<span class="badge-stale">stale</span>{% endif %}
        {% if repo.ahead %}<span class="pull-clean">&uarr;{{ repo.ahead }}</span>{% endif %}
        {% if repo.behind %}<span>&darr;{{ repo.behind }}</span>{% endif %}
        {% if repo.pull_status == "conflict" %}<span class="pull-conflict">&#x26A0; conflict</span>{% endif %}
        {% if repo.remote_url %}&mdash; <a href="{{ repo.remote_url | forge_url }}" target="_blank">{{ (repo.forge_type or "forge") | capitalize }} &#x2197;</a>{% endif %}
    </p>
    {% if repo.default_ahead or repo.default_behind %}
    <p class="card-default-branch">
        {% if repo.default_ahead %}<span class="pull-clean">&uarr;{{ repo.default_ahead }}</span>{% endif %}
        {% if repo.default_behind %}<span>&darr;{{ repo.default_behind }}</span>{% endif %}
        from <code>{{ repo.default_branch }}</code>
    </p>
    {% endif %}
</hgroup>

<div class="repo-actions">
    <button class="outline secondary repo-action-btn" hx-post="/action/fetch/{{ repo.name }}" hx-swap="none"
            data-after="feedback" data-feedback-done="Fetched" data-feedback-idle="Fetch">
        Fetch
    </button>
    {% if repo.pull_status == "clean" %}
    <button class="outline secondary repo-action-btn" hx-post="/action/pull/{{ repo.name }}" hx-swap="none">
        &darr; Pull
    </button>
    {% endif %}
    {% if repo.stale and repo.default_branch %}
    <button class="outline secondary repo-action-btn" hx-post="/action/checkout-default/{{ repo.name }}" hx-swap="none"
            data-after="reload">
        &times; Checkout {{ repo.default_branch }}
    </button>
    {% endif %}
    <button class="outline secondary repo-action-btn repo-action-danger" hx-post="/action/exclude/{{ repo.name }}" hx-swap="none"
            hx-confirm="Exclude {{ repo.name }} from dashboard?"
            data-after="redirect" data-redirect="/">
        Exclude
    </button>
</div>

{% if repo.has_forge %}
<section>
    <h3>Merge Requests</h3>
    <div hx-get="/fragment/repo/{{ repo.name }}/mrs"
         hx-trigger="load, every 5s"
         hx-swap="innerHTML">
    <p class="htmx-indicator-inline" aria-busy="true">Loading from cache…</p>
    </div>
</section>

<section>
    <h3>Issues</h3>
    <div hx-get="/fragment/repo/{{ repo.name }}/issues"
         hx-trigger="load, every 5s"
         hx-swap="innerHTML">
    <p class="htmx-indicator-inline" aria-busy="true">Loading from cache…</p>
    </div>
</section>

<div class="repo-split">
    <section>
        <div class="section-header">
            <h3>Pipelines</h3>
            {% set gl = repo.remote_url | forge_url %}
            {% if repo.forge_type == "gitlab" and gl %}<a class="section-link" href="{{ gl }}/-/pipelines" target="_blank">GitLab &nearr;</a>{% endif %}
        </div>
        <div hx-get="/fragment/repo/{{ repo.name }}/pipelines"
             hx-trigger="load, every 5s"
             hx-swap="innerHTML">
        <p class="htmx-indicator-inline" aria-busy="true">Loading from cache…</p>
        </div>
    </section>

    <section>
        <div class="section-header">
            <h3>Vulnerabilities</h3>
            <span class="section-note">active · default branch</span>
            {% set gl = repo.remote_url | forge_url %}
            {% if repo.forge_type == "gitlab" and gl %}<a class="section-link" href="{{ gl }}/-/security/vulnerability_report" target="_blank">GitLab &nearr;</a>{% endif %}
        </div>
        <div hx-get="/fragment/repo/{{ repo.name }}/vulns"
             hx-trigger="load, every 5s"
             hx-swap="innerHTML">
        <p class="htmx-indicator-inline" aria-busy="true">Loading from cache…</p>
        </div>
    </section>
</div>
{% endif %}

{% else %}
<p>Repo <strong>{{ repo_name }}</strong> not found.</p>
{% endif %}
{% endblock %}
