import { Injectable } from '@nestjs/common';
import { BaseIndexManager } from 'src/core/modules/domain/usecase/managers/base-index.manager';
import { TransactionEntity } from '../../entities/transaction.entity';
import { SelectQueryBuilder } from 'typeorm';
import {
  Param,
  RelationParam,
} from 'src/core/modules/domain/entities/base-filter.entity';
import { BetweenQueryHelper } from 'src/core/helpers/query/between-query.helper';
import { STATUS } from 'src/core/strings/constants/base.constants';

@Injectable()
export class IndexTransactionManager extends BaseIndexManager<TransactionEntity> {
  async prepareData(): Promise<void> {
    return;
  }

  async beforeProcess(): Promise<void> {
    return;
  }

  async afterProcess(): Promise<void> {
    this.result?.data?.map((item) => {
      const activeRefund = item['refunds'].find(
        (refund) => ![STATUS.CANCEL].includes(refund.status),
      );

      Object.assign(item, {
        refund_type: activeRefund?.type ?? null,
        refund_code: activeRefund?.code ?? null,
        refund_date: activeRefund?.refund_date ?? null,
      });

      delete item['refunds'];
    });
    return;
  }

  get relations(): RelationParam {
    return {
      // relation only join (for query purpose)
      joinRelations: [],

      // relation join and select (relasi yang ingin ditampilkan),
      selectRelations: ['items', 'refunds'],

      // relation yang hanya ingin dihitung (akan return number)
      countRelations: [],
    };
  }

  get selects(): string[] {
    return [
      `${this.tableName}.id`,
      `${this.tableName}.status`,
      `${this.tableName}.invoice_code`,
      `${this.tableName}.creator_counter_no`,
      `${this.tableName}.creator_counter_name`,
      `${this.tableName}.booking_date`,
      `${this.tableName}.no_of_group`,
      `${this.tableName}.type`,
      `${this.tableName}.payment_total`,
      `${this.tableName}.customer_type`,
      `${this.tableName}.customer_name`,
      `${this.tableName}.customer_phone`,
      `${this.tableName}.customer_description`,
      `${this.tableName}.customer_email`,
      `${this.tableName}.invoice_date`,
      `${this.tableName}.settlement_date`,
      `${this.tableName}.created_at`,
      `${this.tableName}.creator_id`,
      `${this.tableName}.creator_name`,
      `${this.tableName}.updated_at`,
      `${this.tableName}.editor_id`,
      `${this.tableName}.editor_name`,

      `${this.tableName}.payment_code`,
      `${this.tableName}.payment_card_information`,
      `${this.tableName}.payment_type`,
      `${this.tableName}.payment_type_counter`,
      `${this.tableName}.payment_date`,
      `${this.tableName}.payment_total_pay`,
      `${this.tableName}.payment_type_method_id`,
      `${this.tableName}.payment_type_method_name`,
      `${this.tableName}.payment_type_method_number`,

      `${this.tableName}.payment_midtrans_url`,

      `refunds.id`,
      `refunds.code`,
      `refunds.refund_date`,
      `refunds.request_date`,
      `refunds.type`,
    ];
  }

  get specificFilter(): Param[] {
    return [
      {
        cols: `${this.tableName}.invoice_code`,
        data: this.filterParam.invoice_codes,
      },
      {
        cols: `${this.tableName}.type::text`,
        data: this.filterParam.types,
      },
      {
        cols: `${this.tableName}.customer_type::text`,
        data: this.filterParam.customer_types,
      },
      {
        cols: `${this.tableName}.customer_name`,
        data: this.filterParam.customer_names,
      },
      {
        cols: `${this.tableName}.payment_type::text`,
        data: this.filterParam.payment_types,
      },
      {
        cols: `${this.tableName}.payment_bank`,
        data: this.filterParam.payment_banks,
      },
      {
        cols: `refund.code`,
        data: this.filterParam.refund_codes,
      },
      {
        cols: `${this.tableName}.creator_name`,
        data: this.filterParam.creator_names,
      },
    ];
  }

  setQueryFilter(
    queryBuilder: SelectQueryBuilder<TransactionEntity>,
  ): SelectQueryBuilder<TransactionEntity> {
    if (this.filterParam.booking_date_from) {
      new BetweenQueryHelper(
        queryBuilder,
        this.tableName,
        'booking_date',
        this.filterParam.booking_date_from,
        this.filterParam.booking_date_to,
        'booking_date',
      ).getQuery();
    }

    if (this.filterParam.invoice_date_from) {
      new BetweenQueryHelper(
        queryBuilder,
        this.tableName,
        'invoice_date',
        this.filterParam.invoice_date_from,
        this.filterParam.invoice_date_to,
        'invoice_date',
      ).getQuery();
    }

    if (this.filterParam.settlement_date_from) {
      new BetweenQueryHelper(
        queryBuilder,
        this.tableName,
        'settlement_date',
        this.filterParam.settlement_date_from,
        this.filterParam.settlement_date_to,
        'settlement_date',
      ).getQuery();
    }

    if (this.filterParam.request_refund_date_from) {
      new BetweenQueryHelper(
        queryBuilder,
        this.tableName,
        'request_refund_date',
        this.filterParam.request_refund_date_from,
        this.filterParam.request_refund_date_to,
        'request_refund',
      ).getQuery();
    }
    queryBuilder.andWhere(`${this.tableName}.is_recap_transaction is false`);
    return queryBuilder;
  }
}
