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

@Injectable()
export class ProfitShareFormulaDataOrchestrator {
  constructor(
    private updateManager: UpdateProfitShareFormulaManager,
    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();
  }
}
