import mongoose, { Model } from 'mongoose';

import { MODEL_NAMES } from './_models';

export const PROSPECT_INTERNAL_UPDATE_TYPES = ['created', 'status_change', 'is_declined', 'files_update', 'other'] as const;
export type TProspectInternalUpdateType = typeof PROSPECT_INTERNAL_UPDATE_TYPES[number];

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

export type TProspectInternalUpdateModel = Model<IProspectInternalUpdate, object, object>;

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

export const ProspectInternalUpdate = mongoose.model<IProspectInternalUpdate, TProspectInternalUpdateModel>(MODEL_NAMES.CRMProspectInternalUpdates, ProspectInternalUpdateSchema);
