import { apiFetch } from '../../_lib/api';

interface PendingMessage {
  id: string;
  content: string;
  senderJid: string;
  senderName: string | null;
  tags: string[];
  createdAt: string;
  sourceGroupId: string;
  sourceGroupName: string;
  sourceGroupPlatformId: string;
}

type FetchResult<T> = { ok: true; data: T } | { ok: false; status: number; error: string };

async function fetchPending(): Promise<FetchResult<PendingMessage[]>> {
  try {
    const res = await apiFetch('/admin/messages/pending');
    if (!res.ok) {
      const body = await res.text().catch(() => '');
      return { ok: false, status: res.status, error: body.slice(0, 200) || res.statusText };
    }
    return { ok: true, data: (await res.json()) as PendingMessage[] };
  } catch (err) {
    return { ok: false, status: 0, error: `API unreachable: ${(err as Error).message}` };
  }
}

export default async function PendingMessagesPage() {
  const result = await fetchPending();
  const messages = result.ok ? result.data : [];
  const error = !result.ok ? (result.status === 0 ? 'API unreachable' : `API ${result.status}: ${result.error}`) : null;

  return (
    <div className="max-w-3xl">
      <h1 className="text-xl font-semibold mb-2">Pending messages</h1>
      <p className="text-sm text-gray-500 mb-6">
        Flagged messages waiting for an admin to approve. Approving forwards them to every active
        route from the source group and indexes them in search.
      </p>

      {error && (
        <div className="mb-4 rounded border border-red-200 bg-red-50 p-3 text-sm text-red-800">
          Failed to load: {error}
        </div>
      )}

      {!error && messages.length === 0 && (
        <div className="rounded-xl border border-gray-200 bg-white p-6 text-sm text-gray-500">
          No pending messages right now. New flagged messages will appear here as they arrive.
        </div>
      )}

      <ul className="space-y-3">
        {messages.map((m) => (
          <PendingMessageRow key={m.id} message={m} />
        ))}
      </ul>
    </div>
  );
}

function PendingMessageRow({ message }: { message: PendingMessage }) {
  return (
    <li className="rounded-xl border border-gray-200 bg-white p-4 flex flex-col gap-2">
      <div className="flex items-baseline justify-between gap-2">
        <div className="text-sm font-medium text-gray-900">{message.sourceGroupName}</div>
        <div className="text-xs text-gray-500">
          {new Date(message.createdAt).toLocaleString()}
        </div>
      </div>
      <div className="text-xs text-gray-500">
        From <span className="font-mono">{message.senderName ?? message.senderJid}</span>
        {message.tags.length > 0 && (
          <span className="ml-2 inline-flex flex-wrap gap-1">
            {message.tags.map((t) => (
              <span
                key={t}
                className="rounded bg-blue-50 px-1.5 py-0.5 text-[10px] font-medium text-blue-700"
              >
                {t}
              </span>
            ))}
          </span>
        )}
      </div>
      <div className="text-sm text-gray-800 whitespace-pre-wrap break-words">
        {message.content}
      </div>
      <form
        action={`/api/messages/${message.id}/approve`}
        method="post"
        className="flex justify-end pt-1"
      >
        <button
          type="submit"
          className="rounded bg-blue-600 px-3 py-1.5 text-sm font-medium text-white hover:bg-blue-700"
        >
          Approve &amp; forward
        </button>
      </form>
    </li>
  );
}
