{% extends "ui/templates/base/layout.html" %}
{% block content %}
<div class="space-y-6">
  <div class="flex flex-col gap-3 md:flex-row md:items-end md:justify-between">
    <div>
      <h1 class="text-2xl font-semibold text-slate-900">Tenant Subdomains</h1>
      <p class="mt-1 text-sm text-slate-500">Create audit firm platform subdomains like <strong>auditfirm.filingabc.com</strong>. This uses your existing domain_mappings table.</p>
    </div>
    <a href="/domains" class="rounded-xl border px-4 py-2 text-sm text-slate-700">Back to Domains</a>
  </div>

  {% if errors %}
  <div class="rounded-2xl border border-rose-200 bg-rose-50 p-4 text-sm text-rose-800">
    <div class="font-semibold">Please fix the following:</div>
    <ul class="mt-2 list-disc pl-5">{% for e in errors %}<li>{{ e }}</li>{% endfor %}</ul>
  </div>
  {% endif %}
  {% if message %}<div class="rounded-2xl border border-amber-200 bg-amber-50 p-4 text-sm text-amber-800">{{ message }}</div>{% endif %}

  <form method="get" class="rounded-2xl border bg-white p-4 shadow-soft">
    <div class="grid grid-cols-1 gap-3 md:grid-cols-4">
      <label class="grid gap-1 md:col-span-2">
        <span class="text-xs font-medium uppercase tracking-wide text-slate-500">Platform base domain</span>
        <input class="rounded-xl border px-3 py-2 text-sm" name="base_domain" value="{{ base_domain }}" placeholder="filingabc.com" />
      </label>
      <div class="flex items-end"><button class="rounded-xl bg-slate-800 px-4 py-2 text-sm font-medium text-white">Refresh Suggestions</button></div>
    </div>
    <p class="mt-3 text-xs text-slate-500">For local testing you can use <code>filingabc.local</code>. For production use <code>filingabc.com</code> and configure wildcard DNS/SSL for <code>*.filingabc.com</code>.</p>
  </form>

  <div class="rounded-2xl border bg-white p-4 shadow-soft">
    <h2 class="text-lg font-semibold text-slate-900">Audit firm subdomain candidates</h2>
    <div class="mt-4 overflow-hidden rounded-xl border">
      <table class="w-full text-sm">
        <thead class="bg-slate-50 text-slate-600">
          <tr>
            <th class="p-3 text-left">Audit Firm</th>
            <th class="p-3 text-left">Suggested Subdomain</th>
            <th class="p-3 text-left">Current Status</th>
            <th class="p-3 text-left">Action</th>
          </tr>
        </thead>
        <tbody>
          {% for item in candidates %}
          {% set tenant = item.tenant %}
          {% set mapping = item.mapping %}
          <tr class="border-t align-top">
            <td class="p-3">
              <div class="font-semibold text-slate-900">{{ tenant.display_name or tenant.name }}</div>
              <div class="text-xs text-slate-500">Code: {{ tenant.code }} · ID: {{ tenant.id }}</div>
            </td>
            <td class="p-3">
              <code class="rounded-lg bg-slate-100 px-2 py-1 text-xs">{{ item.domain_name }}</code>
            </td>
            <td class="p-3">
              {% if mapping %}
                <div><span class="rounded-full bg-emerald-50 px-2.5 py-1 text-xs font-medium text-emerald-700">Created</span></div>
                <div class="mt-2 text-xs text-slate-500">{{ mapping.status|title }}{% if mapping.is_verified %} · Verified{% else %} · Pending verification{% endif %}</div>
              {% else %}
                <span class="rounded-full bg-slate-100 px-2.5 py-1 text-xs font-medium text-slate-700">Not created</span>
              {% endif %}
            </td>
            <td class="p-3">
              {% if mapping %}
                <a href="/domains/{{ mapping.id }}" class="rounded-xl border px-3 py-2 text-xs font-medium text-slate-700">Open Mapping</a>
              {% else %}
                <form method="post" action="/domains/tenant-subdomains/create" class="space-y-2">
                  <input type="hidden" name="csrf_token" value="{{ csrf_token }}" />
                  <input type="hidden" name="tenant_id" value="{{ tenant.id }}" />
                  <input type="hidden" name="base_domain" value="{{ base_domain }}" />
                  <label class="flex items-center gap-2 text-xs text-slate-600"><input type="checkbox" name="mark_verified_active" checked /> Mark active/verified</label>
                  <button class="rounded-xl bg-slate-900 px-3 py-2 text-xs font-medium text-white">Create Subdomain</button>
                </form>
              {% endif %}
            </td>
          </tr>
          {% else %}
          <tr><td colspan="4" class="p-8 text-center text-slate-500">No audit firms found.</td></tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
  </div>

  <div class="rounded-2xl border border-blue-100 bg-blue-50 p-4 text-sm text-blue-900">
    <div class="font-semibold">Deployment note</div>
    <p class="mt-1">This phase creates and resolves tenant subdomain mappings in the ERP. DNS/SSL is still deployment-level: point <code>*.{{ base_domain }}</code> to your server and configure wildcard SSL in Coolify/Caddy/Traefik/Nginx as applicable.</p>
  </div>
</div>
{% endblock %}
