import { Injectable } from '@nestjs/common';
import { IndexTransactionManager } from './managers/index-transaction.manager';
import { TransactionReadService } from '../../data/services/transaction-read.service';
import { TransactionEntity } from '../entities/transaction.entity';
import { PaginationResponse } from 'src/core/response/domain/ok-response.interface';
import { BaseReadOrchestrator } from 'src/core/modules/domain/usecase/orchestrators/base-read.orchestrator';
import { DetailTransactionManager } from './managers/detail-transaction.manager';
import { TABLE_NAME } from 'src/core/strings/constants/table.constants';
import { PriceCalculator } from './calculator/price.calculator';
import { In } from 'typeorm';
import * as moment from 'moment';

@Injectable()
export class TransactionReadOrchestrator extends BaseReadOrchestrator<TransactionEntity> {
  constructor(
    private indexManager: IndexTransactionManager,
    private detailManager: DetailTransactionManager,
    private serviceData: TransactionReadService,
    private calculator: PriceCalculator,
  ) {
    super();
  }

  async index(params): Promise<PaginationResponse<TransactionEntity>> {
    this.indexManager.setFilterParam(params);
    this.indexManager.setService(this.serviceData, TABLE_NAME.TRANSACTION);
    await this.indexManager.execute();
    return this.indexManager.getResult();
  }

  async summary(posId: string) {
    const today = moment().format('YYYY-MM-DD');
    const summary = await this.serviceData.getSummary(posId, today);
    const lastTransaction = await this.serviceData.getLastTransactionByPos(
      posId,
    );

    return { summary, lastTransaction };
  }

  async detail(dataId: string): Promise<TransactionEntity> {
    this.detailManager.setData(dataId);
    this.detailManager.setService(this.serviceData, TABLE_NAME.TRANSACTION);
    await this.detailManager.execute();
    return this.detailManager.getResult();
  }

  async dummyCalculate(id: string): Promise<void> {
    const transaction = await this.serviceData.getOneByOptions({
      where: {
        id,
      },
      relations: ['items', 'items.bundling_items'],
    });

    const price = await this.calculator.calculate(transaction);
    transaction.payment_total_profit =
      transaction.payment_total -
      price.other.total_profit_share -
      price.other.payment_total_tax -
      price.other.total_share_tenant;
    transaction.payment_total_dpp = price.dpp_value;
    transaction.payment_total_share = price.other.total_profit_share;
    transaction.payment_total_tax = price.other.payment_total_tax;
    console.log(transaction.id);
    await this.serviceData.getRepository().save(transaction);
  }

  ids = [];

  async calculatePrice(): Promise<void> {
    const transactions = await this.serviceData.getManyByOptions({
      where: {
        // is_recap_transaction: false,
        id: In(this.ids),
      },
      relations: ['items', 'items.bundling_items'],
    });

    console.log(transactions.length);
    // return;

    for (const transaction of transactions) {
      try {
        const price = await this.calculator.calculate(transaction);
        transaction.payment_total_profit =
          transaction.payment_total -
          price.other.total_profit_share -
          price.other.payment_total_tax -
          price.other.total_share_tenant;
        transaction.payment_total_dpp = price.dpp_value;
        transaction.payment_total_share = price.other.total_profit_share;
        transaction.payment_total_tax = price.other.payment_total_tax;
        console.log(transaction.id);
        await this.serviceData.getRepository().save(transaction);

        // break;
      } catch (error) {
        console.log(error);
      }
    }
  }
}
