{% 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 }}/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="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">
      {% set acc_status = row.acceptance_status or 'pending_review' %}
      <div class="flex items-start justify-between gap-3">
        <div>
          <h3 class="text-base font-semibold text-slate-900">Client Acceptance Controls</h3>
          <p class="mt-1 text-sm text-slate-500">Embedded AQMM / peer-review evidence for acceptance, independence, conflict, KYC and engagement letter controls.</p>
        </div>
        <span class="rounded-full px-3 py-1 text-xs font-semibold {% if acc_status == 'approved' %}bg-emerald-100 text-emerald-700{% elif acc_status == 'rejected' %}bg-rose-100 text-rose-700{% else %}bg-amber-100 text-amber-800{% endif %}">{{ acc_status.replace('_',' ').title() }}</span>
      </div>

      <div class="mt-4 grid gap-3 md:grid-cols-2">
        {% for label, ok in [
          ('Acceptance Required', row.acceptance_required),
          ('Independence Check', row.independence_check_completed),
          ('Conflict Check', row.conflict_check_completed),
          ('KYC Completed', row.kyc_completed),
          ('Engagement Letter Required', row.engagement_letter_required),
          ('Engagement Letter Received', row.engagement_letter_received)
        ] %}
        <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 font-medium {% if ok %}text-emerald-700{% else %}text-amber-700{% endif %}">{{ 'Yes' if ok else 'No' }}</div>
        </div>
        {% endfor %}
        <div class="rounded-xl border border-slate-200 px-4 py-3">
          <div class="text-xs font-semibold uppercase tracking-wide text-slate-500">Approved By</div>
          <div class="mt-1 text-sm text-slate-800">{{ row.acceptance_approved_by_user_id or '-' }}</div>
        </div>
        <div class="rounded-xl border border-slate-200 px-4 py-3">
          <div class="text-xs font-semibold uppercase tracking-wide text-slate-500">Approved At</div>
          <div class="mt-1 text-sm text-slate-800">{{ row.acceptance_approved_at_utc or '-' }}</div>
        </div>
      </div>

      {% if row.acceptance_review_notes %}
      <div class="mt-4 rounded-xl border border-slate-200 bg-slate-50 px-4 py-3 text-sm text-slate-700">
        <div class="font-semibold text-slate-900">Review Notes</div>
        <div class="mt-1 whitespace-pre-wrap">{{ row.acceptance_review_notes }}</div>
      </div>
      {% endif %}
      {% if row.acceptance_rejection_reason %}
      <div class="mt-4 rounded-xl border border-rose-200 bg-rose-50 px-4 py-3 text-sm text-rose-800">
        <div class="font-semibold text-rose-900">Rejection / Remediation Notes</div>
        <div class="mt-1 whitespace-pre-wrap">{{ row.acceptance_rejection_reason }}</div>
      </div>
      {% endif %}

      {% if can_approve_acceptance or can_manage_acceptance %}
      <div class="mt-5 grid gap-3 md:grid-cols-3">
        {% if can_approve_acceptance %}
        <form method="post" action="/clients/{{ row.id }}/acceptance/approve" class="space-y-2">
          <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
          <textarea name="acceptance_review_notes" rows="2" placeholder="Approval notes" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">{{ row.acceptance_review_notes or '' }}</textarea>
          <button class="w-full rounded-xl bg-emerald-600 px-4 py-2 text-sm font-semibold text-white hover:bg-emerald-700">Approve Acceptance</button>
        </form>
        <form method="post" action="/clients/{{ row.id }}/acceptance/reject" class="space-y-2">
          <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
          <textarea name="acceptance_rejection_reason" rows="2" placeholder="Rejection/remediation reason" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">{{ row.acceptance_rejection_reason or '' }}</textarea>
          <button class="w-full rounded-xl bg-rose-600 px-4 py-2 text-sm font-semibold text-white hover:bg-rose-700">Reject</button>
        </form>
        {% endif %}
        {% if can_manage_acceptance %}
        <form method="post" action="/clients/{{ row.id }}/acceptance/pending" class="space-y-2">
          <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
          <textarea name="acceptance_review_notes" rows="2" placeholder="Pending review notes" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">{{ row.acceptance_review_notes or '' }}</textarea>
          <button class="w-full rounded-xl border border-amber-300 px-4 py-2 text-sm font-semibold text-amber-800 hover:bg-amber-50">Move to Pending</button>
        </form>
        {% endif %}
      </div>
      {% endif %}
      <div class="mt-6 rounded-2xl border border-indigo-100 bg-indigo-50/50 p-4">
        <div class="flex flex-wrap items-start justify-between gap-3">
          <div>
            <h4 class="text-sm font-semibold uppercase tracking-wide text-indigo-900">Digital Acceptance Workflow</h4>
            <p class="mt-1 text-sm text-indigo-800">Declarations, KYC from permanent documents and engagement letter acceptance are captured as audit evidence.</p>
          </div>
          <span class="rounded-full px-3 py-1 text-xs font-semibold {% if workflow_ready %}bg-emerald-100 text-emerald-700{% else %}bg-amber-100 text-amber-800{% endif %}">{{ 'Ready for Approval' if workflow_ready else 'Pending Evidence' }}</span>
        </div>

        <div class="mt-4 grid gap-4 lg:grid-cols-3">
          <div class="rounded-xl border border-white bg-white p-4 shadow-sm">
            <div class="flex items-center justify-between gap-2">
              <h5 class="text-sm font-semibold text-slate-900">Independence / Conflict Declarations</h5>
              {% if can_manage_acceptance %}
              <form method="post" action="/clients/{{ row.id }}/acceptance/request-declarations">
                <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                <button class="rounded-lg bg-indigo-600 px-3 py-1.5 text-xs font-semibold text-white hover:bg-indigo-700">Request</button>
              </form>
              {% endif %}
            </div>
            <div class="mt-3 space-y-2">
              {% if acceptance_declarations %}
                {% for d in acceptance_declarations %}
                <div class="rounded-lg border border-slate-200 p-3 text-xs">
                  <div class="font-semibold text-slate-800">{{ d.declaration_type.replace('_',' ').title() }} · User #{{ d.assigned_user_id }}</div>
                  <div class="mt-1 {% if d.status == 'declared_clear' %}text-emerald-700{% elif d.status == 'declared_issue' %}text-rose-700{% else %}text-amber-700{% endif %}">{{ d.status.replace('_',' ').title() }}</div>
                  {% if d.assigned_user_id == current_user.id and d.status == 'pending' %}
                  <form method="post" action="/clients/{{ row.id }}/acceptance/declarations/{{ d.id }}/submit" class="mt-2 space-y-2">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                    <select name="declaration_result" class="w-full rounded-lg border border-slate-300 px-2 py-1 text-xs">
                      <option value="clear">Declare Clear / No Issue</option>
                      <option value="issue">Declare Issue / Conflict</option>
                    </select>
                    <textarea name="response_notes" rows="2" placeholder="Notes" class="w-full rounded-lg border border-slate-300 px-2 py-1 text-xs"></textarea>
                    <textarea name="issue_details" rows="2" placeholder="Issue details if any" class="w-full rounded-lg border border-slate-300 px-2 py-1 text-xs"></textarea>
                    <button class="rounded-lg bg-slate-900 px-3 py-1.5 text-xs font-semibold text-white">Submit Declaration</button>
                  </form>
                  {% endif %}
                </div>
                {% endfor %}
              {% else %}
                <div class="text-xs text-slate-500">No declaration requests have been created yet.</div>
              {% endif %}
            </div>
          </div>

          <div class="rounded-xl border border-white bg-white p-4 shadow-sm">
            <div class="flex items-center justify-between gap-2">
              <h5 class="text-sm font-semibold text-slate-900">KYC from Permanent Documents</h5>
              {% if can_manage_acceptance %}
              <form method="post" action="/clients/{{ row.id }}/acceptance/kyc/sync">
                <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                <button class="rounded-lg border border-slate-300 px-3 py-1.5 text-xs font-semibold text-slate-700 hover:bg-slate-50">Sync</button>
              </form>
              {% endif %}
            </div>
            <div class="mt-3 text-xs text-slate-600">Permanent documents found: <span class="font-semibold">{{ permanent_document_count }}</span></div>
            <div class="mt-2 rounded-lg bg-slate-50 p-3 text-xs text-slate-600 whitespace-pre-wrap max-h-40 overflow-auto">{{ permanent_document_summary or 'No permanent document summary available. Click Sync after client uploads permanent documents.' }}</div>
            {% if kyc_verification %}
            <div class="mt-3 text-xs font-semibold {% if kyc_verification.status == 'verified' %}text-emerald-700{% elif kyc_verification.status == 'rejected' %}text-rose-700{% else %}text-amber-700{% endif %}">KYC Status: {{ kyc_verification.status.replace('_',' ').title() }}</div>
            {% endif %}
            {% if can_manage_acceptance or can_approve_acceptance %}
            <div class="mt-3 grid gap-2">
              <form method="post" action="/clients/{{ row.id }}/acceptance/kyc/verify" class="space-y-2">
                <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                <textarea name="verification_notes" rows="2" placeholder="KYC verification notes" class="w-full rounded-lg border border-slate-300 px-2 py-1 text-xs"></textarea>
                <button class="w-full rounded-lg bg-emerald-600 px-3 py-1.5 text-xs font-semibold text-white hover:bg-emerald-700">Verify KYC</button>
              </form>
              <form method="post" action="/clients/{{ row.id }}/acceptance/kyc/reject" class="space-y-2">
                <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                <textarea name="verification_notes" rows="2" placeholder="Resubmission remarks" class="w-full rounded-lg border border-slate-300 px-2 py-1 text-xs"></textarea>
                <button class="w-full rounded-lg border border-rose-300 px-3 py-1.5 text-xs font-semibold text-rose-700 hover:bg-rose-50">Reject / Request Resubmission</button>
              </form>
            </div>
            {% endif %}
          </div>

          <div class="rounded-xl border border-white bg-white p-4 shadow-sm">
            <h5 class="text-sm font-semibold text-slate-900">Engagement Letter</h5>
            {% if engagement_letter %}
              <div class="mt-2 text-xs text-slate-600">Version {{ engagement_letter.version_no }} · <span class="font-semibold">{{ engagement_letter.status.replace('_',' ').title() }}</span></div>
              <a href="/clients/{{ row.id }}/acceptance/engagement-letter/{{ engagement_letter.id }}/download" target="_blank" class="mt-3 inline-flex rounded-lg border border-slate-300 px-3 py-1.5 text-xs font-semibold text-slate-700 hover:bg-slate-50">View / Print PDF</a>
              {% if can_approve_acceptance and engagement_letter.status == 'draft' %}
              <form method="post" action="/clients/{{ row.id }}/acceptance/engagement-letter/{{ engagement_letter.id }}/approve-send" class="mt-3">
                <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                <button class="w-full rounded-lg bg-indigo-600 px-3 py-1.5 text-xs font-semibold text-white hover:bg-indigo-700">Partner Approve & Send</button>
              </form>
              {% endif %}
              {% if (can_manage_acceptance or can_approve_acceptance) and engagement_letter.status == 'manual_uploaded' %}
              <form method="post" action="/clients/{{ row.id }}/acceptance/engagement-letter/{{ engagement_letter.id }}/verify-manual" class="mt-3">
                <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                <button class="w-full rounded-lg bg-emerald-600 px-3 py-1.5 text-xs font-semibold text-white hover:bg-emerald-700">Verify Manual Signed Copy</button>
              </form>
              {% endif %}
            {% else %}
              <div class="mt-2 text-xs text-slate-500">No engagement letter drafted yet.</div>
            {% endif %}
            {% if can_manage_acceptance %}
            <form method="post" action="/clients/{{ row.id }}/acceptance/engagement-letter/draft" class="mt-4 space-y-2">
              <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
              <input name="title" placeholder="Letter title" class="w-full rounded-lg border border-slate-300 px-2 py-1 text-xs" value="Engagement Letter - {{ row.client_name }}">
              <textarea name="body_text" rows="4" placeholder="Leave blank to use standard engagement letter draft" class="w-full rounded-lg border border-slate-300 px-2 py-1 text-xs"></textarea>
              <button class="w-full rounded-lg border border-indigo-300 px-3 py-1.5 text-xs font-semibold text-indigo-700 hover:bg-indigo-50">Create New Draft</button>
            </form>
            {% endif %}
          </div>
        </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 Review Partner:</span> {{ row.default_review_partner_user_id or '-' }}</div>
        <div><span class="font-medium">Consultant:</span> {{ row.assoc_consultant_id or '-' }}</div>
      </div>
    </section>
  </div>
</div>
{% endblock %}
