import mongoose, { Model } from 'mongoose';

import { MODEL_NAMES } from './_models';

export const PROSPECT_UPDATE_TYPES = ['fields_update', 'files_update'] as const;
export type TProspectUpdateType = typeof PROSPECT_UPDATE_TYPES[number];

export interface IProspectExternalUpdate {
  prospectId: mongoose.Types.ObjectId;
  update: unknown;
  type: TProspectUpdateType;
  updatedBy: string;
}

export type TProspectExternalUpdateModel = Model<IProspectExternalUpdate, object, object>;

export const ProspectExternalUpdateSchema = new mongoose.Schema<IProspectExternalUpdate, TProspectExternalUpdateModel>(
  {
    prospectId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.CRMProspects,
      required: true,
      index: true,
    },
    type: {
      type: String,
      enum: PROSPECT_UPDATE_TYPES,
      required: true,
    },
    update: {
      type: mongoose.Schema.Types.Mixed,
    },
    updatedBy: {
      type: String,
    },
  },
  {
    timestamps: { createdAt: true, updatedAt: false },
    versionKey: false,
  },
);

export const ProspectExternalUpdate = mongoose.model<IProspectExternalUpdate, TProspectExternalUpdateModel>(MODEL_NAMES.CRMProspectExternalUpdates, ProspectExternalUpdateSchema);
