import {
  IInventoryAvailabilityDocument,
  IInventoryAvailabilitySummary,
  InventoryAvailabilityModel,
} from '../models/InventoryAvailability.model';
import { IInventoryAvailabilityItem, InventoryAvailabilityItemModel } from '../models/InventoryAvailabilityItem.model';

export const getInventoryAvailabilityByBBCDateId = async (bbcDateId: string): Promise<IInventoryAvailabilityDocument> => {
  return InventoryAvailabilityModel
    .findOne({ bbcDateId })
    .lean();
}

export const saveInventoryAvailabilityItems = async (inventoryAvailabilityId: string, items: IInventoryAvailabilityItem[]) => {
  await InventoryAvailabilityItemModel.deleteMany({ inventoryAvailabilityId });
  await InventoryAvailabilityItemModel.insertMany(items);
}

export const saveInventoryAvailabilitySummary = async (inventoryAvailabilityId: string, summary: IInventoryAvailabilitySummary[]) => {
  await InventoryAvailabilityModel.findByIdAndUpdate(
    inventoryAvailabilityId,
    { summary },
    { upsert: true, new: true }
  );
}

export const updateAvailabilityStatus = async (bbcDateId: string, status: string) => {
  await InventoryAvailabilityModel.updateOne({ bbcDateId }, { status });
}

export const setManualInputsToggle = async (bbcDateId: string, useManualInputs: boolean) => {
  await InventoryAvailabilityModel.updateOne({ bbcDateId }, { useManualInputs });
}
