{% extends "ui/templates/base/layout.html" %}
{% block content %}
<div class="space-y-6">
  <div class="flex flex-col gap-4 md:flex-row md:items-center md:justify-between">
    <div>
      <h2 class="text-2xl font-semibold text-slate-900">{{ row.client_name }}</h2>
      <p class="text-sm text-slate-500">{{ row.client_code }} • {{ row.client_type }} • {{ row.status|title }}</p>
    </div>

    <div class="flex flex-wrap gap-3">
      {% if can_edit %}
      <a href="/clients/{{ row.id }}/business-structure" class="rounded-xl border border-emerald-300 px-4 py-2 text-sm font-medium text-emerald-700 hover:bg-emerald-50">Business Structure</a>
      <a href="/registrations/clients/{{ row.id }}" class="rounded-xl border border-amber-300 px-4 py-2 text-sm font-medium text-amber-700 hover:bg-amber-50">Registrations</a>
      <a href="/client-identity/clients/{{ row.id }}" class="rounded-xl border border-indigo-300 px-4 py-2 text-sm font-medium text-indigo-700 hover:bg-indigo-50">Portal Identity</a>
      <a href="/clients/{{ row.id }}/edit"
         class="rounded-xl border border-slate-300 px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">
        Edit
      </a>
      {% endif %}

      {% if can_activate and row.status != 'active' and row.status != 'archived' %}
      <form method="post" action="/clients/{{ row.id }}/activate">
        <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
        <button class="rounded-xl border border-emerald-300 px-4 py-2 text-sm font-medium text-emerald-700 hover:bg-emerald-50">
          Activate
        </button>
      </form>
      {% endif %}

      {% if can_deactivate and row.status == 'active' %}
      <form method="post" action="/clients/{{ row.id }}/deactivate">
        <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
        <button class="rounded-xl border border-rose-300 px-4 py-2 text-sm font-medium text-rose-700 hover:bg-rose-50">
          Deactivate
        </button>
      </form>
      {% endif %}

      {% if can_archive and row.status != 'archived' %}
      <form method="post" action="/clients/{{ row.id }}/archive">
        <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
        <button class="rounded-xl border border-amber-300 px-4 py-2 text-sm font-medium text-amber-800 hover:bg-amber-50">
          Archive
        </button>
      </form>
      {% endif %}

      {% if can_restore and row.status == 'archived' %}
      <form method="post" action="/clients/{{ row.id }}/restore">
        <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
        <button class="rounded-xl border border-sky-300 px-4 py-2 text-sm font-medium text-sky-700 hover:bg-sky-50">
          Restore
        </button>
      </form>
      {% endif %}
    </div>
  </div>

  {% if form_errors %}
  <div class="rounded-2xl border border-rose-200 bg-rose-50 p-4 text-sm text-rose-800">
    {% for err in form_errors %}<div>{{ err }}</div>{% endfor %}
  </div>
  {% endif %}

  <div class="rounded-2xl border border-slate-200 bg-white p-5 shadow-soft"><div class="text-xs font-semibold uppercase tracking-wide text-slate-500">Client Group</div>{% if client_group %}<div class="mt-2 text-lg font-semibold text-slate-900"><a class="text-brand-700 hover:underline" href="/client-groups/{{ client_group.id }}">{{ client_group.group_name }}</a></div><div class="mt-1 text-sm text-slate-500">{{ client_group.group_code }} · {{ row.group_relationship or client_group.group_type }}{% if row.is_group_head %} · Group Head{% endif %}</div>{% else %}<div class="mt-2 text-sm text-slate-500">Not linked to a group.</div>{% endif %}</div>
