import { XSSUploadValidationContext } from "./types";

/**
 * Upload context definitions for all entry points
 * Defines allowed MIME types, extensions, and size limits per context
 */

/**
 * Record management upload context
 * For user records and document uploads
 */
export const recordManagementContext: XSSUploadValidationContext = {
  uploadContext: "record",
  allowedMimeTypes: [
    "application/pdf",
    "application/msword",
    "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    "application/vnd.ms-excel",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    "image/jpeg",
    "image/jpeg",
    "image/png",
    "image/gif",
    "text/plain",
    "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: [
    ".pdf",
    ".doc",
    ".docx",
    ".xls",
    ".xlsx",
    ".jpg",
    ".jpeg",
    ".png",
    ".gif",
    ".txt",
    ".cpg",
    ".dbf",
    ".qmd",
    ".shp",
    ".shx",
  ],
  maxSizeMB: 50,
};

/**
 * External portal upload context
 * For external user document uploads
 */
export const externalPortalContext: XSSUploadValidationContext = {
  uploadContext: "external-portal",
  allowedMimeTypes: [
    "application/pdf",
    "application/msword",
    "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    "image/jpeg",
    "image/png",
    "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: [
    ".pdf",
    ".doc",
    ".docx",
    ".jpg",
    ".jpeg",
    ".png",
    ".dwg",
    ".dxf",
    ".dwt",
    ".bak",
    ".sv$",
    ".dws",
    ".mxd",
    ".aprx",
    ".rar",
    ".zip",
    ".cpg",
    ".dbf",
    ".qmd",
    ".shp",
    ".shx",
  ],
  maxSizeMB: 25,
};

/**
 * Incoming record upload context
 * For incoming record attachments
 */
export const incomingRecordContext: XSSUploadValidationContext = {
  uploadContext: "incoming",
  allowedMimeTypes: [
    "application/pdf",
    "application/msword",
    "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    "image/jpeg",
    "image/png",
    "text/plain",
    "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: [".pdf", ".doc", ".docx", ".jpg", ".jpeg", ".png", ".txt", ".cpg", ".dbf", ".qmd", ".shp", ".shx"],
  maxSizeMB: 50,
};

/**
 * Outgoing record upload context
 * For outgoing record attachments
 */
export const outgoingRecordContext: XSSUploadValidationContext = {
  uploadContext: "outgoing",
  allowedMimeTypes: [
    "application/pdf",
    "application/msword",
    "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    "image/jpeg",
    "image/png",
    "text/plain",
    "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: [".pdf", ".doc", ".docx", ".jpg", ".jpeg", ".png", ".txt", ".cpg", ".dbf", ".qmd", ".shp", ".shx"],
  maxSizeMB: 50,
};

/**
 * Signature and seal upload context
 * For digital signatures and official seals
 */
export const signatureContext: XSSUploadValidationContext = {
  uploadContext: "signature",
  allowedMimeTypes: ["image/jpeg", "image/png", "image/gif"],
  allowedExtensions: [".jpg", ".jpeg", ".png", ".gif"],
  maxSizeMB: 10,
};

/**
 * Get upload context by name
 * @param contextName - Name of the upload context
 * @returns Upload context configuration
 */
export function getUploadContext(
  contextName:
    | "record"
    | "external-portal"
    | "incoming"
    | "outgoing"
    | "signature",
): XSSUploadValidationContext {
  const contexts: Record<string, XSSUploadValidationContext> = {
    record: recordManagementContext,
    "external-portal": externalPortalContext,
    incoming: incomingRecordContext,
    outgoing: outgoingRecordContext,
    signature: signatureContext,
  };

  return contexts[contextName] || recordManagementContext;
}

/**
 * Get secure default context
 * Used when no specific context is provided
 * Blocks dangerous file types by default
 */
export const secureDefaultContext: XSSUploadValidationContext = {
  uploadContext: "record",
  allowedMimeTypes: [
    "application/pdf",
    "image/jpeg",
    "image/png",
    "text/plain",
  ],
  allowedExtensions: [".pdf", ".jpg", ".jpeg", ".png", ".txt"],
  maxSizeMB: 500,
};
