{% extends "ui/templates/base/layout.html" %}
{% block content %}
{% include "modules/consultants/templates/consultants/_consultant_tabs.html" %}
<div class="space-y-6">
  <div class="flex flex-wrap items-start justify-between gap-4">
    <div>
      <h2 class="text-xl font-semibold text-slate-900">{{ client.client_name }} — {{ catalogue.service_name }}</h2>
      <p class="text-sm text-slate-500">{{ task.task_name }}{% if task.internal_target_date %} • Target {{ task.internal_target_date.strftime('%d-%m-%Y') }}{% endif %}</p>
    </div>
    <a href="/consultant/work" class="rounded-xl border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Back to My Work Board</a>
  </div>

  <div class="grid gap-6 xl:grid-cols-[1fr_360px]">
    <div class="space-y-6">
      <div class="rounded-2xl border border-slate-200 bg-white p-5 shadow-soft">
        <div class="grid gap-4 md:grid-cols-4">
          <div><div class="text-xs uppercase tracking-wide text-slate-500">Task Status</div><div class="mt-1 font-semibold text-slate-900">{{ task.status.replace('_',' ').title() }}</div></div>
          {% if link.can_view_assignee and task.assigned_to %}<div><div class="text-xs uppercase tracking-wide text-slate-500">Assigned To</div><div class="mt-1 font-semibold text-slate-900">{{ task.assigned_to.full_name or task.assigned_to.email }}</div></div>{% endif %}<div><div class="text-xs uppercase tracking-wide text-slate-500">Priority</div><div class="mt-1 font-semibold text-slate-900">{{ task.priority.replace('_',' ').title() }}</div></div>
          <div><div class="text-xs uppercase tracking-wide text-slate-500">Financial Year</div><div class="mt-1 font-semibold text-slate-900">{{ task.financial_year }}</div></div>
          <div><div class="text-xs uppercase tracking-wide text-slate-500">Engagement Status</div><div class="mt-1 font-semibold text-slate-900">{{ subscription.status.replace('_',' ').title() }}</div></div>
        </div>
        {% if task.description %}<div class="mt-5 rounded-xl bg-slate-50 p-4 text-sm text-slate-700 whitespace-pre-line">{{ task.description }}</div>{% endif %}
      </div>

      {% if task.execution_mode == 'consultant' and task.assigned_consultant_id == consultant.id %}
      <div class="rounded-2xl border border-indigo-200 bg-indigo-50 p-5 shadow-soft">
        <div class="flex flex-wrap items-start justify-between gap-3">
          <div><h3 class="font-semibold text-slate-900">Consultant Assignment Execution</h3><p class="mt-1 text-sm text-slate-600">Status: <span class="font-semibold">{{ task.consultant_assignment_status.replace('_',' ').title() }}</span>{% if task.consultant_due_date %} • Due {{ task.consultant_due_date.strftime('%d-%m-%Y') }}{% endif %}</p></div>
        </div>
        {% if task.rework_reason %}<div class="mt-3 rounded-xl border border-amber-200 bg-amber-50 p-3 text-sm text-amber-800"><strong>Rework:</strong> {{ task.rework_reason }}</div>{% endif %}
        <form method="post" action="/consultant/assignments/{{ task.id }}/execution" class="mt-4 space-y-3">
          <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
          <textarea name="note" rows="3" placeholder="Reason for decline or submission note" class="w-full rounded-xl border border-slate-300 bg-white px-3 py-2 text-sm"></textarea>
          <div class="flex flex-wrap gap-2">
            {% if task.consultant_assignment_status == 'offered' %}<button name="action" value="accept" class="rounded-xl bg-emerald-600 px-4 py-2 text-sm font-semibold text-white">Accept Assignment</button><button name="action" value="decline" class="rounded-xl border border-red-300 bg-white px-4 py-2 text-sm font-semibold text-red-700">Decline</button>{% endif %}
            {% if task.consultant_assignment_status in ['accepted','rework_required'] %}<button name="action" value="start" class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-semibold text-white">Start Work</button>{% endif %}
            {% if task.consultant_assignment_status in ['accepted','in_progress','rework_required'] %}<button name="action" value="submit" class="rounded-xl bg-indigo-600 px-4 py-2 text-sm font-semibold text-white">Submit for Firm Review</button>{% endif %}
          </div>
        </form>
      </div>
      {% endif %}

      <div class="rounded-2xl border border-slate-200 bg-white shadow-soft">
        <div class="border-b border-slate-200 px-5 py-4"><h3 class="font-semibold text-slate-900">Consultant Communication Timeline</h3></div>
        <div class="divide-y divide-slate-100">
          {% for note in timeline %}
          <div class="p-5">
            <div class="flex flex-wrap items-center justify-between gap-2">
              <div class="text-sm font-semibold text-slate-900">{{ note.comment_type.replace('_',' ').title() }}</div>
              <div class="text-xs text-slate-500">{{ note.created_at_utc.strftime('%d-%m-%Y %H:%M') if note.created_at_utc else '' }}</div>
            </div>
            <div class="mt-2 whitespace-pre-line rounded-xl bg-slate-50 p-3 text-sm text-slate-700">{{ note.message }}</div>
          </div>
          {% else %}
          <div class="p-6 text-sm text-slate-500">No consultant-visible timeline yet.</div>
          {% endfor %}
        </div>
      </div>

      {% if link.can_reply_to_clarifications %}<form method="post" action="/consultant/assignments/{{ task.id }}/reply" class="rounded-2xl border border-slate-200 bg-white p-5 shadow-soft">
        <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
        <h3 class="font-semibold text-slate-900">Send Reply / Submit Update</h3>
        {% if errors %}<div class="mt-3 rounded-xl border border-red-200 bg-red-50 p-3 text-sm text-red-700">{{ errors|join(' ') }}</div>{% endif %}
        <textarea name="message" rows="5" required placeholder="Type clarification reply, submission note, or work update" class="mt-4 w-full rounded-xl border border-slate-300 px-3 py-2 text-sm"></textarea>
        <div class="mt-4 flex justify-end"><button class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-semibold text-white hover:bg-brand-700">Send Update</button></div>
      </form>{% endif %}
    </div>

    <aside class="space-y-6">
      <div class="rounded-2xl border border-slate-200 bg-white shadow-soft">
        <div class="border-b border-slate-200 px-4 py-3"><h3 class="font-semibold text-slate-900">Engagement Documents</h3></div>
        <div class="divide-y divide-slate-100">
          {% for doc in engagement_documents %}
          <div class="p-4">
            <div class="font-semibold text-slate-900">{{ doc.title }}</div>
            <div class="text-xs text-slate-500">{{ doc.document_type }} • v{{ doc.current_version_no }}</div>
            {% if doc.versions %}<div class="mt-1 text-xs text-slate-500">Latest: {{ doc.versions[0].original_filename }}</div>{% endif %}
          </div>
          {% else %}<div class="p-4 text-sm text-slate-500">No shared engagement documents found.</div>{% endfor %}
        </div>
      </div>
      {% if link.can_view_permanent_documents %}<div class="rounded-2xl border border-slate-200 bg-white shadow-soft">
        <div class="border-b border-slate-200 px-4 py-3"><h3 class="font-semibold text-slate-900">Permanent Documents</h3></div>
        <div class="divide-y divide-slate-100">
          {% for doc in permanent_documents %}
          <div class="p-4">
            <div class="font-semibold text-slate-900">{{ doc.title }}</div>
            <div class="text-xs text-slate-500">{{ doc.category }} • v{{ doc.current_version_no }}</div>
          </div>
          {% else %}<div class="p-4 text-sm text-slate-500">No shared permanent documents found.</div>{% endfor %}
        </div>
      </div>{% endif %}
    </aside>
  </div>
</div>
{% endblock %}
