import mongoose from 'mongoose';

import { MODEL_NAMES } from './_models';
import { ISelectedMonth } from '../helpers/date.helper';

export interface ILoanStatementStatus extends ISelectedMonth {
  productId: mongoose.Types.ObjectId;
  actual: boolean;
  locked: boolean;
  lastCalculated: Date;
}

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

export interface ILoanStatementStatusLean extends ILoanStatementStatus {
  _id: mongoose.Types.ObjectId;
  createdAt: Date;
  updatedAt: Date;
}

export interface ILoanStatementStatusPlain extends Omit<ILoanStatementStatus, 'productId'> {
  _id: string;
  productId: string;
}

export type TLoanStatementStatusModel = mongoose.Model<ILoanStatementStatus>;

export const LoanStatementStatusSchema = new mongoose.Schema(
  {
    year: {
      type: Number,
      required: true,
    },
    month: {
      type: Number,
      required: true,
    },
    productId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.loanProducts,
      required: true,
    },
    actual: {
      type: Boolean,
    },
    locked: {
      type: Boolean,
    },
    lastCalculated: {
      type: Date,
    },
    __v: { type: Number, select: false },
  },
  { timestamps: true },
);

export const LoanStatementStatusModel: mongoose.Model<ILoanStatementStatusDoc> = mongoose.model<ILoanStatementStatusDoc>(MODEL_NAMES.loanStatementStatus, LoanStatementStatusSchema);
