import mongoose from 'mongoose';
import { MODEL_NAMES } from './_models';

export const TERM_LOAN_SETTINGS_FIELDS = [
  'termMonths',
  'amortizationProfileMonths',
  'gracePeriodMonths',
  'principalDueDay',
];

export interface ITermLoanSettingsOnly {
  termMonths: number;
  amortizationProfileMonths: number;
  gracePeriodMonths: number;
  principalDueDay: number;
}

export interface ITermLoanSettings extends ITermLoanSettingsOnly {
  termLoanId: mongoose.Types.ObjectId;
}

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

export interface ITermLoanSettingsLean extends ITermLoanSettings {
  _id: mongoose.Types.ObjectId;
  createdAt: Date;
  updatedAt: Date;
}

export interface ITermLoanSettingsPlain extends Omit<ITermLoanSettings, 'termLoanId'> {
  _id: string;
  termLoanId: string;
}

export type TTermLoanSettingsModel = mongoose.Model<ITermLoanSettings>;

export const TermLoanSettingsSchema = new mongoose.Schema(
  {
    termLoanId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.termLoan,
      required: true,
    },
    termMonths: {
      type: Number,
      required: true,
    },
    amortizationProfileMonths: {
      type: Number,
      required: true,
    },
    gracePeriodMonths: {
      type: Number,
      required: true,
    },
    principalDueDay: {
      type: Number,
      required: true,
    },
    __v: { type: Number, select: false },
  },
  { timestamps: true },
);

export const TermLoanSettingsModel: mongoose.Model<ITermLoanSettingsDoc> = mongoose.model<ITermLoanSettingsDoc>(MODEL_NAMES.termLoanSettings, TermLoanSettingsSchema);
