import mongoose, { Document } from 'mongoose';

import { MODEL_NAMES } from './_models';
import { IBorrowerDoc } from './Borrower.model';
import { IEmailRecipient } from './ComplianceItem.model';

const mongooseLeanId = require('../plugins/id.plugin');

export interface IFinancialComplianceBorrowerSettings {
  invoiceSentDate: Date;
  dueDate: Date;
  stopFundingDate: Date;
  lastReminderSentAt: Date;
  accountClearedDate: Date;
  amountOwed: number;
  addToLoan: boolean;
  willPayIn: boolean;
  transferDone: boolean;
  amountReceived: number;
  balanceOutstanding: number;
  reminderCounter: number;
}

export interface IFinancialComplianceBorrower extends IFinancialComplianceBorrowerSettings {
  borrower: IBorrowerDoc;
}

export interface IFinancialComplianceBorrowerDocument extends IFinancialComplianceBorrower, Document {
}

export interface IFinancialComplianceBorrowerDocumentWithEmailingStatus extends IFinancialComplianceBorrowerDocument {
  isEmailingActive: boolean;
  isDailyTransactionsEmailingActive: boolean;
}

export interface IFinancialComplianceBorrowerDocumentWithMails extends IFinancialComplianceBorrowerDocument {
  mainEmails: IEmailRecipient[];
  financialEmails: IEmailRecipient[];
}

export const FinancialComplianceBorrowerSchema = new mongoose.Schema(
  {
    isDeleted: {
      type: Boolean,
      default: false
    },
    borrower: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.borrowers,
      required: true,
    },
    invoiceSentDate: {
      type: Date,
      required: false,
      default: null,
    },
    dueDate: {
      type: Date,
      required: false,
      default: null,
    },
    stopFundingDate: {
      type: Date,
      required: false,
      default: null,
    },
    lastReminderSentAt: {
      type: Date,
      required: false,
      default: null,
    },
    accountClearedDate: {
      type: Date,
      required: false,
      default: null,
    },
    amountOwed: {
      type: Number,
      required: false,
      default: null,
    },
    addToLoan: {
      type: Boolean,
      required: false,
      default: null,
    },
    willPayIn: {
      type: Boolean,
      required: false,
      default: null,
    },
    transferDone: {
      type: Boolean,
      required: false,
      default: null,
    },
    amountReceived: {
      type: Number,
      required: false,
      default: null,
    },
    balanceOutstanding: {
      type: Number,
      required: false,
      default: null,
    },
    reminderCounter: {
      type: Number,
      required: false,
      default: 0,
    },
    __v: { type: Number, select: false },
  }
);

FinancialComplianceBorrowerSchema.plugin(mongooseLeanId);

export default mongoose.model<IFinancialComplianceBorrowerDocument>(MODEL_NAMES.financialComplianceBorrowers, FinancialComplianceBorrowerSchema);
