// Reusable detail body — used by full-page ContractDetailPage AND embedded
// in MyContractsPage 3-panel layout (Panel 2). Renders header (title + phase
// + actions) + Info + Comments + Attachments + transition Dialog. Workflow +
// approval history live separately in WorkflowHistoryPanel (Panel 3).
import { useState, type FormEvent } from 'react'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { ArrowLeft, CheckCircle2, MessageSquare, XCircle, ListChecks } from 'lucide-react'
import { toast } from 'sonner'
import { PhaseBadge } from '@/components/PhaseBadge'
import { SlaTimer } from '@/components/SlaTimer'
import { ContractAttachmentsSection } from '@/components/ContractAttachmentsSection'
import { ContractDetailsTab } from '@/components/contracts/ContractDetailsTab'
import { Button } from '@/components/ui/Button'
import { Select } from '@/components/ui/Select'
import { Textarea } from '@/components/ui/Textarea'
import { Dialog } from '@/components/ui/Dialog'
import { api } from '@/lib/api'
import { getErrorMessage } from '@/lib/apiError'
import {
  ApprovalDecision,
  ContractPhase,
  ContractPhaseLabel,
  type ContractDetail,
} from '@/types/contracts'
import { ContractTypeLabel } from '@/types/forms'

const fmt = (s: string) => new Date(s).toLocaleString('vi-VN')
const fmtMoney = (v: number) => v.toLocaleString('vi-VN') + ' VND'

