import { Injectable } from '@nestjs/common';
import { BaseIndexManager } from 'src/core/modules/domain/usecase/managers/base-index.manager';
import { RefundEntity } from '../../entities/refund.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';

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

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

  async afterProcess(): Promise<void> {
    this.result?.data?.map((item) => {
      delete item['transaction']?.id;
      Object.assign(item, {
        ...item['transaction'],
      });

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

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

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

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

  get selects(): string[] {
    return [
      `${this.tableName}.id`,
      `${this.tableName}.code`,
      `${this.tableName}.status`,
      `${this.tableName}.type`,

      `${this.tableName}.refund_reason`,
      `${this.tableName}.refund_reason_type`,
      `${this.tableName}.request_date`,
      `${this.tableName}.refund_date`,
      `${this.tableName}.created_at`,
      `${this.tableName}.updated_at`,
      `${this.tableName}.creator_name`,
      `${this.tableName}.editor_name`,

      `${this.tableName}.refund_total`,
      `${this.tableName}.bank_name`,
      `${this.tableName}.bank_account_name`,
      `${this.tableName}.bank_account_number`,

      `transaction.id`,
      `transaction.invoice_code`,
      `transaction.settlement_date`,
      `transaction.payment_total`,
      `transaction.customer_name`,
      `transaction.customer_phone`,
      `transaction.customer_email`,
      `transaction.customer_description`,
    ];
  }

  get specificFilter(): Param[] {
    return [
      {
        cols: `${this.tableName}.code`,
        data: this.filterParam.codes,
      },
      {
        cols: `transaction.invoice_code`,
        data: this.filterParam.invoice_codes,
      },
      {
        cols: `${this.tableName}.bank_name`,
        data: this.filterParam.bank_names,
      },
      {
        cols: `${this.tableName}.bank_account_name`,
        data: this.filterParam.bank_account_names,
      },
      {
        cols: `${this.tableName}.bank_account_number`,
        data: this.filterParam.bank_account_numbers,
      },
      {
        cols: `transaction.customer_name`,
        data: this.filterParam.contact_names,
      },
      {
        cols: `${this.tableName}.creator_name`,
        data: this.filterParam.creator_names,
      },
    ];
  }

  setQueryFilter(
    queryBuilder: SelectQueryBuilder<RefundEntity>,
  ): SelectQueryBuilder<RefundEntity> {
    if (!!this.filterParam.refund_date_from)
      new BetweenQueryHelper(
        queryBuilder,
        this.tableName,
        'refund_date',
        this.filterParam.refund_date_from,
        this.filterParam.refund_date_to ?? this.filterParam.refund_date_from,
        'refund_date',
      ).getQuery();

    if (!!this.filterParam.request_date_from)
      new BetweenQueryHelper(
        queryBuilder,
        this.tableName,
        'request_date',
        this.filterParam.request_date_from,
        this.filterParam.request_date_to ?? this.filterParam.request_date_from,
        'request_date',
      ).getQuery();

    if (!!this.filterParam.settlement_date_from)
      new BetweenQueryHelper(
        queryBuilder,
        'transaction',
        'settlement_date',
        this.filterParam.settlement_date_from,
        this.filterParam.settlement_date_to ??
          this.filterParam.settlement_date_from,
        'settlement_date',
      ).getQuery();

    return queryBuilder;
  }
}
