import { DashboardShell } from '../../../components/dashboard-shell';
import { ConversationsInbox } from '../../../components/conversations-inbox';
import { requireAuthToken } from '../../../lib/auth';
import { fetchConversationDetail, fetchConversations } from '../../../lib/api';

export default async function ConversationsPage({
  searchParams,
}: {
  searchParams?: Promise<{ q?: string }>;
}) {
  const token = await requireAuthToken();
  const resolvedSearchParams = (await searchParams) || {};
  const search = resolvedSearchParams.q?.trim() || '';
  const conversations = await fetchConversations(token, { filter: 'all', search });
  const initialConversationDetail = conversations[0]
    ? await fetchConversationDetail(token, conversations[0].id)
    : null;

  return (
    <DashboardShell
      currentPath="/dashboard/conversations"
      title="Conversations"
      searchPlaceholder="Search conversations..."
      searchValue={search}
      searchActionPath="/dashboard/conversations"
      searchQueryName="q"
    >
      <ConversationsInbox
        initialConversations={conversations}
        initialConversationDetail={initialConversationDetail}
        initialSearch={search}
      />
    </DashboardShell>
  );
}
