import { Injectable } from '@nestjs/common';
import {
  SalesPriceFormulaDataService,
  TransactionSettingDataService,
} from '../../data/services/sales-price-formula-data.service';
import {
  SalesPriceFormulaEntity,
  TransactionSetting,
} 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';
import { UpdateTransactionSettingManager } from './managers/update-transaction-setting.manager';

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

  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();
  }

  async updateTransactionSetting(data): Promise<TransactionSetting> {
    this.transactionSettingManager.setData(data.id, data);
    this.transactionSettingManager.setService(
      this.transactionSettingService,
      TABLE_NAME.TRANSACTION_SETTING,
    );
    await this.transactionSettingManager.execute();
    return this.transactionSettingManager.getResult();
  }
}
