import mongoose, { Document } from 'mongoose';

import { MODEL_NAMES } from './_models';
import { IInventoryItemDoc } from './InventoryItem.model';

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

export interface IInventoryAvailabilityItem {
  order: number;
  inventoryAvailabilityId: mongoose.Types.ObjectId;
  inventoryId: mongoose.Types.ObjectId;
  matched: boolean;
  unique: boolean;
  groups?: string[];
  inventoryName?: string;
  advanceRate?: number;
  availability?: number;
  expireDays?: number;
  skuDays?: number;
}

export interface IInventoryAvailabilityItemDoc extends IInventoryAvailabilityItem, Document {
}

export interface IInventoryAvailabilityItemDocWithInventory extends IInventoryAvailabilityItemDoc {
  inventory: IInventoryItemDoc;
}

export const InventoryAvailabilityItemScheme = new mongoose.Schema(
  {
    order: {
      type: Number,
      required: true,
    },
    inventoryAvailabilityId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.inventoryAvailability,
      required: true,
    },
    inventoryId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.inventoryItem,
      required: true,
    },
    matched: {
      type: Boolean,
      required: true,
    },
    unique: {
      type: Boolean,
      required: true,
    },
    groups: [{
      type: String,
      required: false,
      trim: true,
    }],
    inventoryName: {
      type: String,
      required: false,
      trim: true,
    },
    advanceRate: {
      type: Number,
      required: false,
    },
    availability: {
      type: Number,
      required: false,
    },
    expireDays: {
      type: Number,
      required: false,
    },
    skuDays: {
      type: Number,
      required: false,
    },
    __v: { type: Number, select: false },
  },
  { timestamps: true }
);

InventoryAvailabilityItemScheme.plugin(mongooseLeanId);

export const InventoryAvailabilityItemModel: mongoose.Model<IInventoryAvailabilityItemDoc> =  mongoose.model<IInventoryAvailabilityItemDoc>(MODEL_NAMES.inventoryAvailabilityItem, InventoryAvailabilityItemScheme);
