import mongoose, { Document } from 'mongoose';

import { MODEL_NAMES } from './_models';

export interface IInventorySeasonalRatesItem {
  sku: string;
  start: Date;
  end: Date;
  advanceRate: number;
}

export interface IInventorySeasonalRatesItemDoc extends IInventorySeasonalRatesItem, Document {
}

export interface IInventorySeasonalRates {
  borrowerId: mongoose.Types.ObjectId;
  items: IInventorySeasonalRatesItem[];
}

export interface IInventorySeasonalRatesDoc extends IInventorySeasonalRates, Document {
  items: IInventorySeasonalRatesItemDoc[];
}

export const InventorySeasonalEntrySchema = new mongoose.Schema(
  {
    borrowerId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.borrowers,
      required: true,
    },
    items: [{
      sku: {
        type: String,
        required: true,
        trim: true,
      },
      start: {
        type: Date,
        required: true,
      },
      end: {
        type: Date,
        required: true,
      },
      advanceRate: {
        type: Number,
        required: true,
      },
    }],
    __v: { type: Number, select: false },
  },
  { timestamps: true },
);

export const InventorySeasonalEntryModel: mongoose.Model<IInventorySeasonalRatesDoc> = mongoose.model<IInventorySeasonalRatesDoc>(MODEL_NAMES.inventorySeasonalRates, InventorySeasonalEntrySchema);
