import { PageHeader } from "@/components/admin/PageHeader";
import { ConversationsView } from "@/components/admin/conversazioni/ConversationsView";
import {
  getConversations,
  getConversationThread,
} from "@/lib/conversations-queries";

export const revalidate = 0;

export default async function ConversazioniPage({
  searchParams,
}: {
  searchParams: Promise<{ c?: string }>;
}) {
  const { c } = await searchParams;

  const conversations = await getConversations();
  // Default selection: the requested client, else the most recent conversation.
  const activeClientId =
    (c && conversations.some((conv) => conv.clientId === c) ? c : null) ??
    conversations[0]?.clientId ??
    null;

  const activeThread = activeClientId
    ? await getConversationThread(activeClientId)
    : null;

  return (
    <div className="space-y-6">
      <PageHeader
        title="Conversazioni"
        subtitle="Centro messaggi clienti — tutte le richieste in un unico posto"
      />
      <ConversationsView
        conversations={conversations}
        activeThread={activeThread}
        activeClientId={activeClientId}
      />
    </div>
  );
}
