import { Injectable } from '@nestjs/common';
import { BaseUpdateManager } from 'src/core/modules/domain/usecase/managers/base-update.manager';
import { SalesPriceFormulaEntity } from '../../entities/sales-price-formula.entity';
import { SalesPriceFormulaModel } from '../../../data/models/sales-price-formula.model';
import { SalesPriceFormulaUpdatedEvent } from '../../entities/event/sales-price-formula-updated.event';
import {
  EventTopics,
  columnUniques,
  validateRelations,
} from 'src/core/strings/constants/interface.constants';
import { STATUS } from 'src/core/strings/constants/base.constants';
import { In } from 'typeorm';
import { calculateSalesFormula } from './helpers/calculation-formula.helper';

@Injectable()
export class UpdateSalesPriceFormulaManager extends BaseUpdateManager<SalesPriceFormulaEntity> {
  async validateProcess(): Promise<void> {
    const taxes = await this.dataServiceFirstOpt.getManyByOptions({
      where: {
        status: In([STATUS.ACTIVE]),
      },
    });

    calculateSalesFormula(this.data.formula_string, taxes, 10000, true);
    return;
  }

  async beforeProcess(): Promise<void> {
    return;
  }

  async afterProcess(): Promise<void> {
    return;
  }

  get validateRelations(): validateRelations[] {
    return [];
  }

  get uniqueColumns(): columnUniques[] {
    return [];
  }

  get entityTarget(): any {
    return SalesPriceFormulaModel;
  }

  get eventTopics(): EventTopics[] {
    return [
      {
        topic: SalesPriceFormulaUpdatedEvent,
      },
    ];
  }
}
