import mongoose, { Document } from 'mongoose';

import { MODEL_NAMES } from './_models';
import { EEquipmentTypes } from '../enums/equipment-types.enum';

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

export interface IEquipmentSummaryItem {
  amount: number;
  advanceRate: number;
  availability: number;
}

export interface IEquipmentSummary {
  [summaryField: string]: IEquipmentSummaryItem
}

export interface IEquipmentItem {
  active: boolean;
  inventoryName: string;
  type: string;
  amount: number;
  advanceRate: number;
  productId: mongoose.Types.ObjectId;
}

export interface IEquipment {
  borrowerId: mongoose.Types.ObjectId;
  date: Date;
  items: IEquipmentItem[];
  summary: IEquipmentSummary;
}

export interface IEquipmentDoc extends IEquipment, Document {
}

export const EquipmentSchema = new mongoose.Schema(
  {
    borrowerId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.borrowers,
      required: true,
    },
    date: {
      type: Date,
      required: true,
    },
    summary: {
      [EEquipmentTypes.EQUIPMENT]: {
        amount: {
          type: Number,
          required: true,
        },
        availability: {
          type: Number,
          required: true,
        },
        advanceRate: {
          type: Number,
          required: true,
        },
      },
      [EEquipmentTypes.REAL_ESTATE]: {
        amount: {
          type: Number,
          required: true,
        },
        availability: {
          type: Number,
          required: true,
        },
        advanceRate: {
          type: Number,
          required: true,
        },
      },
      [EEquipmentTypes.BOOT_COLLATERAL]: {
        amount: {
          type: Number,
          required: true,
        },
        availability: {
          type: Number,
          required: true,
        },
        advanceRate: {
          type: Number,
          required: true,
        },
      },
      netTotal: {
        amount: {
          type: Number,
          required: true,
        },
        availability: {
          type: Number,
          required: true,
        },
        advanceRate: {
          type: Number,
          required: true,
        },
      },
      [EEquipmentTypes.INTELLECTUAL_PROPERTY]: {
        amount: {
          type: Number,
          required: true,
        },
        availability: {
          type: Number,
          required: true,
        },
        advanceRate: {
          type: Number,
          required: true,
        },
      },
      grossTotal: {
        amount: {
          type: Number,
          required: true,
        },
        availability: {
          type: Number,
          required: true,
        },
        advanceRate: {
          type: Number,
          required: true,
        },
      },
    },
    items: [
      {
        active: {
          type: Boolean,
          required: true,
        },
        inventoryName: {
          type: String,
          trim: true,
          required: true,
        },
        type: {
          type: String,
          required: true,
          trim: true,
        },
        amount: {
          type: Number,
          required: true,
        },
        advanceRate: {
          type: Number,
          required: false,
        },
        productId: {
          type: mongoose.Types.ObjectId,
          ref: MODEL_NAMES.loanProducts,
          required: false,
        }
      },
    ],
    __v: { type: Number, select: false },
  },
  { timestamps: true }
);

EquipmentSchema.plugin(mongooseLeanId);

export default mongoose.model<IEquipmentDoc>(MODEL_NAMES.equipment, EquipmentSchema);
