import { InboxPlaceholder } from "@/components/placeholders";
import { PlaceholderActions, ShellPage } from "@/components/page-templates";
import {
  addConversationNote,
  assignConversation,
  getInboxWorkspace,
  replyToConversation,
  setConversationTags,
  updateConversationStatus
} from "@/lib/inbox-ops";

const allowedFilters = ["all", "open", "pending", "resolved", "unassigned"] as const;

export default async function InboxPage({
  searchParams
}: {
  searchParams: Promise<{ conversationId?: string; filter?: string }>;
}) {
  const params = await searchParams;
  const filter =
    params?.filter && allowedFilters.includes(params.filter as (typeof allowedFilters)[number])
      ? (params.filter as (typeof allowedFilters)[number])
      : "all";

  const data = await getInboxWorkspace({
    scope: "admin",
    conversationId: params?.conversationId,
    filter
  });

  return (
    <ShellPage
      shell="admin"
      title="Shared Inbox"
      description="Split layout untuk conversation list, timeline, assignment, notes, tags, dan reply composer."
      actions={<PlaceholderActions primaryHref="/team" primaryLabel="Manage team" secondaryHref="/contacts" secondaryLabel="Open contacts" />}
    >
      <InboxPlaceholder
        conversations={data.conversations}
        selectedConversation={data.selectedConversation}
        defaultPath={data.defaultPath}
        agents={data.agents}
        role={data.role}
        filter={data.filter}
        canSelfAssign={data.canSelfAssign}
        assignConversation={assignConversation}
        updateConversationStatus={updateConversationStatus}
        replyToConversation={replyToConversation}
        addConversationNote={addConversationNote}
        setConversationTags={setConversationTags}
      />
    </ShellPage>
  );
}
