import Link from 'next/link';
import { apiFetch } from '../../_lib/api';

interface MessageDetail {
  id: string;
  tenantId: string;
  platform: string;
  platformMsgId: string;
  sourceGroupId: string;
  sourceGroup: {
    id: string;
    name: string;
    platformId: string;
    claimStatus: string;
    isActive: boolean;
    createdAt: string;
  } | null;
  senderJid: string;
  senderName: string | null;
  senderTowerUser: {
    id: string;
    jid: string;
    phoneHash: string;
    displayName: string | null;
    createdAt: string;
  } | null;
  content: string;
  mediaUrl: string | null;
  tags: string[];
  status: string;
  createdAt: string;
  updatedAt: string;
  approval: {
    id: string;
    adminId: string;
    decision: string;
    notes: string | null;
    decidedAt: string;
  } | null;
}

function Field({ label, children }: { label: string; children: React.ReactNode }) {
  return (
    <div className="grid grid-cols-[160px_1fr] gap-2 text-sm py-2 border-b border-gray-100">
      <span className="text-gray-500 font-medium">{label}</span>
      <span className="text-gray-900 break-words">{children ?? <span className="text-gray-300">—</span>}</span>
    </div>
  );
}

export default async function MessageDetailPage({
  params,
}: {
  params: Promise<{ id: string }>;
}) {
  const { id } = await params;

  let msg: MessageDetail | null = null;
  let error: string | null = null;
  try {
    const res = await apiFetch(`/admin/messages/${id}`);
    if (res.ok) {
      msg = await res.json();
    } else {
      error = `API returned ${res.status}`;
    }
  } catch {
    error = 'Failed to load message';
  }

  if (error || !msg) {
    return (
      <div className="max-w-2xl">
        <Link href="/search" className="text-sm text-blue-600 hover:underline mb-4 inline-block">&larr; Back to search</Link>
        <p className="text-red-600 text-sm">{error ?? 'Message not found'}</p>
      </div>
    );
  }

  return (
    <div className="max-w-2xl">
      <Link href="/search" className="text-sm text-blue-600 hover:underline mb-4 inline-block">&larr; Back to search</Link>
      <h1 className="text-xl font-semibold mb-6">Message Detail</h1>

      <div className="border border-gray-200 rounded-lg p-4 mb-6">
        <p className="text-sm whitespace-pre-wrap bg-gray-50 rounded p-3 mb-2">{msg.content}</p>
        <div className="flex flex-wrap gap-1">
          {msg.tags.map((tag) => (
            <span key={tag} className="rounded-full bg-blue-50 px-2 py-0.5 text-xs text-blue-600">{tag}</span>
          ))}
        </div>
      </div>

      <div className="border border-gray-200 rounded-lg p-4">
        <h2 className="text-sm font-semibold text-gray-700 mb-3">Metadata</h2>

        <Field label="Status">
          <span className={`font-medium ${msg.status === 'APPROVED' ? 'text-green-600' : msg.status === 'REJECTED' ? 'text-red-600' : 'text-amber-600'}`}>
            {msg.status}
          </span>
        </Field>
        <Field label="Message ID">{msg.id}</Field>
        <Field label="Platform">{msg.platform}</Field>
        <Field label="Platform Msg ID">{msg.platformMsgId}</Field>
        <Field label="Created">{new Date(msg.createdAt).toLocaleString()}</Field>
        <Field label="Updated">{new Date(msg.updatedAt).toLocaleString()}</Field>

        <div className="mt-4 pt-2 border-t border-gray-200">
          <h3 className="text-xs font-semibold text-gray-500 uppercase mb-2">Sender</h3>
          <Field label="JID">{msg.senderJid}</Field>
          <Field label="Name">{msg.senderName}</Field>
          {msg.senderTowerUser && (
            <>
              <Field label="Display Name">{msg.senderTowerUser.displayName}</Field>
              <Field label="Phone Hash">{msg.senderTowerUser.phoneHash}</Field>
              <Field label="Tower User ID">{msg.senderTowerUser.id}</Field>
            </>
          )}
        </div>

        <div className="mt-4 pt-2 border-t border-gray-200">
          <h3 className="text-xs font-semibold text-gray-500 uppercase mb-2">Source Group</h3>
          {msg.sourceGroup ? (
            <>
              <Field label="Name">{msg.sourceGroup.name}</Field>
              <Field label="Platform ID">{msg.sourceGroup.platformId}</Field>
              <Field label="Claim Status">{msg.sourceGroup.claimStatus}</Field>
              <Field label="Active">{msg.sourceGroup.isActive ? 'Yes' : 'No'}</Field>
            </>
          ) : (
            <Field label="Group">(deleted)</Field>
          )}
        </div>

        {msg.approval && (
          <div className="mt-4 pt-2 border-t border-gray-200">
            <h3 className="text-xs font-semibold text-gray-500 uppercase mb-2">Approval</h3>
            <Field label="Decision">
              <span className={`font-medium ${msg.approval.decision === 'APPROVED' ? 'text-green-600' : 'text-red-600'}`}>
                {msg.approval.decision}
              </span>
            </Field>
            <Field label="Admin ID">{msg.approval.adminId}</Field>
            <Field label="Decided At">{new Date(msg.approval.decidedAt).toLocaleString()}</Field>
            <Field label="Notes">{msg.approval.notes}</Field>
          </div>
        )}

        {msg.mediaUrl && (
          <div className="mt-4 pt-2 border-t border-gray-200">
            <h3 className="text-xs font-semibold text-gray-500 uppercase mb-2">Media</h3>
            <Field label="Media URL">{msg.mediaUrl}</Field>
          </div>
        )}
      </div>
    </div>
  );
}
