{% extends "base.html" %}
{% import "macros/ui.html" as m %}
{% block title %}{{ ws_name }} · Workflows{% endblock %}
{% block nav_workspaces %}btn-active{% endblock %}

{% block content %}
{{ m::ws_tabs(ws_name=ws_name, active=ws_active_tab, ws_envs=ws_envs, ws_has_clusters=ws_has_clusters, ws_has_workflows=ws_has_workflows, current_env=current_env) }}

{% if not has_root %}
<div class="alert alert-warning mb-4">
  <span>Workspace path not resolved.</span>
</div>
{% endif %}

{% if env_workflows %}
  {% for ew in env_workflows %}
  <div class="mb-8">
    <h3 class="font-mono font-semibold text-sm mb-3 flex items-center gap-2">
      <svg class="w-3.5 h-3.5 text-primary/70" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
          d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"/>
      </svg>
      {{ ew.env }}
    </h3>

    {% for wf in ew.workflows %}
    <div class="card bg-base-200 shadow mb-4">
      <div class="card-body p-4">
        <div class="flex items-center gap-3 mb-3">
          <h4 class="font-mono font-bold text-primary">{{ wf.name }}</h4>
          <span class="badge badge-ghost badge-xs font-mono">{{ wf.count }} workflow(s)</span>
          <span class="badge badge-outline badge-xs font-mono">{{ wf.steps | length }} steps</span>
        </div>

        {% if wf.steps %}
        <div class="overflow-x-auto">
          <table class="table table-xs w-full font-mono">
            <thead>
              <tr class="text-xs text-base-content/40 uppercase tracking-wider">
                <th>#</th>
                <th>Step</th>
                <th>Targets</th>
                <th>Depends On</th>
                <th>Condition</th>
                <th>On Error</th>
              </tr>
            </thead>
            <tbody>
              {% set_global step_i = 0 %}
              {% for step in wf.steps %}
              {% set_global step_i = step_i + 1 %}
              <tr class="hover">
                <td class="text-base-content/30 w-8">{{ step_i }}</td>
                <td class="font-semibold text-base-content">{{ step.id }}</td>
                <td>
                  {% for t in step.targets %}
                  <span class="badge badge-outline badge-xs mr-1">{{ t }}</span>
                  {% endfor %}
                </td>
                <td>
                  {% if step.depends_on %}
                    {% for dep in step.depends_on %}
                    <span class="badge badge-ghost badge-xs mr-1 text-base-content/60">{{ dep }}</span>
                    {% endfor %}
                  {% else %}
                    <span class="text-base-content/20">—</span>
                  {% endif %}
                </td>
                <td class="text-base-content/50 text-xs max-w-xs truncate">
                  {% if step.condition %}{{ step.condition }}{% else %}<span class="text-base-content/20">—</span>{% endif %}
                </td>
                <td>
                  {% if step.on_error == "Stop" %}
                    <span class="badge badge-error badge-xs">stop</span>
                  {% elif step.on_error == "Continue" %}
                    <span class="badge badge-warning badge-xs">continue</span>
                  {% elif step.on_error %}
                    <span class="badge badge-ghost badge-xs">{{ step.on_error }}</span>
                  {% else %}
                    <span class="text-base-content/20">—</span>
                  {% endif %}
                </td>
              </tr>
              {% endfor %}
            </tbody>
          </table>
        </div>
        {% else %}
        <p class="text-sm text-base-content/40 font-mono">No steps defined.</p>
        {% endif %}
      </div>
    </div>
    {% endfor %}
  </div>
  {% endfor %}

{% elif has_root %}
<div class="alert">
  <span class="text-sm">No workflow definitions found in this workspace.</span>
</div>
{% endif %}
{% endblock %}
