import { EMonthEndDataType, IMonthEndData, MonthEndData } from '../models/MonthEndData.Model';

export class MonthEndDataService {

  async upsertMonthEndData(input: IMonthEndData) {
    const { productId, year, month, dataType, dataValue } = input;

    if (!Object.values(EMonthEndDataType).includes(input.dataType)) {
      throw new Error(`Invalid dataType: ${dataType}`);
    }

    return MonthEndData.findOneAndUpdate(
      {
        productId,
        year,
        month,
        dataType,
      },
      {
        $set: {
          dataValue,
        },
      },
      {
        upsert: true,
        new: true,
        setDefaultsOnInsert: true,
      },
    );
  }

  async getMonthEndData(productId: string, year: number, month: number) {
    return MonthEndData.find({ productId, year, month });
  }
}
