import mongoose, { Document } from 'mongoose';

import { IReceivableItemDoc } from './ReceivableItem.model';
import { MODEL_NAMES } from './_models';

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

export interface IReceivableAvailabilityItem {
  actualDays: number;
  balanceAfterContra: number;
  balanceAfterCrossAge: number;
  balanceAfterDeduction: number;
  balanceAfterExclusion: number;
  balanceAfterPartially: number;
  bucket30: number;
  bucket60: number;
  bucketCurrent: number;
  contra: number;
  contraDeduction: number;
  creditNotes: number;
  crossAge: number;
  crossAgeDeduction: number;
  customer: string;
  customerConcentration: number;
  customerGroup: string;
  customerGroupId: mongoose.Types.ObjectId;
  customerTitle: string;
  deduction: number;
  deductionPercent: number;
  excludedCustomer: boolean;
  excludedCustomerDeduction: number;
  finalResult: number;
  grossAR: number;
  insuredComponent: number;
  invoiceAmount: number;
  invoiceDate: Date;
  invoiceId: string;
  invoiceNumber: string;
  order?: number;
  partiallyPaidDeduction: number;
  partiallyPaidDifference: boolean;
  permitted60: number;
  permittedCustomerConcentration: number;
  permittedDays: number;
  permittedOver: number;
  preliminaryEligibleBalance: number;
  uninsuredComponent: number;
  waivedDeduction: number;
}

export interface IReceivableAvailabilityItemDoc extends IReceivableAvailabilityItem, Document {
}

export interface IReceivableAvailabilityItemView extends IReceivableAvailabilityItem {
  invoice: IReceivableItemDoc;
}

export const ReceivableAvailabilityItemSchema = new mongoose.Schema(
  {
    availabilityId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.ARAvailabilities,
      required: true,
    },
    order: {
      type: Number,
      required: true,
    },
    invoiceId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.receivableItems,
      required: true,
    },
    bucketCurrent: {
      type: Number,
      required: false,
    },
    bucket30: {
      type: Number,
      required: false,
    },
    bucket60: {
      type: Number,
      required: false,
    },
    actualDays: {
      type: Number,
      required: false,
    },
    permittedDays: {
      type: Number,
      required: false,
    },
    permitted60: {
      type: Number,
      required: false,
    },
    permittedOver: {
      type: Number,
      required: false,
    },
    grossAR: {
      type: Number,
      required: false,
    },
    creditNotes: {
      type: Number,
      required: false,
    },
    preliminaryEligibleBalance: {
      type: Number,
      required: false,
    },
    permittedCustomerConcentration: {
      type: Number,
      required: false,
    },
    customerConcentration: {
      type: Number,
      required: false,
    },
    deductionPercent: {
      type: Number,
      required: false,
    },
    deduction: {
      type: Number,
      required: false,
    },
    balanceAfterDeduction: {
      type: Number,
      required: false,
    },
    partiallyPaidDifference: {
      type: Boolean,
      required: false,
    },
    partiallyPaidDeduction: {
      type: Number,
      required: false,
    },
    balanceAfterPartially: {
      type: Number,
      required: false,
    },
    excludedCustomer: {
      type: Boolean,
      required: false,
    },
    excludedCustomerDeduction: {
      type: Number,
      required: false,
    },
    balanceAfterExclusion: {
      type: Number,
      required: false,
    },
    customerGroupId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.customerGroups,
      required: false,
    },
    contra: {
      type: Number,
      required: false,
    },
    contraDeduction: {
      type: Number,
      required: false,
    },
    balanceAfterContra: {
      type: Number,
      required: false,
    },
    crossAge: {
      type: Number,
      required: false,
    },
    crossAgeDeduction: {
      type: Number,
      required: false,
    },
    balanceAfterCrossAge: {
      type: Number,
      required: false,
    },
    waivedDeduction: {
      type: Number,
      required: false,
    },
    finalResult: {
      type: Number,
      required: false,
    },
    insuredComponent: {
      type: Number,
      required: false,
    },
    uninsuredComponent: {
      type: Number,
      required: false,
    },
    __v: { type: Number, select: false },
  },
  { timestamps: true }
);

ReceivableAvailabilityItemSchema.plugin(mongooseLeanId);

export const ReceivableAvailabilityItemModel = mongoose.model<IReceivableAvailabilityItemDoc>(MODEL_NAMES.ARAvailabilityItems, ReceivableAvailabilityItemSchema);
