import {
  HttpStatus,
  Injectable,
  UnprocessableEntityException,
} from '@nestjs/common';
import { BaseUpdateStatusManager } from 'src/core/modules/domain/usecase/managers/base-update-status.manager';
import { STATUS } from 'src/core/strings/constants/base.constants';
import {
  EventTopics,
  validateRelations,
} from 'src/core/strings/constants/interface.constants';
import { TransactionModel } from 'src/modules/transaction/transaction/data/models/transaction.model';
import { TransactionChangeStatusEvent } from 'src/modules/transaction/transaction/domain/entities/event/transaction-change-status.event';
import { TransactionEntity } from 'src/modules/transaction/transaction/domain/entities/transaction.entity';

@Injectable()
export class CancelReconciliationManager extends BaseUpdateStatusManager<TransactionEntity> {
  protected payloadBody: any;

  setCustomBodyRequest(body) {
    this.payloadBody = body;
  }

  getResult(): string {
    return `Success active data ${this.result.id}`;
  }

  async validateProcess(): Promise<void> {
    // untuk dapat current status
    const transaction = await this.dataService.getOneByOptions({
      where: {
        id: this.dataId,
      },
    });

    if (
      ![STATUS.SETTLED, STATUS.WAITING].includes(transaction.status) &&
      !this.data.is_recap_transaction
    ) {
      throw new UnprocessableEntityException({
        statusCode: HttpStatus.UNPROCESSABLE_ENTITY,
        message: `Gagal! tidak bisa batalkan, karena status transaksi tidak settled atau waiting`,
        error: 'Unprocessable Entity',
      });
    } else if (this.data.is_recap_transaction) {
      // FIXME => VALIDATION GUARD CANCEL FOR RECONCILIATION FROM CASHIER
      // throw new UnprocessableEntityException({
      //   statusCode: HttpStatus.UNPROCESSABLE_ENTITY,
      //   message: `Gagagl! tidak dapat batalkan data rekap`,
      //   error: 'Unprocessable Entity',
      // });
    }
    return;
  }

  async beforeProcess(): Promise<void> {
    if (this.data.is_recap_transaction) {
      Object.assign(this.data, {
        otp_code: this.payloadBody?.otp_code,
        reconciliation_confirm_by: null,
        reconciliation_confirm_date: null,
        reconciliation_status: STATUS.PENDING,
        payment_code: null,
        settlement_date: null,
      });
    } else {
      Object.assign(this.data, {
        otp_code: this.payloadBody?.otp_code,
        reconciliation_mdr: this.data.reconciliation_mdr ?? null,
        reconciliation_confirm_by: this.user.name,
        reconciliation_confirm_date: new Date().getTime(),
        status: this.dataStatus,
        reconciliation_status: this.dataStatus,
        payment_date: this.data.payment_date,
        settlement_date:
          this.dataStatus === STATUS.REJECTED
            ? null
            : this.data.settlement_date,
      });
    }

    return;
  }

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

  get validateRelations(): validateRelations[] {
    return [];
  }

  get entityTarget(): any {
    return TransactionModel;
  }

  get eventTopics(): EventTopics[] {
    return [
      {
        topic: TransactionChangeStatusEvent,
      },
    ];
  }
}
