// insertUploadedFile.ts
import { uploadedFiles, docTypeEnum } from "@/app/db/surveyDB/schema/surveyDB";
import { surveyDB } from "../connection";
import type { ReportType, ReportTypeSchema} from "../schema/documents";
import { reportTypeToDbLabel } from "../schema/documents";
type DbDocType = (typeof docTypeEnum.enumValues)[number];

export interface UploadedFileInput {
  s3JsonUri?: string; // optional
  s3FileUri: string;
  docType: ReportType;
  s3FileUploadTimestamp: Date;
  s3JsonUploadTimestamp?: Date; // optional
  uprn: string;
}

export async function insertUploadedFile(data: UploadedFileInput) {
  const [newFile] = await surveyDB
    .insert(uploadedFiles)
    .values({
      s3JsonUri: data.s3JsonUri ?? null, // Pass null if missing
      s3FileUri: data.s3FileUri,
      docType: reportTypeToDbLabel[data.docType] as DbDocType, // map UI value -> DB enum NAME
      s3FileUploadTimestamp: data.s3FileUploadTimestamp,
      s3JsonUploadTimestamp: data.s3JsonUploadTimestamp ?? null, // Pass null if missing
      uprn: data.uprn,
    })
    .returning();

  return newFile;
}