import { BaseUploadDto } from "@/record-management/dto/fileDto";
import {
  updateIncomingRecordStatus,
  UploadContext,
  uploadFile,
  uploadMeta,
} from "@/shared/services/fileService";
import recordAxiosInstance from "@/shared/services/recordAxiosInstance";
import { withHeaders } from "./withHeaders";

const nonSmartOfficeFileContext: UploadContext<BaseUploadDto> = {
  endpoint: "/records",
  module: "external-portal",
  uploadContext: "external-portal",
  allowedMimeTypes: [
    "image/png",
    "image/jpeg",
    "image/gif",
    "application/pdf",
    "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    "application/msword",
    "application/vnd.ms-excel",
    "application/acad",
    "application/x-autocad",
    "application/x-dwg",
    "image/vnd.dwg",
    "application/dxf",
    "application/x-dxf",
    "image/vnd.dxf",
    "application/x-autocad-dwt",
    "application/x-autocad-dws",
    "application/x-esri-map",
    "application/x-esri-arcgis-pro-project",
    "application/x-rar-compressed",
    "application/vnd.rar",
    "application/zip",
    "application/octet-stream",
    "application/x-shapefile",
    "application/x-dbf",
    "application/x-cpg",
    "application/x-shx",
    "application/x-qmd",
    "application/vnd.shp",
    "application/vnd.dbf",
    "application/vnd.cpg",
    "application/vnd.shx",
    "application/vnd.qmd",
  ],
  allowedExtensions: [
    ".png",
    ".jpg",
    ".jpeg",
    ".gif",
    ".pdf",
    ".doc",
    ".docx",
    ".xls",
    ".xlsx",
    ".dwg",
    ".dxf",
    ".dwt",
    ".bak",
    ".sv$",
    ".dws",
    ".mxd",
    ".aprx",
    ".rar",
    ".zip",
    ".cpg",
    ".dbf",
    ".qmd",
    ".shp",
    ".shx",
  ],
  maxSizeMB: 25,
  buildBody: (file, fileName, recordContentId) => {
    // Infer MIME type from file extension if file.type is empty
    let contentType = file.type || "application/octet-stream";

    if (!file.type && file.name) {
      const ext = file.name.split(".").pop()?.toLowerCase();
      const extensionMimeMap: Record<string, string> = {
        shp: "application/vnd.shp",
        dbf: "application/vnd.dbf",
        cpg: "application/vnd.cpg",
        shx: "application/vnd.shx",
        qmd: "application/vnd.qmd",
        dwg: "application/x-dwg",
        dxf: "application/dxf",
        txt: "text/plain",
        pdf: "application/pdf",
        zip: "application/zip",
        rar: "application/x-rar-compressed",
        jpg: "image/jpeg",
        jpeg: "image/jpeg",
        png: "image/png",
        gif: "image/gif",
        doc: "application/msword",
        docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
        xls: "application/vnd.ms-excel",
        xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        ppt: "application/vnd.ms-powerpoint",
        pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
      };
      contentType = ext ? extensionMimeMap[ext] : "application/octet-stream";
    }

    return {
      fileInfo: {
        fileName,
        originalname: file.name,
        contentType: contentType || "application/octet-stream",
        fileType: contentType || "application/octet-stream",
        size: file.size,
      },
      name: { am: file.name, en: file.name },
      recordContentId,
    };
  },
};

export const nonSmartOfficeFileService = {
  uploadMeta: (file: File, fileName: string, employeeId: string) =>
    uploadMeta(
      nonSmartOfficeFileContext,
      file,
      fileName,
      employeeId,
      recordAxiosInstance,
    ),
  updateStatus: (fileId: string, uploaded: boolean) =>
    updateIncomingRecordStatus(
      "/records",
      fileId,
      uploaded,
      recordAxiosInstance,
    ),
  uploadFile,
  getFileUploadUrl: (file: File[]) => {
    try {
      const result = recordAxiosInstance.post(
        "/records/get-upload-url",
        file,
        withHeaders(),
      );
      return result;
    } catch (error) {
    throw error;
  }
  },
};
