import mongoose from 'mongoose';

import { MODEL_NAMES } from './_models';
import { ICollateralItem } from '../interfaces/collaterals.interface';

export const SALES_FIELDS = [
  '_id',
  'sku',
  'skuName',
  'unitsSold',
  'unitsCost',
  'unitSalePrice',
  'salesPeriodStart',
  'salesPeriodEnd',
  'salesPerDay',
];

export interface ISalesItem extends ICollateralItem {
  sku: string;
  skuName?: string;
  unitsSold: number;
  unitsCost?: number;
  unitSalePrice?: number;
  salesPeriodStart: Date;
  salesPeriodEnd: Date;
  salesPerDay: number;
}

export interface ISalesItemLean extends ISalesItem {
  _id: mongoose.Types.ObjectId;
  createdAt: Date;
  updatedAt: Date;
}

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

export type TSalesItemDoc = mongoose.HydratedDocument<ISalesItem>

export type TSalesItemModel = mongoose.Model<ISalesItem>;

export const SalesItemSchema = new mongoose.Schema(
  {
    bbcSheetId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: MODEL_NAMES.BBCSheets,
      required: true,
    },
    order: {
      type: Number,
      required: true,
    },
    sku: {
      type: String,
      trim: true,
      required: true,
    },
    skuName: {
      type: String,
      trim: true,
      required: false,
    },
    unitsSold: {
      type: Number,
      required: true,
    },
    unitsCost: {
      type: Number,
      required: false,
    },
    unitSalePrice: {
      type: Number,
      required: false,
    },
    salesPeriodStart: {
      type: Date,
    },
    salesPeriodEnd: {
      type: Date,
    },
    salesPerDay: {
      type: Number,
    },
    __v: { type: Number, select: false },
  },
  { timestamps: true }
);

export const SalesItem = mongoose.model<ISalesItem, TSalesItemModel>(MODEL_NAMES.salesItem, SalesItemSchema);
