import { jobModel } from '../../schemas/production/job.schema.js';
import { subJobModel } from '../../schemas/production/subjob.schema.js';
import { printerModel } from '../../schemas/production/printer.schema.js';
import { filamentModel } from '../../schemas/management/filament.schema.js';
import { gcodeFileModel } from '../../schemas/production/gcodefile.schema.js';
import { partModel } from '../../schemas/management/part.schema.js';
import { productModel } from '../../schemas/management/product.schema.js';
import { vendorModel } from '../../schemas/management/vendor.schema.js';
import { filamentStockModel } from '../../schemas/inventory/filamentstock.schema.js';
import { stockEventModel } from '../../schemas/inventory/stockevent.schema.js';
import { stockAuditModel } from '../../schemas/inventory/stockaudit.schema.js';
import { partStockModel } from '../../schemas/inventory/partstock.schema.js';
import { auditLogModel } from '../../schemas/management/auditlog.schema.js';
import { userModel } from '../../schemas/management/user.schema.js';
import { noteTypeModel } from '../../schemas/management/notetype.schema.js';
import { noteModel } from '../../schemas/misc/note.schema.js';
import { documentSizeModel } from '../../schemas/management/documentsize.schema.js';
import { documentTemplateModel } from '../../schemas/management/documenttemplate.schema.js';
import { hostModel } from '../../schemas/management/host.schema.js';
import { documentPrinterModel } from '../../schemas/management/documentprinter.schema.js';
import { documentJobModel } from '../../schemas/management/documentjob.schema.js';
import { fileModel } from '../../schemas/management/file.schema.js';

// Map prefixes to models and id fields
const PREFIX_MODEL_MAP = {
  PRN: { model: printerModel, idField: '_id', type: 'printer' },
  FIL: { model: filamentModel, idField: '_id', type: 'filament' },
  GCF: { model: gcodeFileModel, idField: '_id', type: 'gcodeFile' },
  JOB: { model: jobModel, idField: '_id', type: 'job' },
  PRT: { model: partModel, idField: '_id', type: 'part' },
  PRD: { model: productModel, idField: '_id', type: 'product' },
  VEN: { model: vendorModel, idField: '_id', type: 'vendor' },
  SJB: { model: subJobModel, idField: '_id', type: 'subJob' },
  FLS: { model: filamentStockModel, idField: '_id', type: 'filamentStock' },
  SEV: { model: stockEventModel, idField: '_id', type: 'stockEvent' },
  SAU: { model: stockAuditModel, idField: '_id', type: 'stockAudit' },
  PTS: { model: partStockModel, idField: '_id', type: 'partStock' },
  PDS: { model: null, idField: '_id', type: 'productStock' }, // No productStockModel found
  ADL: { model: auditLogModel, idField: '_id', type: 'auditLog' },
  USR: { model: userModel, idField: '_id', type: 'user' },
  NTY: { model: noteTypeModel, idField: '_id', type: 'noteType' },
  NTE: { model: noteModel, idField: '_id', type: 'note' },
  DSZ: { model: documentSizeModel, idField: '_id', type: 'documentSize' },
  DTP: { model: documentTemplateModel, idField: '_id', type: 'documentTemplate' },
  DPR: { model: documentPrinterModel, idField: '_id', type: 'documentPrinter' },
  DJB: { model: documentJobModel, idField: '_id', type: 'documentJob' },
  HST: { model: hostModel, idField: '_id', type: 'host' },
  FLE: { model: fileModel, idField: '_id', type: 'file' },
};

/**
 * Get all models from the PREFIX_MODEL_MAP
 * @returns {Array} Array of model entries with model, idField, and type properties
 */
export const getAllModels = () => {
  return Object.values(PREFIX_MODEL_MAP);
};

/**
 * Get a model by its type name
 * @param {string} name - The type name of the model (e.g., 'printer', 'filament', 'job')
 * @returns {Object|null} The model entry or null if not found
 */
export const getModelByName = (name) => {
  const entry = Object.values(PREFIX_MODEL_MAP).find((entry) => entry.type === name);
  return entry || null;
};

/**
 * Get a model by its prefix
 * @param {string} prefix - The prefix of the model (e.g., 'PRN', 'FIL', 'JOB')
 * @returns {Object|null} The model entry or null if not found
 */
export const getModelByPrefix = (prefix) => {
  return PREFIX_MODEL_MAP[prefix] || null;
};

// Export the PREFIX_MODEL_MAP for backward compatibility
export { PREFIX_MODEL_MAP };
