{% extends "ui/templates/base/layout.html" %}
{% block content %}
<div class="space-y-6">
  <div class="flex flex-wrap items-center justify-between gap-3">
    <div>
      <h2 class="text-xl font-semibold text-slate-900">Permanent Client Documents</h2>
      <p class="text-sm text-slate-500">{{ client.client_name }} · {{ client.client_code or '-' }}</p>
    </div>
    <div class="flex flex-wrap gap-2">
      <a href="/documents/permanent" class="rounded-xl border border-slate-300 px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Back</a>
      <a href="/documents" class="rounded-xl border border-slate-300 px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Engagement Documents</a>
    </div>
  </div>

  {% if not request.query_params.get('onboarding') %}
  <div class="rounded-2xl border border-sky-200 bg-sky-50 p-4 text-sm text-sky-800">
    Permanent documents belong to the client master. Acceptance, independence, conflict, engagement-letter and other AQMM requirements are completed from the applicable engagement according to its Assurance / Non-Assurance classification.
  </div>
  {% endif %}

  {% if request.query_params.get('onboarding') %}
  <section class="rounded-2xl border border-indigo-200 bg-indigo-50 p-5 shadow-soft">
    <div class="flex flex-col gap-4 lg:flex-row lg:items-start lg:justify-between">
      <div>
        <div class="text-xs font-semibold uppercase tracking-[0.18em] text-indigo-700">Client Onboarding · Permanent Documents</div>
        <h3 class="mt-1 text-lg font-semibold text-indigo-950">Complete the legal/KYC document checklist</h3>
        <p class="mt-1 max-w-3xl text-sm text-indigo-800">The client master has been created. Upload the standing documents below using the existing permanent-document, versioning and branch-local-storage workflow. Engagement-specific AQMM formalities are intentionally completed only inside the relevant Assurance / Non-Assurance engagement.</p>
      </div>
      <div class="shrink-0 rounded-xl bg-white px-4 py-3 text-center shadow-sm">
        <div class="text-xs font-semibold uppercase tracking-wide text-slate-500">Onboarding status</div>
        <div class="mt-1 text-sm font-semibold {{ 'text-emerald-700' if onboarding_complete else 'text-amber-700' }}">{{ 'Document checklist complete' if onboarding_complete else 'Documents pending' }}</div>
      </div>
    </div>

    <div class="mt-5 grid gap-3 md:grid-cols-2 xl:grid-cols-3">
      {% for item in onboarding_checklist %}
      <div class="rounded-xl border px-4 py-3 {{ 'border-emerald-200 bg-emerald-50' if item.complete else 'border-amber-200 bg-white' }}">
        <div class="flex items-start gap-3">
          <div class="mt-0.5 flex h-6 w-6 shrink-0 items-center justify-center rounded-full text-xs font-bold {{ 'bg-emerald-600 text-white' if item.complete else 'bg-amber-100 text-amber-800' }}">{{ '✓' if item.complete else '!' }}</div>
          <div>
            <div class="text-sm font-semibold text-slate-900">{{ item.label }}</div>
            <div class="mt-1 text-xs {{ 'text-emerald-700' if item.complete else 'text-amber-700' }}">{{ 'Uploaded' if item.complete else 'Required to complete onboarding' }}</div>
          </div>
        </div>
      </div>
      {% endfor %}
    </div>

    <div class="mt-5 flex flex-wrap gap-3">
      {% if onboarding_complete %}
      <a href="/clients/{{ client.id }}" class="rounded-xl bg-emerald-600 px-4 py-2 text-sm font-semibold text-white hover:bg-emerald-700">Complete Onboarding</a>
      {% else %}
      <a href="#permanent-upload-form" class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-semibold text-white hover:bg-brand-700">Upload Required Documents</a>
      <a href="/clients/{{ client.id }}" class="rounded-xl border border-slate-300 bg-white px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Save and Continue Later</a>
      {% endif %}
      <a href="/services/engagements/new?client_id={{ client.id }}" class="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>
  </section>
  {% endif %}

  {% if request.query_params.get('uploaded') %}<div class="rounded-2xl border border-emerald-200 bg-emerald-50 p-4 text-sm text-emerald-800">Permanent document uploaded successfully.</div>{% endif %}
  {% if request.query_params.get('deleted') %}<div class="rounded-2xl border border-amber-200 bg-amber-50 p-4 text-sm text-amber-800">Permanent document archived successfully.</div>{% endif %}
  {% if request.query_params.get('download_queued') %}<div class="rounded-2xl border border-sky-200 bg-sky-50 p-4 text-sm text-sky-800">The file is stored in branch local storage. Retrieval request #{{ request.query_params.get('download_queued') }} has been queued.</div>{% endif %}
  {% if request.query_params.get('error') %}<div class="rounded-2xl border border-red-200 bg-red-50 p-4 text-sm text-red-800">Action failed. Please check file size, permission and storage path.</div>{% endif %}

  {% if can_upload %}
  <section id="permanent-upload-form" class="rounded-2xl bg-white p-5 shadow-soft">
    <h3 class="text-sm font-semibold text-slate-900">Upload Permanent Document / New Revision</h3>
    <form method="post" action="/documents/permanent/clients/{{ client.id }}/upload" enctype="multipart/form-data" class="mt-4 grid gap-4 md:grid-cols-2">
      <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
      <div>
        <label class="mb-1 block text-xs font-semibold uppercase tracking-wide text-slate-500">Title</label>
        <input type="text" name="title" placeholder="Document title" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
      </div>
      <div>
        <label class="mb-1 block text-xs font-semibold uppercase tracking-wide text-slate-500">Category</label>
        <select name="category" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
          {% for item in permanent_document_categories %}<option value="{{ item }}">{{ item }}</option>{% endfor %}
        </select>
      </div>
      <div>
        <label class="mb-1 block text-xs font-semibold uppercase tracking-wide text-slate-500">Existing Document</label>
        <select name="existing_document_id" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
          <option value="">Create new document</option>
          {% for doc in documents %}<option value="{{ doc.id }}">Add revision to {{ doc.title }} (v{{ doc.current_version_no }})</option>{% endfor %}
        </select>
      </div>
      <div>
        <label class="mb-1 block text-xs font-semibold uppercase tracking-wide text-slate-500">File</label>
        <input type="file" name="file" required class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm">
      </div>
      <div class="md:col-span-2">
        <label class="mb-1 block text-xs font-semibold uppercase tracking-wide text-slate-500">Description</label>
        <textarea name="description" rows="2" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm"></textarea>
      </div>
      <div class="md:col-span-2">
        <label class="mb-1 block text-xs font-semibold uppercase tracking-wide text-slate-500">Revision Remarks</label>
        <textarea name="remarks" rows="2" class="w-full rounded-xl border border-slate-300 px-3 py-2 text-sm"></textarea>
      </div>
      <div class="md:col-span-2"><button class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-medium text-white hover:bg-brand-700">Upload</button></div>
    </form>
  </section>
  {% endif %}

  <div class="overflow-hidden rounded-2xl bg-white shadow-soft">
    <table class="min-w-full divide-y divide-slate-200">
      <thead class="bg-slate-50">
        <tr>
          <th class="px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide text-slate-500">Document</th>
          <th class="px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide text-slate-500">Category</th>
          <th class="px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide text-slate-500">Latest Version</th>
          <th class="px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide text-slate-500">Storage Path</th>
          <th class="px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide text-slate-500">Versions</th>
          <th class="px-4 py-3 text-right text-xs font-semibold uppercase tracking-wide text-slate-500">Action</th>
        </tr>
      </thead>
      <tbody class="divide-y divide-slate-100">
        {% for doc in documents %}
        {% set latest = doc.versions[0] if doc.versions else None %}
        <tr>
          <td class="px-4 py-3 text-sm"><div class="font-medium text-slate-900">{{ doc.title }}</div><div class="text-xs text-slate-500">{{ doc.document_code }}{% if doc.description %} · {{ doc.description }}{% endif %}</div></td>
          <td class="px-4 py-3 text-sm text-slate-700">{{ doc.category }}</td>
          <td class="px-4 py-3 text-xs text-slate-600">{% if latest %}<div>v{{ latest.version_no }} · {{ latest.original_filename }}</div><div>{{ (latest.file_size_bytes / 1024)|round(1) }} KB · {{ latest.uploaded_at_utc }}</div>{% else %}-{% endif %}</td>
          <td class="px-4 py-3 text-xs text-slate-600">{% if latest %}<div><span class="rounded-full bg-slate-100 px-2 py-1 text-slate-700">{{ latest.storage_status.replace('_',' ').title() }}</span></div><div class="mt-1 font-mono text-[11px] break-all">{{ latest.local_relative_path }}</div>{% else %}-{% endif %}</td>
          <td class="px-4 py-3 text-xs text-slate-600">{% for v in doc.versions %}<div><a href="/documents/permanent/versions/{{ v.id }}/download" class="text-brand-700 hover:underline">v{{ v.version_no }}</a> · {{ v.original_filename }}{% if v.remarks %} · {{ v.remarks }}{% endif %}</div>{% endfor %}</td>
          <td class="px-4 py-3 text-right text-sm">
            {% if latest %}<a href="/documents/permanent/{{ doc.id }}/download" class="font-medium text-brand-700 hover:underline">Download Latest</a>{% endif %}
            {% if can_upload %}
            <form method="post" action="/documents/permanent/{{ doc.id }}/delete" class="ml-3 inline" onsubmit="return confirm('Archive this permanent document?');">
              <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
              <button class="font-medium text-red-600 hover:underline">Archive</button>
            </form>
            {% endif %}
          </td>
        </tr>
        {% else %}
        <tr><td colspan="6" class="px-4 py-8 text-center text-sm text-slate-500">No permanent documents uploaded for this client.</td></tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
</div>
{% endblock %}
