{% extends "base.html" %}
{% import "macros/ui.html" as m %}
{% block title %}{{ ws_name }} · Servers{% 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) }}

<div class="flex items-center gap-2 mb-3">
  <span class="text-xs text-base-content/30 font-mono">Servers{% if current_env %} · {{ current_env }}{% endif %}</span>
  <div class="flex-1"></div>
  <button onclick="liveBtnClick(this)" data-live-url="?{% if current_env %}env={{ current_env }}{% endif %}"
          class="btn btn-ghost btn-xs gap-1 font-mono text-base-content/40 hover:text-base-content"
          title="Reload live server state from hcloud">
    <span class="live-spin loading loading-spinner loading-xs hidden"></span>
    <svg class="live-icon w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
        d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"/>
    </svg>
    <span class="live-label">↺ live</span>
  </button>
</div>

{% if servers and servers.items %}
<div class="overflow-x-auto">
  <table class="table table-sm font-mono">
    <thead>
      <tr class="text-base-content/50 text-xs uppercase tracking-wider">
        <th>Name</th>
        <th>Role</th>
        <th>Provider</th>
        <th>Status</th>
        <th>Deploy</th>
        <th>DAG</th>
        <th>Type</th>
        <th>Location</th>
        <th>Public IP</th>
        <th>Floating IP</th>
        <th>Private IP</th>
        <th>Protection</th>
        <th>Sync</th>
      </tr>
    </thead>
    <tbody>
      {% for srv in servers.items %}
      <tr class="hover">
        <td><a href="/ui/workspaces/{{ ws_name }}/servers/{{ srv.name }}"
               class="font-mono font-semibold text-primary hover:underline">{{ srv.name }}</a></td>
        <td>
          {% if srv.role == "ControlPlane" %}
            <span class="badge badge-primary badge-xs font-mono">CP</span>
          {% elif srv.role == "Worker" %}
            <span class="badge badge-info badge-xs font-mono">worker</span>
          {% elif srv.role == "LoadBalancer" %}
            <span class="badge badge-warning badge-xs font-mono">lb</span>
          {% else %}
            <span class="text-base-content/20">—</span>
          {% endif %}
        </td>
        <td class="text-base-content/50 text-xs font-mono">
          {{ srv.provider | default(value="hetzner") }}
        </td>
        <td>{{ m::server_status(status=srv.status) }}</td>
        <td class="text-xs">
          {% if srv.deploy == "skip" %}
            <span class="badge badge-ghost badge-xs font-mono text-base-content/30">skip</span>
          {% else %}
            <span class="badge badge-success badge-xs font-mono">yes</span>
          {% endif %}
        </td>
        <td class="text-xs font-mono text-base-content/50">
          {% if srv.fsm_state and srv.fsm_state != "—" %}{{ srv.fsm_state }}{% else %}<span class="text-base-content/20">—</span>{% endif %}
        </td>
        <td class="text-base-content/60 text-xs">
          {% if srv.server_type %}{{ srv.server_type.name }}{% else %}—{% endif %}
        </td>
        <td class="text-base-content/50 text-xs">
          {% if srv.datacenter %}{{ srv.datacenter.location.name }}{% else %}—{% endif %}
        </td>
        <td class="text-xs">
          {% if srv.public_net and srv.public_net.ipv4 %}{{ srv.public_net.ipv4.ip }}{% else %}—{% endif %}
        </td>
        <td class="text-xs">
          {% if srv.floating_ip_address %}
            <span class="font-mono text-accent">{{ srv.floating_ip_address }}</span>
            {% if srv.floating_ip %}
            <span class="text-base-content/30 text-xs ml-1">({{ srv.floating_ip }})</span>
            {% endif %}
          {% else %}
            <span class="text-base-content/20">—</span>
          {% endif %}
        </td>
        <td class="text-xs text-base-content/60">
          {% if srv.private_net %}
            {% set pn = srv.private_net | first %}
            {% if pn %}{{ pn.ip }}{% else %}—{% endif %}
          {% else %}—{% endif %}
        </td>
        <td class="text-xs">
          {% if srv.protection and srv.protection.delete %}
            <span class="badge badge-warning badge-xs">del</span>
          {% else %}
            <span class="text-base-content/20">—</span>
          {% endif %}
        </td>
        <td class="text-xs text-base-content/30 font-mono">
          {% if srv.last_sync %}{{ srv.last_sync | truncate(length=16, end="") }}{% else %}—{% endif %}
        </td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
</div>
<p class="text-xs text-base-content/30 mt-3">{{ servers.total }} server(s) — declared in NCL + live hcloud data</p>
{% elif servers %}
<div class="alert">
  <span class="text-sm">No servers declared in servers.ncl for this environment.</span>
</div>
{% else %}
<div class="alert alert-warning">
  <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
      d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-2.194-.833-2.964 0L3.232 16.5c-.77.833.192 2.5 1.732 2.5z"/>
  </svg>
  <span>No workspace configuration found — check that servers.ncl exists for this environment.</span>
</div>
{% endif %}
{% endblock %}
