import mongoose, { Document } from 'mongoose';

import { MODEL_NAMES } from './_models';
import { ICollateralItem } from '../interfaces/collaterals.interface';
import { IBBCSheetDoc } from './BBCSheet.model';
import { IBBCDateDoc } from './BBCDate.model';

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

export const INVENTORY_FIELDS = [
  '_id',
  'skuDate',
  'category',
  'sku',
  'qty',
  'value',
  'expiryDate',
  'location',
  'unitCost',
  'skuDescription1',
  'skuDescription2',
  'skuDescription3',
  'detail1',
  'detail2',
  'detail3',
];

export interface IInventoryItem extends ICollateralItem {
  skuDate: Date;
  category: string;
  sku: string;
  qty: number;
  value: number;
  expiryDate: Date;
  location: string;
  unitCost: number;
  skuDescription1: string;
  skuDescription2: string;
  skuDescription3: string;
  detail1: string;
  detail2: string;
  detail3: string;
}

export interface IInventoryItemDoc extends Document, IInventoryItem {
}

export type ЕInventoryItemDoc = mongoose.HydratedDocument<IInventoryItem>

export interface IInventoryItemLean extends IInventoryItem {
  _id: mongoose.Types.ObjectId;
  createdAt: Date;
  updatedAt: Date;
}

export interface IInventoryItemPlain extends Omit<IInventoryItem, 'bbcSheetId'> {
  _id: string;
  bbcSheetId: string;
  createdAt: Date;
  updatedAt: Date;
}

export type TIInventoryItemModel = mongoose.Model<IInventoryItem>;

export interface IInventoryItemDocWithCalculation extends IInventoryItemDoc {
  bbcSheet: IBBCSheetDoc;
  bbc: IBBCDateDoc;
  expireDays: number;
  skuDays: number;
  sheetName: string;
  inventoryName: string;
  advanceRate: number;
  availability: number;
}

export interface IInventoryItemAvailabilityWithGroups extends IInventoryItemDocWithCalculation {
  groups: string[];
}

interface IMatchedGroup {
  groups: string[];
  items: IInventoryItemAvailabilityWithGroups[];
}

export interface IMatchedGroups {
  [groupName: string]: IMatchedGroup;
}

export const InventoryItemSchema = new mongoose.Schema(
  {
    bbcSheetId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.BBCSheets,
      required: true,
    },
    order: {
      type: Number,
      required: true,
    },
    skuDate: {
      type: Date,
      required: true,
    },
    category: {
      type: String,
      trim: true,
      default: 'NO CATEGORY',
    },
    sku: {
      type: String,
      trim: true,
      required: true,
    },
    lotNumber: {
      type: String,
      trim: true,
      required: false,
    },
    qty: {
      type: Number,
      required: true,
    },
    value: {
      type: Number,
      required: true,
    },
    expiryDate: {
      type: Date,
    },
    location: {
      type: String,
      trim: true,
    },
    unitCost: {
      type: Number,
    },
    skuDescription1: {
      type: String,
      trim: true,
    },
    skuDescription2: {
      type: String,
      trim: true,
    },
    skuDescription3: {
      type: String,
      trim: true,
    },
    detail1: {
      type: String,
      trim: true,
    },
    detail2: {
      type: String,
      trim: true,
    },
    detail3: {
      type: String,
      trim: true,
    },
    __v: { type: Number, select: false },
  },
  { timestamps: true }
);

InventoryItemSchema.plugin(mongooseLeanId);

export const InventoryItemModel: mongoose.Model<IInventoryItemDoc> = mongoose.model<IInventoryItemDoc>(MODEL_NAMES.inventoryItem, InventoryItemSchema);
