import mongoose, { Model } from 'mongoose';

import { MODEL_NAMES } from './_models';

export enum EMonthEndDataType {
  BALANCE = 'BALANCE',
  FLOATED_BALANCE = 'FLOATED_BALANCE',
  STATEMENT_BALANCE = 'STATEMENT_BALANCE',
}

export interface IMonthEndData {
  _id?: mongoose.Types.ObjectId;
  productId: mongoose.Types.ObjectId;
  year: number;
  month: number;
  dataType: EMonthEndDataType;
  dataValue: number;
}

type MonthEndDataModel = Model<IMonthEndData>;

export const MonthEndDataSchema = new mongoose.Schema<IMonthEndData, MonthEndDataModel>(
  {
    productId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.loanProducts,
      required: true,
    },
    year: {
      type: Number,
      required: true,
    },
    month: {
      type: Number,
      required: true,
    },
    dataType: {
      type: String,
      required: true,
      trim: true,
      enum: Object.values(EMonthEndDataType),
    },
    dataValue: {
      type: Number,
      required: true,
    },
  },
  {
    timestamps: { createdAt: true, updatedAt: true },
  },
);

export const MonthEndData = mongoose.model<IMonthEndData, MonthEndDataModel>(MODEL_NAMES.monthEndData, MonthEndDataSchema);
