import { Injectable } from '@nestjs/common';
import { DetailProfitShareFormulaManager } from './managers/detail-profit-share-formula.manager';
import { TABLE_NAME } from 'src/core/strings/constants/table.constants';
import { SalesPriceFormulaReadService } from 'src/modules/transaction/sales-price-formula/data/services/sales-price-formula-read.service';
import { SalesPriceFormulaEntity } from 'src/modules/transaction/sales-price-formula/domain/entities/sales-price-formula.entity';
import { IndexProfitShareFormulaManager } from './managers/index-profit-share-formula.manager';
import { IndexTaxFormulaManager } from './managers/tax-formula.manager';
import { TaxReadService } from 'src/modules/transaction/tax/data/services/tax-read.service';

@Injectable()
export class ProfitShareFormulaReadOrchestrator {
  constructor(
    private detailManager: DetailProfitShareFormulaManager,
    private indexManager: IndexProfitShareFormulaManager,
    private taxManager: IndexTaxFormulaManager,
    private serviceData: SalesPriceFormulaReadService,
    private taxServiceData: TaxReadService,
  ) {}

  async index(): Promise<SalesPriceFormulaEntity[]> {
    this.indexManager.setFilterParam({});
    this.indexManager.setService(this.serviceData, TABLE_NAME.PRICE_FORMULA);
    await this.indexManager.execute();
    const { data } = this.indexManager.getResult();
    return data;
  }

  async tax(): Promise<Partial<SalesPriceFormulaEntity>[]> {
    this.taxManager.setFilterParam({});
    this.taxManager.setService(this.taxServiceData, TABLE_NAME.TAX);
    await this.taxManager.execute();
    const { data } = this.taxManager.getResult();
    return data.map((tax) => {
      return {
        ...tax,
        value_for: tax.name,
      };
    });
  }

  async detail(): Promise<SalesPriceFormulaEntity> {
    this.detailManager.setData('');
    this.detailManager.setService(this.serviceData, TABLE_NAME.PRICE_FORMULA);
    await this.detailManager.execute();
    return this.detailManager.getResult();
  }
}
