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

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

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

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

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

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

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

  get selects(): string[] {
    return [
      `${this.tableName}.id`,
      `${this.tableName}.type`,
      `${this.tableName}.reconciliation_status`,
      `${this.tableName}.reconciliation_mdr`,
      `${this.tableName}.reconciliation_confirm_date`,
      `${this.tableName}.reconciliation_confirm_by`,

      `${this.tableName}.customer_name`,
      `${this.tableName}.creator_counter_no`,
      `${this.tableName}.creator_counter_name`,

      `${this.tableName}.invoice_code`,
      `${this.tableName}.booking_date`,
      `${this.tableName}.payment_type`,
      `${this.tableName}.payment_type_counter`,
      `${this.tableName}.payment_type_method_id`,
      `${this.tableName}.payment_type_method_name`,
      `${this.tableName}.payment_type_method_number`,
      `${this.tableName}.payment_code_reference`,
      `${this.tableName}.payment_code`,
      `${this.tableName}.payment_date`,
      `${this.tableName}.payment_date_bank`,

      `${this.tableName}.payment_total`,
      `${this.tableName}.payment_total_net_profit`,
    ];
  }

  get specificFilter(): Param[] {
    return [
      {
        cols: `${this.tableName}.reconciliation_status::text`,
        data: this.filterParam.statuses,
        isStatus: true,
      },
      {
        cols: `${this.tableName}.customer_name`,
        data: this.filterParam.customer_names,
      },
      {
        cols: `${this.tableName}.reconciliation_confirm_by`,
        data: this.filterParam.confirm_by_names,
      },
      {
        cols: `${this.tableName}.payment_type_method_name`,
        data: this.filterParam.payment_banks,
      },
      {
        cols: `${this.tableName}.payment_type_method_number`,
        data: this.filterParam.payment_bank_numbers,
      },
    ];
  }

  setQueryFilter(
    queryBuilder: SelectQueryBuilder<TransactionEntity>,
  ): SelectQueryBuilder<TransactionEntity> {
    if (this.filterParam.payment_date_from) {
      new BetweenQueryHelper(
        queryBuilder,
        this.tableName,
        'payment_date',
        this.filterParam.payment_date_from,
        this.filterParam.payment_date_to,
        'payment_created',
      ).getQuery();
    }

    if (this.filterParam.transaction_type) {
      queryBuilder.andWhere(`${this.tableName}.type In (:...types)`, {
        types: [this.filterParam.transaction_type],
      });
    }

    if (this.filterParam.counter_no) {
      queryBuilder.andWhere(
        `${this.tableName}.creator_counter_no In (:...counters)`,
        {
          counters: [this.filterParam.counter_no],
        },
      );
    }

    if (this.filterParam.counter_name) {
      queryBuilder.andWhere(
        `${this.tableName}.creator_counter_name In (:...counterNames)`,
        {
          counterNames: [this.filterParam.counter_name],
        },
      );
    }

    if (this.filterParam.payment_type) {
      queryBuilder.andWhere(
        `${this.tableName}.payment_type::text In (:...paymentType)`,
        {
          paymentType: [this.filterParam.payment_type],
        },
      );
    }

    if (this.filterParam.payment_via) {
      queryBuilder.andWhere(
        `${this.tableName}.payment_type_method_name::text LIKE :type`,
        {
          type: `%${this.filterParam.payment_via}%`,
        },
      );
    }

    if (this.filterParam.payment_bank) {
      queryBuilder.andWhere(
        `${this.tableName}.payment_type_method_name::text LIKE :banks`,
        {
          banks: `%${this.filterParam.payment_bank}%`,
        },
      );
    }

    if (this.filterParam.confirmation_date_from) {
      const confirmationDateFrom = new Date(
        this.filterParam?.confirmation_date_from,
      ).getTime();
      const confirmationDateTo = new Date(
        `${this.filterParam?.confirmation_date_to?.split(' ')[0]} 23:59:59`,
      ).getTime();

      new BetweenQueryHelper(
        queryBuilder,
        this.tableName,
        'reconciliation_confirm_date',
        confirmationDateFrom,
        confirmationDateTo,
        'reconciliation_confirm_dated',
      ).getQuery();
    }

    if (!this.filterParam.statuses || this.filterParam.statuses?.length === 0) {
      queryBuilder.andWhere(
        `${this.tableName}.reconciliation_status Not In (:...statuses)`,
        {
          statuses: [STATUS.DRAFT],
        },
      );
    }

    return queryBuilder;
  }
}