export function ContractDetailContent({
  contract: c,
  onBack,
}: {
  contract: ContractDetail
  /** Optional back handler — shown as arrow button next to title. Pass `navigate(-1)` for fullpage; omit for embedded panel. */
  onBack?: () => void
}) {
  const qc = useQueryClient()
  const [actionOpen, setActionOpen] = useState(false)
  const [targetPhase, setTargetPhase] = useState<number>(0)
  const [decision, setDecision] = useState<number>(ApprovalDecision.Approve)
  const [comment, setComment] = useState('')
  const [commentInput, setCommentInput] = useState('')

  const transition = useMutation({
    mutationFn: async () => {
      await api.post(`/contracts/${c.id}/transitions`, { targetPhase, decision, comment: comment || null })
    },
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['contract', c.id] })
      qc.invalidateQueries({ queryKey: ['my-contracts'] })
      qc.invalidateQueries({ queryKey: ['inbox'] })
      toast.success('Đã chuyển phase')
      setActionOpen(false)
      setComment('')
    },
    onError: err => toast.error(getErrorMessage(err)),
  })

  const addComment = useMutation({
    mutationFn: async (content: string) => {
      await api.post(`/contracts/${c.id}/comments`, { content })
    },
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['contract', c.id] })
      setCommentInput('')
      toast.success('Đã gửi')
    },
    onError: err => toast.error(getErrorMessage(err)),
  })

  const availableTargets = c.workflow?.nextPhases ?? []

  function openAction(decisionType: number) {
    const targets = c.workflow?.nextPhases ?? []
    const defaultTarget = decisionType === ApprovalDecision.Reject
      ? targets.find(t => t === ContractPhase.DangSoanThao) ?? targets[0]
      : targets[0]
    setTargetPhase(defaultTarget)
    setDecision(decisionType)
    setActionOpen(true)
  }

  return (
    <div className="space-y-4">
      {/* Header — sticky inside scroll container so actions luôn visible */}
      <div className="sticky top-0 z-10 -mx-5 -mt-5 border-b border-slate-200 bg-white px-5 pt-5 pb-3 md:-mx-6 md:-mt-6 md:px-6 md:pt-6">
        <div className="flex flex-wrap items-start justify-between gap-3">
          <div className="min-w-0 flex-1">
            <div className="flex items-center gap-2">
              {onBack && (
                <button onClick={onBack} className="text-slate-400 hover:text-slate-700" aria-label="Quay lại">
                  <ArrowLeft className="h-5 w-5" />
                </button>
              )}
              <h1 className="truncate text-[18px] font-semibold tracking-tight text-slate-900">
                {c.tenHopDong ?? 'HĐ chưa đặt tên'}
              </h1>
            </div>
            <div className="mt-1 flex items-center gap-3 text-xs text-slate-500">
              <span className="font-mono">{c.maHopDong ?? '(chưa có mã)'}</span>
              <PhaseBadge phase={c.phase} />
            </div>
          </div>
          {availableTargets.length > 0 && (
            <div className="flex shrink-0 gap-2">
              <Button variant="outline" onClick={() => openAction(ApprovalDecision.Reject)}>
                <XCircle className="h-4 w-4" />
                Yêu cầu sửa
              </Button>
              <Button onClick={() => openAction(ApprovalDecision.Approve)}>
                <CheckCircle2 className="h-4 w-4" />
                Duyệt → tiếp
              </Button>
            </div>
          )}
        </div>
      </div>

      {/* Tổng quan content — luôn hiển thị, không tabs */}
      <section className="rounded-lg border border-slate-200 bg-white p-5">
        <h2 className="mb-3 text-sm font-semibold text-slate-700">Thông tin HĐ</h2>
        <dl className="grid grid-cols-2 gap-3 text-sm">
          <div><dt className="text-slate-500">Loại</dt><dd>{ContractTypeLabel[c.type] ?? '—'}</dd></div>
          <div><dt className="text-slate-500">Giá trị</dt><dd>{fmtMoney(c.giaTri)}</dd></div>
          <div><dt className="text-slate-500">NCC</dt><dd>{c.supplierName}</dd></div>
          <div><dt className="text-slate-500">Dự án</dt><dd>{c.projectName}</dd></div>
          <div><dt className="text-slate-500">Người soạn</dt><dd>{c.drafterName ?? '—'}</dd></div>
          <div className="col-span-2">
            <dt className="text-slate-500 mb-1">SLA</dt>
            <dd><SlaTimer deadline={c.slaDeadline} createdAt={c.createdAt} variant="full" /></dd>
          </div>
        </dl>
        {c.noiDung && (
          <div className="mt-3">
            <dt className="text-sm text-slate-500">Nội dung</dt>
            <dd className="mt-1 whitespace-pre-wrap text-sm text-slate-700">{c.noiDung}</dd>
          </div>
        )}
      </section>

      <section className="rounded-lg border border-slate-200 bg-white p-5">
        <h2 className="mb-3 flex items-center gap-2 text-sm font-semibold text-slate-700">
          <MessageSquare className="h-4 w-4" />
          Góp ý ({c.comments.length})
        </h2>
        <div className="space-y-3">
          {c.comments.length === 0 && <div className="text-sm text-slate-400">Chưa có góp ý.</div>}
          {c.comments.map(cm => (
            <div key={cm.id} className="rounded-md border border-slate-100 p-3">
              <div className="flex items-center justify-between text-xs text-slate-500">
                <span className="font-medium text-slate-700">{cm.userName}</span>
                <span>{fmt(cm.createdAt)} · {ContractPhaseLabel[cm.phase]}</span>
              </div>
              <div className="mt-1 whitespace-pre-wrap text-sm text-slate-700">{cm.content}</div>
            </div>
          ))}
        </div>
        <form
          className="mt-4 flex gap-2"
          onSubmit={(e: FormEvent) => {
            e.preventDefault()
            if (!commentInput.trim()) return
            addComment.mutate(commentInput.trim())
          }}
        >
          <Textarea rows={2} placeholder="Thêm góp ý…" value={commentInput} onChange={e => setCommentInput(e.target.value)} />
          <Button type="submit" disabled={addComment.isPending || !commentInput.trim()}>
            Gửi
          </Button>
        </form>
      </section>

      <ContractAttachmentsSection contractId={c.id} attachments={c.attachments} />

      {/* Chi tiết HĐ full-width — Lịch sử điều chỉnh đã move sang Panel 3 */}
      <section className="rounded-lg border border-slate-200 bg-white p-5">
        <h2 className="mb-3 flex items-center gap-2 text-sm font-semibold text-slate-700">
          <ListChecks className="h-4 w-4" />
          Chi tiết ({ContractTypeLabel[c.type] ?? '—'})
        </h2>
        <ContractDetailsTab contract={c} />
      </section>

      {/* [Plan B S29 2026-05-22 Chunk E3] Section 5 — Ý kiến cấp duyệt V2 dynamic.
          Mirror PE LevelOpinionsSectionV2 pattern. Chỉ render khi V2 pin
          (approvalWorkflowId set). V1 legacy contract KHÔNG hiển thị. */}
      {c.approvalWorkflowId && (
        <section className="rounded-lg border border-emerald-200 bg-emerald-50/40 p-5">
          <h2 className="mb-3 flex items-center gap-2 text-sm font-semibold text-emerald-800">
            <ListChecks className="h-4 w-4" />
            Ý kiến cấp duyệt (Quy trình V2)
          </h2>
          {(c.levelOpinions?.length ?? 0) === 0 ? (
            <p className="text-sm text-slate-500">Chưa có ý kiến — workflow vừa bắt đầu hoặc chưa có ai duyệt.</p>
          ) : (
            <ul className="space-y-3">
              {c.levelOpinions!.map(o => {
                const adminProxy = o.signedByUserId !== o.approverUserId
                return (
                  <li key={o.id} className="rounded-md border border-emerald-200 bg-white p-3">
                    <div className="mb-1 flex items-center justify-between gap-2 text-xs">
                      <span className="font-medium text-emerald-900">
                        Bước {o.stepOrder} {o.stepName ? `(${o.stepName})` : ''} — Cấp {o.levelOrder}
                        {o.levelName ? ` (${o.levelName})` : ''}
                      </span>
                      <span className="text-slate-500">{new Date(o.signedAt).toLocaleString('vi-VN')}</span>
                    </div>
                    <p className="mb-1 text-sm text-slate-800">{o.comment}</p>
                    <div className="flex flex-wrap items-center gap-2 text-xs text-slate-600">
                      <span>NV duyệt: <strong>{o.approverFullName ?? o.approverUserId.slice(0, 8)}</strong></span>
                      {adminProxy && (
                        <span className="rounded bg-amber-100 px-2 py-0.5 text-amber-800">
                          ⚡ Admin duyệt thay ({o.signedByFullName ?? o.signedByUserId.slice(0, 8)})
                        </span>
                      )}
                    </div>
                  </li>
                )
              })}
            </ul>
          )}
        </section>
      )}

      <Dialog
        open={actionOpen}
        onClose={() => setActionOpen(false)}
        title={decision === ApprovalDecision.Reject ? 'Yêu cầu sửa' : 'Chuyển phase tiếp'}
        footer={
          <>
            <Button variant="outline" onClick={() => setActionOpen(false)}>Hủy</Button>
            <Button onClick={() => transition.mutate()} disabled={transition.isPending || !targetPhase}>
              {transition.isPending ? 'Đang xử lý…' : 'Xác nhận'}
            </Button>
          </>
        }
      >
        <div className="space-y-4">
          <div className="space-y-1.5">
            <label className="text-sm font-medium text-slate-700">Chuyển đến phase</label>
            <Select value={targetPhase} onChange={e => setTargetPhase(Number(e.target.value))}>
              {availableTargets.map(p => (
                <option key={p} value={p}>{ContractPhaseLabel[p]}</option>
              ))}
            </Select>
          </div>
          <div className="space-y-1.5">
            <label className="text-sm font-medium text-slate-700">Ghi chú (optional)</label>
            <Textarea rows={3} value={comment} onChange={e => setComment(e.target.value)} />
          </div>
        </div>
      </Dialog>
    </div>
  )
}

