import type { FileInMessage } from "../messages/utils";
import type { UploadedFileInfo } from "../uploads/api";

export type MentionReference = {
  filename: string;
  path?: string;
  size?: number;
  ref_kind: "mention";
  ref_source: "artifact" | "upload";
  stale?: boolean;
};

export function buildFilesForSubmit(
  uploadedFileInfo: UploadedFileInfo[],
  references: MentionReference[] = [],
) {
  const uploadedFiles: FileInMessage[] = uploadedFileInfo.map((info) => ({
    filename: info.filename,
    size: info.size,
    path: info.virtual_path,
    status: "uploaded" as const,
  }));

  const referenceFiles: FileInMessage[] = [];
  let staleCount = 0;

  for (const reference of references) {
    if (reference.stale) {
      staleCount += 1;
      continue;
    }
    referenceFiles.push({
      filename: reference.filename,
      size: reference.size ?? 0,
      path: reference.path,
      status: "uploaded" as const,
      ref_kind: "mention",
      ref_source: reference.ref_source,
    });
  }

  return {
    files: [...uploadedFiles, ...referenceFiles],
    staleCount,
  };
}
