import mongoose from 'mongoose';

import { MODEL_NAMES } from './_models';

export interface IParticipation {
  loanProductId: string;
  settlementFrequency: string;
  settlementDate: string | number | null;
}

export interface IParticipationMongo extends Omit<IParticipation, 'loanProductId'> {
  loanProductId: mongoose.Types.ObjectId;
}

export type TParticipationDoc = mongoose.HydratedDocument<IParticipationMongo>;

export interface IParticipationLean extends IParticipationMongo {
  _id: mongoose.Types.ObjectId;
  createdAt: Date;
  updatedAt: Date;
}

export interface IParticipationPlain extends IParticipation {
  _id: string;
}

export type TParticipationModel = mongoose.Model<IParticipationMongo>;

export const ParticipationSchema = new mongoose.Schema(
  {
    loanProductId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.loanProducts,
      required: true,
    },
    settlementFrequency: {
      type: String,
      required: true,
    },
    settlementDate: {
      type: mongoose.Schema.Types.Mixed,
      required: true,
    },
    __v: { type: Number, select: false },
  },
  { timestamps: true },
);

export const Participation = mongoose.model<IParticipationMongo>(MODEL_NAMES.participations, ParticipationSchema);
