import { Injectable, UnprocessableEntityException } from '@nestjs/common';
import { BaseDataService } from 'src/core/modules/data/service/base-data.service';
import { SalesPriceFormulaEntity } from '../../domain/entities/sales-price-formula.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { SalesPriceFormulaModel } from '../models/sales-price-formula.model';
import { CONNECTION_NAME } from 'src/core/strings/constants/base.constants';
import { Repository } from 'typeorm';
import { FormulaType } from '../../constants';
import { TaxModel } from 'src/modules/transaction/tax/data/models/tax.model';
import { ItemModel } from 'src/modules/item-related/item/data/models/item.model';

@Injectable()
export class SalesPriceFormulaDataService extends BaseDataService<SalesPriceFormulaEntity> {
  constructor(
    @InjectRepository(SalesPriceFormulaModel, CONNECTION_NAME.DEFAULT)
    private repo: Repository<SalesPriceFormulaModel>,
    @InjectRepository(TaxModel, CONNECTION_NAME.DEFAULT)
    private tax: Repository<TaxModel>,
    @InjectRepository(ItemModel, CONNECTION_NAME.DEFAULT)
    private item: Repository<ItemModel>,
  ) {
    super(repo);
  }

  profitShareFormula() {
    return this.repo.find({
      where: {
        type: FormulaType.PROFIT_SHARE,
      },
    });
  }

  async itemTax(id: string) {
    const item = await this.item.findOne({
      relations: ['tenant'],
      where: {
        id,
      },
    });

    const tenantShareMargin = item?.tenant?.share_margin;
    const profitShare = (item?.share_profit ?? 0) / 100;
    const tenantShare = tenantShareMargin ? (100 - tenantShareMargin) / 100 : 0;

    return { profitShare, tenantShare };
  }

  async salesPriceFormula() {
    const salesFormula = await this.repo.findOne({
      where: {
        type: FormulaType.SALES_PRICE,
      },
    });

    const taxes = await this.tax.find();

    for (const tax of taxes) {
      salesFormula.formula_string = salesFormula.formula_string.replace(
        tax.name,
        `(${tax.formula_string})`,
      );
    }

    const counter = {};
    do {
      let isInfinite = false;
      for (const tax of taxes) {
        salesFormula.formula_string = salesFormula.formula_string.replace(
          `${tax.name}_value`,
          `(${tax.formula_string})`,
        );

        if (salesFormula.formula_string.includes(`${tax.name}_value`))
          counter[tax.name] = counter[tax.name] ? counter[tax.name] + 1 : 1;

        for (const count of Object.keys(counter)) {
          if (!isInfinite && counter[count] > 50) isInfinite = true;
        }
      }

      if (isInfinite) {
        throw new UnprocessableEntityException({
          message: 'Infinity Loop Formula, please fix formula',
          error: 'Infinity Loop Formula',
          meta: counter,
        });
      }
    } while (salesFormula.formula_string.includes('_value'));

    return salesFormula;
  }
}
