{% extends "base.html" %}
{% set active_nav = "pipelines" %}
{% block title %}{{ name }} — Loom Workshop{% endblock %}
{% block content %}
<hgroup>
    <h1>Pipeline: {{ name }}</h1>
    <p>{{ graph.stage_count }} stages &middot; {{ graph.levels | length }} execution levels</p>
</hgroup>

<h2>Execution Graph</h2>
<div class="pipeline-graph" role="img" aria-label="Pipeline execution graph with {{ graph.levels | length }} levels">
    {% for level in graph.levels %}
    <div class="pipeline-level">
        <small class="level-label">Level {{ loop.index0 }}</small>
        <div class="level-stages">
            {% for stage_name in level %}
            <div class="stage-card">
                <strong>{{ stage_name }}</strong>
                {% for s in config.pipeline_stages %}
                    {% if s.name == stage_name %}
                <br><kbd>{{ s.worker_type }}</kbd>
                    {% if s.get('model_tier') %}<br><small>{{ s.model_tier }}</small>{% endif %}
                    {% endif %}
                {% endfor %}
                {% if graph.dependencies.get(stage_name) %}
                <br><small>depends: {{ graph.dependencies[stage_name] | join(', ') }}</small>
                {% endif %}
            </div>
            {% endfor %}
        </div>
    </div>
    {% if not loop.last %}<div class="level-arrow" aria-hidden="true">&#8595;</div>{% endif %}
    {% else %}
    <p>No stages defined.</p>
    {% endfor %}
</div>

<h2>Stage Operations</h2>

<details>
    <summary>Insert Stage</summary>
    <form method="post" action="/pipelines/{{ name }}/stage">
        <input type="hidden" name="action" value="insert">
        <label for="after_stage">After stage (empty = append)</label>
        <select name="after_stage" id="after_stage">
            <option value="">End of pipeline</option>
            {% for s in config.pipeline_stages %}
            <option value="{{ s.name }}">{{ s.name }}</option>
            {% endfor %}
        </select>
        <label for="stage_yaml_insert">Stage definition (YAML)</label>
        <textarea name="stage_yaml" id="stage_yaml_insert" rows="6" placeholder='name: new_stage
worker_type: summarizer
model_tier: local
input_mapping:
  text: "goal.context.text"'></textarea>
        <button type="submit">Insert</button>
    </form>
</details>

<details>
    <summary>Swap Worker</summary>
    <form method="post" action="/pipelines/{{ name }}/stage">
        <input type="hidden" name="action" value="swap">
        <div class="grid">
            <div>
                <label for="swap_stage">Stage</label>
                <select name="stage_name" id="swap_stage">
                    {% for s in config.pipeline_stages %}
                    <option value="{{ s.name }}">{{ s.name }} ({{ s.worker_type }})</option>
                    {% endfor %}
                </select>
            </div>
            <div>
                <label for="new_worker_type">New worker type</label>
                <input type="text" name="new_worker_type" id="new_worker_type" placeholder="new_worker_type" required>
            </div>
            <div>
                <label for="new_tier">New tier (optional)</label>
                <input type="text" name="new_tier" id="new_tier" placeholder="">
            </div>
        </div>
        <button type="submit">Swap</button>
    </form>
</details>

<details>
    <summary>Remove Stage</summary>
    <form method="post" action="/pipelines/{{ name }}/stage">
        <input type="hidden" name="action" value="remove">
        <label for="remove_stage">Stage to remove</label>
        <select name="stage_name" id="remove_stage">
            {% for s in config.pipeline_stages %}
            <option value="{{ s.name }}">{{ s.name }}</option>
            {% endfor %}
        </select>
        <button type="submit" class="secondary">Remove</button>
    </form>
</details>

<details>
    <summary>Add Parallel Branch</summary>
    <form method="post" action="/pipelines/{{ name }}/stage">
        <input type="hidden" name="action" value="branch">
        <label for="stage_yaml_branch">Branch stage (YAML, must only reference goal.* paths)</label>
        <textarea name="stage_yaml" id="stage_yaml_branch" rows="6" placeholder='name: parallel_task
worker_type: image_extractor
model_tier: local
input_mapping:
  file_ref: "goal.context.file_ref"'></textarea>
        <button type="submit">Add Branch</button>
    </form>
</details>

<h2>Raw Config</h2>
<details>
    <summary>View YAML</summary>
    <pre><code>{{ config | tojson(indent=2) }}</code></pre>
</details>
{% endblock %}
