import { privateMutator } from "@/api/privateClient";
import type { Message } from "@/api/generated/private/models/message";

export interface SendMessageParams {
  chatId: number;
  content?: string;
  replyToId?: number;
  files?: File[];
}

/**
 * Sends a chat message via HTTP multipart POST.
 * Use this whenever files are attached; text-only messages can go through WS.
 */
export async function sendChatMessage({
  chatId,
  content,
  replyToId,
  files,
}: SendMessageParams): Promise<Message> {
  const form = new FormData();
  form.append("chat", String(chatId));
  if (content) form.append("content", content);
  if (replyToId != null) form.append("reply_to", String(replyToId));
  for (const file of files ?? []) {
    form.append("files", file);
  }
  // privateMutator deletes Content-Type for FormData so Axios sets the multipart boundary
  return privateMutator<Message>({
    url: "/api/social/messages/send/",
    method: "POST",
    data: form,
  });
}
