import mongoose from 'mongoose';

import { MODEL_NAMES } from './_models';

export interface ITermCalculatedDaily {
  day: string;
  interestRate: number;
  interest: number;
  paymentDate: boolean;
}

export interface ITermLoanCalculated {
  termLoanId: mongoose.Types.ObjectId;
  relevantStatement: string;
  openingBalance: number;
  monthlyInterest: number;
  monthlyPrincipal: number;
  closingBalance: number;
  adminFee: number;
  interestRate: number;
  interestDaily: number;
  gracePeriod: boolean;
  amortization: boolean;
  paymentDueDate: Date;
  payments: {
    amount: number;
    paymentId: mongoose.Types.ObjectId;
  }[];
  disbursements: number;
  dailyResults: ITermCalculatedDaily[];
}

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

export interface ITermLoanCalculatedLean extends ITermLoanCalculated {
  _id: mongoose.Types.ObjectId;
  createdAt: Date;
  updatedAt: Date;
}

export interface ITermLoanCalculatedPlain extends Omit<ITermLoanCalculated, 'termLoanId' | 'payments'> {
  _id: string;
  termLoanId: string;
  payments: {
    amount: number;
    paymentId: string;
  }[];
}

export type TTermLoanCalculatedModel = mongoose.Model<ITermLoanCalculated>;

export const TermLoanCalculatedSchema = new mongoose.Schema(
  {
    termLoanId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.termLoan,
      required: true,
    },
    relevantStatement: {
      type: String,
      required: true,
    },
    openingBalance: {
      type: Number,
      required: true,
    },
    monthlyInterest: {
      type: Number,
      required: true,
    },
    monthlyPrincipal: {
      type: Number,
      required: true,
    },
    closingBalance: {
      type: Number,
      required: true,
    },
    adminFee: {
      type: Number,
      required: true,
    },
    interestRate: {
      type: Number,
      required: true,
    },
    interestDaily: {
      type: Number,
      required: true,
    },
    gracePeriod: {
      type: Boolean,
      required: true,
    },
    amortization: {
      type: Boolean,
      required: true,
    },
    paymentDueDate: {
      type: Date,
      required: false,
    },
    payments: [
      {
        amount: {
          type: Number,
          required: true,
        },
        paymentId: {
          type: mongoose.Schema.Types.ObjectId,
          ref: MODEL_NAMES.loanTransactions,
          required: true,
        },
      },
    ],
    disbursements: {
      type: Number,
      required: true,
    },
    dailyResults: [{
      day: {
        type: String,
        required: true,
      },
      interestRate: {
        type: Number,
        required: true,
      },
      interest: {
        type: Number,
        required: true,
      },
      paymentDate: {
        type: Boolean,
        required: true,
      },
    }],
    __v: { type: Number, select: false },
  },
  { timestamps: true },
);

export const TermLoanCalculatedModel: mongoose.Model<ITermLoanCalculatedDoc> = mongoose.model<ITermLoanCalculatedDoc>(MODEL_NAMES.termLoanCalculated, TermLoanCalculatedSchema);
