import mongoose from 'mongoose';

import { MODEL_NAMES } from './_models';

export interface IAssignedProspect {
  borrowerId: mongoose.Types.ObjectId;
  prospectId: mongoose.Types.ObjectId;
  assignedAt?: Date;
  assignedBy?: string;
}

export interface IAssignedProspectCreate extends Omit<IAssignedProspect, 'borrowerId' | 'prospectId'> {
  borrowerId: string;
  prospectId: string;
}

export interface IAssignedProspectDoc extends IAssignedProspect, Document {
  _id: mongoose.Types.ObjectId;
  createdAt: Date;
  updatedAt: Date;
}

export interface IAssignedProspectLean extends IAssignedProspect {
  _id: mongoose.Types.ObjectId;
  createdAt: Date;
  updatedAt: Date;
}

export interface IAssignedProspectPlain extends IAssignedProspect {
  _id: string;
  createdAt: Date;
  updatedAt: Date;
}

export type AssignedProspectModel = mongoose.Model<IAssignedProspectDoc>;

export const AssignedProspectSchema = new mongoose.Schema<IAssignedProspectDoc>({
  prospectId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: MODEL_NAMES.CRMProspects,
    required: true,
  },
  borrowerId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: MODEL_NAMES.borrowers,
    required: true,
  },
  assignedAt: {
    type: Date,
  },
  assignedBy: {
    type: String,
  },
}, {
  timestamps: true,
  versionKey: false,
});

AssignedProspectSchema.index({ borrowerId: 1 }, { unique: true });

export const AssignedProspect = mongoose.model<IAssignedProspectDoc>(MODEL_NAMES.assignedProspects, AssignedProspectSchema);
