{% extends "base.html.jinja" %}

{% block content %}
<section class="max-w-4xl mx-auto px-4 py-12">
    <div class="flex items-center justify-between mb-8">
        <h1 class="text-2xl font-bold">Destinations</h1>
    </div>

    {# ── Create destination form (admin only) ─────────────────── #}
    {% if is_admin %}
    <div class="border border-gray-200 rounded-lg p-5 mb-6">
        <h2 class="text-sm font-medium text-gray-900 mb-3">Add destination</h2>
        <form method="POST" action="/orgs/{{ org_name }}/destinations" class="flex items-end gap-3 flex-wrap">
            <input type="hidden" name="_csrf" value="{{ csrf_token }}">
            <div class="flex-1 min-w-[140px]">
                <label for="dest-name" class="block text-xs text-gray-500 mb-1">Name</label>
                <input type="text" id="dest-name" name="name" required placeholder="my-service" class="w-full border border-gray-300 rounded-md px-3 py-1.5 text-sm focus:outline-none focus:ring-2 focus:ring-gray-900 focus:border-transparent">
            </div>
            <div class="min-w-[140px]">
                <label for="dest-env" class="block text-xs text-gray-500 mb-1">Environment</label>
                <select id="dest-env" name="environment" class="w-full border border-gray-300 rounded-md px-3 py-1.5 text-sm focus:outline-none focus:ring-2 focus:ring-gray-900 focus:border-transparent">
                    <option value="staging">staging</option>
                    <option value="preprod">preprod</option>
                    <option value="production">production</option>
                </select>
            </div>
            <button type="submit" class="px-4 py-1.5 bg-gray-900 text-white text-sm rounded-md hover:bg-gray-800">Add</button>
        </form>
    </div>
    {% endif %}

    {# ── Destination list ─────────────────────────────────────── #}
    {% if destinations | length > 0 %}
    <div class="space-y-2">
        {% for dest in destinations %}
        <div class="border border-gray-200 rounded-lg px-5 py-4 flex items-center justify-between">
            <div class="flex items-center gap-3 min-w-0">
                <span class="w-2 h-2 rounded-full shrink-0 {% if dest.environment == 'production' %}bg-purple-500{% elif dest.environment == 'preprod' %}bg-orange-500{% elif dest.environment == 'staging' %}bg-yellow-500{% else %}bg-gray-400{% endif %}"></span>
                <div class="min-w-0">
                    <div class="flex items-center gap-2">
                        <span class="font-medium text-gray-900">{{ dest.name }}</span>
                        <span class="text-xs px-1.5 py-0.5 rounded-full {% if dest.environment == 'production' %}bg-purple-100 text-purple-800{% elif dest.environment == 'preprod' %}bg-orange-100 text-orange-800{% elif dest.environment == 'staging' %}bg-yellow-100 text-yellow-800{% else %}bg-gray-100 text-gray-600{% endif %}">{{ dest.environment }}</span>
                    </div>
                    {% if dest.artifact_title %}
                    <p class="text-sm text-gray-500 mt-0.5">
                        Last: <a href="/orgs/{{ org_name }}/projects/{{ dest.project_name }}/releases/{{ dest.artifact_slug }}" class="text-gray-700 hover:text-black underline">{{ dest.artifact_title }}</a>
                        in <a href="/orgs/{{ org_name }}/projects/{{ dest.project_name }}" class="text-gray-700 hover:text-black underline">{{ dest.project_name }}</a>
                    </p>
                    {% endif %}
                </div>
            </div>
            {% if dest.created_at %}
            <span class="text-xs text-gray-400 shrink-0" title="{{ dest.created_at | datetime }}">{{ dest.created_at | timeago }}</span>
            {% endif %}
        </div>
        {% endfor %}
    </div>
    {% else %}
    <div class="border border-gray-200 rounded-lg p-8 text-center text-gray-500">
        <p class="font-medium text-gray-700">No destinations yet</p>
        <p class="mt-1 text-sm">Destinations appear when releases are deployed with <code class="bg-gray-100 px-1.5 py-0.5 rounded text-gray-700">forest release create --dest</code></p>
    </div>
    {% endif %}
</section>
{% endblock %}
