import mongoose, { Model } from 'mongoose';
import Joi from 'joi';

import { MODEL_NAMES } from './_models';

export enum EProspectSourceType {
  LENDER = 'LENDER',
  BROKER = 'BROKER',
  DIRECT = 'DIRECT',
  INVESTMENT_BANK = 'INVESTMENT_BANK',
  CONSULTANT = 'CONSULTANT',
  ATTORNEY = 'ATTORNEY',
  EQUITY = 'EQUITY',
  PROFESSIONAL_ORGANIZATION = 'PROFESSIONAL_ORGANIZATION',
  INSURANCE_AGENCY = 'INSURANCE_AGENCY',
  OTHER = 'OTHER',
}

export interface ICRMProspectSource {
  name: string;
  contactPerson: string;
  emails: string;
  order: number;
  type: EProspectSourceType;
}

export interface ICRMProspectSourceWithId extends ICRMProspectSource {
  _id: string;
}

export const CRMProspectSourceValidationSchema = Joi.object({
  _id: Joi.string(),
  name: Joi.string().required(),
  contactPerson: Joi.string().allow('', null).required(),
  emails: Joi.string().allow('', null).required(),
  order: Joi.number().required().allow(null),
  type: Joi.string().valid(...Object.values(EProspectSourceType), null).required(),
});

export type CRMProspectSourceModel = Model<ICRMProspectSource, object, object>;

export const CRMProspectSourceSchema = new mongoose.Schema<ICRMProspectSource, CRMProspectSourceModel>(
  {
    name: {
      type: String,
      required: true,
    },
    contactPerson: {
      type: String,
      required: false,
      default: '',
    },
    emails: {
      type: String,
      required: false,
      default: '',
    },
    order: {
      type: Number,
      required: true,
    },
    type: {
      type: String,
      enum: [...Object.values(EProspectSourceType), null],
      default: null,
      required: false,
    },
  },
  {
    timestamps: { createdAt: true, updatedAt: true },
    versionKey: false,
  },
);

export const CRMProspectSource = mongoose.model<ICRMProspectSource, CRMProspectSourceModel>(MODEL_NAMES.CRMProspectSources, CRMProspectSourceSchema);
