import { requirePermission } from "@/shared/lib/auth-guard"
import { Permissions } from "@/shared/types/permissions"
import { getRecipients } from "@/modules/messaging/data-access"
import { MessageCompose } from "@/modules/messaging/components/message-compose"

export const dynamic = "force-dynamic"

export default async function ComposeMessagePage({
  searchParams,
}: {
  searchParams: Promise<{ parentId?: string; receiverId?: string; subject?: string }>
}) {
  const ctx = await requirePermission(Permissions.MESSAGE_SEND)
  const sp = await searchParams

  const recipients = await getRecipients(ctx.userId, ctx.dataScope)

  return (
    <div className="flex h-full flex-col p-8">
      <div className="mx-auto w-full max-w-3xl space-y-6">
        <div>
          <h2 className="text-2xl font-bold tracking-tight">Compose Message</h2>
          <p className="text-muted-foreground">Send a message to another user.</p>
        </div>
        <MessageCompose
          recipients={recipients}
          parentMessageId={sp.parentId}
          defaultReceiverId={sp.receiverId}
          defaultSubject={sp.subject}
        />
      </div>
    </div>
  )
}