<div class="grid gap-6 xl:grid-cols-3">
    <section class="rounded-2xl bg-white p-6 shadow-soft xl:col-span-2">
      <h3 class="text-base font-semibold text-slate-900">Profile</h3>
      <div class="mt-4 grid gap-4 md:grid-cols-2">
        {% for label, value in [
          ('Trade Name', row.trade_name),
          ('PAN', row.pan),
          ('GSTIN', row.gstin),
          ('TAN', row.tan),
          ('Contact Person', row.contact_person_name),
          ('Designation', row.contact_person_designation),
          ('Mobile', row.mobile),
          ('Email', row.email)
        ] %}
        <div class="rounded-xl border border-slate-200 px-4 py-3">
          <div class="text-xs font-semibold uppercase tracking-wide text-slate-500">{{ label }}</div>
          <div class="mt-1 text-sm text-slate-800">{{ value or '-' }}</div>
        </div>
        {% endfor %}
      </div>
    </section>

    <section class="rounded-2xl bg-white p-6 shadow-soft xl:col-span-2">
      <div class="flex flex-col gap-4 md:flex-row md:items-start md:justify-between">
        <div>
          <h3 class="text-base font-semibold text-slate-900">Client Onboarding & Permanent Documents</h3>
          <p class="mt-1 text-sm text-slate-500">Client-master onboarding is limited to legal/KYC records and permanent documents. Engagement-specific AQMM formalities are managed from each engagement according to its existing Assurance / Non-Assurance classification.</p>
        </div>
        <span class="shrink-0 rounded-full px-3 py-1 text-xs font-semibold {% if permanent_document_count %}bg-emerald-100 text-emerald-700{% else %}bg-amber-100 text-amber-800{% endif %}">
          {{ permanent_document_count }} permanent document{{ '' if permanent_document_count == 1 else 's' }}
        </span>
      </div>

      <div class="mt-5 grid gap-4 md:grid-cols-2">
        <div class="rounded-2xl border border-slate-200 bg-slate-50 p-4">
          <h4 class="text-sm font-semibold text-slate-900">Permanent KYC Documents</h4>
          <p class="mt-1 text-sm text-slate-600">Upload PAN, registration/constitution records, authorised-person KYC, bank proof and other standing documents. The checklist adapts to the client’s legal constitution.</p>
          <a href="/documents/permanent/clients/{{ row.id }}?onboarding=1" class="mt-4 inline-flex rounded-xl bg-brand-600 px-4 py-2 text-sm font-semibold text-white hover:bg-brand-700">Open Onboarding Documents</a>
        </div>

        <div class="rounded-2xl border border-indigo-200 bg-indigo-50 p-4">
          <h4 class="text-sm font-semibold text-indigo-950">AQMM at Engagement Level</h4>
          <p class="mt-1 text-sm text-indigo-800">Create or open an engagement to complete the applicable acceptance, independence, conflict, engagement-letter, evidence, review and closure requirements. The existing engagement classification remains the single source of truth.</p>
          <a href="/services/engagements/new?client_id={{ row.id }}" class="mt-4 inline-flex rounded-xl border border-indigo-300 bg-white px-4 py-2 text-sm font-semibold text-indigo-700 hover:bg-indigo-100">Assign Service / Create Engagement</a>
        </div>
      </div>
    </section>

    <section class="rounded-2xl bg-white p-6 shadow-soft">
      <h3 class="text-base font-semibold text-slate-900">Association</h3>
      <div class="mt-4 space-y-2 text-sm text-slate-700">
        <div><span class="font-medium">Type:</span> {{ row.association_type or 'legacy_firm' }}</div>
        <div><span class="font-medium">Source:</span> {{ row.assoc_created_source or 'legacy' }}</div>
        <div><span class="font-medium">Audit Firm:</span> {{ row.assoc_firm_tenant_id or row.tenant_id or '-' }}</div>
        <div><span class="font-medium">Branch:</span> {{ row.branch_id or '-' }}</div>
        <div><span class="font-medium">Partner:</span> {{ row.assoc_partner_user_id or row.partner_id or '-' }}</div>
        <div><span class="font-medium">Default Performing Partner:</span> {{ row.default_performing_partner_user_id or row.partner_id or '-' }}</div>
        <div><span class="font-medium">Default Review Partner:</span> {{ row.default_review_partner_user_id or '-' }}</div>
        <div><span class="font-medium">Referred by:</span> {{ consultant_summary.referred_by.contact_person if consultant_summary and consultant_summary.referred_by else 'Direct / Not recorded' }}</div>
        <div><span class="font-medium">Primary consultant:</span> {{ consultant_summary.primary.contact_person if consultant_summary and consultant_summary.primary else 'Firm managed' }}</div>
        <div><span class="font-medium">Communication:</span> {{ (row.communication_routing_mode or 'client_and_consultant')|replace('_', ' ')|title }}</div>
        <div><span class="font-medium">Referral status:</span> {{ (row.referral_status or 'active')|title }}</div>
      </div>
    </section>
  </div>
</div>
{% endblock %}
