import { Injectable } from '@nestjs/common';
import { SalesPriceFormulaDataService } from '../../data/services/sales-price-formula-data.service';
import { SalesPriceFormulaEntity } from '../entities/sales-price-formula.entity';
import { UpdateSalesPriceFormulaManager } from './managers/update-sales-price-formula.manager';
import { TABLE_NAME } from 'src/core/strings/constants/table.constants';
import { FormulaType } from '../../constants';
import { TaxDataService } from 'src/modules/transaction/tax/data/services/tax-data.service';

@Injectable()
export class SalesPriceFormulaDataOrchestrator {
  constructor(
    private updateManager: UpdateSalesPriceFormulaManager,
    private serviceData: SalesPriceFormulaDataService,
    private taxService: TaxDataService,
  ) {}

  async update(data): Promise<SalesPriceFormulaEntity> {
    const formula = await this.serviceData.getOneByOptions({
      where: {
        type: FormulaType.SALES_PRICE,
      },
    });

    this.updateManager.setData(formula.id, data);
    this.updateManager.setService(
      this.serviceData,
      TABLE_NAME.PRICE_FORMULA,
      this.taxService,
    );
    await this.updateManager.execute();
    return this.updateManager.getResult();
  }
}
