import { BaseUploadDto } from '@/record-management/dto/fileDto'
import {
	updateStatus,
	UploadContext,
	uploadFile,
	uploadMeta,
} from '@/shared/services/fileService'
import recordAxiosInstance from '@/shared/services/recordAxiosInstance'

const mimeToExtension: Record<string, string> = {
  "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
    "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
  "application/msword": "application/msword",
  "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  "application/vnd.ms-excel": "application/vnd.ms-excel",
  "application/pdf": "application/pdf",
  "image/png": "image/png",
  "image/jpeg": "image/jpeg",
  "image/gif": "image/gif",
  "text/plain": "text/plain",
  "application/vnd.ms-powerpoint": "application/vnd.ms-powerpoint",
  "application/vnd.openxmlformats-officedocument.presentationml.presentation":
    "application/vnd.openxmlformats-officedocument.presentationml.presentation",
  "application/acad": "application/acad",
  "application/x-autocad": "application/x-autocad",
  "application/x-dwg": "application/x-dwg",
  "image/vnd.dwg": "image/vnd.dwg",
  "application/dxf": "application/dxf",
  "application/x-dxf": "application/x-dxf",
  "image/vnd.dxf": "image/vnd.dxf",
  "application/x-autocad-dwt": "application/x-autocad-dwt",
  "application/x-autocad-dws": "application/x-autocad-dws",
  "application/x-esri-map": "application/x-esri-map",
  "application/x-esri-arcgis-pro-project":
    "application/x-esri-arcgis-pro-project",
  "application/x-rar-compressed": "application/x-rar-compressed",
  "application/vnd.rar": "application/vnd.rar",
  "application/zip": "application/zip",
  "application/octet-stream": "application/octet-stream",
  "application/x-shapefile": "application/x-shapefile",
  "application/x-dbf": "application/x-dbf",
  "application/x-cpg": "application/x-cpg",
  "application/x-shx": "application/x-shx",
  "application/x-qmd": "application/x-qmd",
  "application/vnd.shp": "application/vnd.shp",
  "application/vnd.dbf": "application/vnd.dbf",
  "application/vnd.cpg": "application/vnd.cpg",
  "application/vnd.shx": "application/vnd.shx",
  "application/vnd.qmd": "application/vnd.qmd",
};

const recordFileContext: UploadContext<BaseUploadDto> = {
  endpoint: "/record-attachments",
  module: "record-management",
  uploadContext: "record",
  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/vnd.ms-powerpoint",
    "application/vnd.openxmlformats-officedocument.presentationml.presentation",
    "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",
    ".ppt",
    ".pptx",
    ".dwg",
    ".dxf",
    ".dwt",
    ".bak",
    ".sv$",
    ".dws",
    ".mxd",
    ".aprx",
    ".rar",
    ".zip",
    ".cpg",
    ".dbf",
    ".qmd",
    ".shp",
    ".shx",
  ],
  maxSizeMB: 500,
  buildBody: (file, fileName, recordContentId) => {
    // Infer MIME type from file extension if file.type is empty
    let contentType = mimeToExtension[file.type] || file.type;

    if (!contentType && 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: (
          file.name.split(".").pop() ||
          file.type.split("/").pop() ||
          "bin"
        ).toLowerCase(),
        size: file.size,
      },
      name: { am: file.name, en: file.name },
      recordContentId,
    };
  },
};

export const recordFileService = {
  uploadMeta: (file: File, fileName: string, employeeId: string) =>
    uploadMeta(
      recordFileContext,
      file,
      fileName,
      employeeId,
      recordAxiosInstance,
    ),
  updateStatus: (fileId: string, parentId: string, uploaded: boolean) =>
    updateStatus(
      "/record-attachments",
      fileId,
      parentId,
      uploaded,
      recordAxiosInstance,
    ),
  uploadFile,
};
