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

@Injectable()
export class BatchConfirmDataTransactionManager extends BaseBatchUpdateStatusManager<TransactionEntity> {
  validateData(data: TransactionEntity): Promise<void> {
    if (
      [
        TransactionPaymentType.BANK_TRANSFER,
        TransactionPaymentType.QRIS,
      ].includes(this.data.payment_type) &&
      (!this.data.payment_date ||
        !this.data.payment_total ||
        !this.data.payment_type_method_number ||
        !this.data.payment_type_method_name)
    ) {
      throw new UnprocessableEntityException({
        statusCode: HttpStatus.UNPROCESSABLE_ENTITY,
        message: `Gagal! tolong lengkapi data terlebih dahulu`,
        error: 'Unprocessable Entity',
      });
    }

    if (
      ![STATUS.PENDING, STATUS.REJECTED, STATUS.EXPIRED].includes(data.status)
    ) {
      throw new UnprocessableEntityException({
        statusCode: HttpStatus.UNPROCESSABLE_ENTITY,
        message: `Gagal! hanya pemesanan dengan status ${STATUS.PENDING}, ${STATUS.REJECTED}, ${STATUS.EXPIRED} dapat di${this.dataStatus}`,
        error: 'Unprocessable Entity',
      });
    }

    Object.assign(data, {
      status: STATUS.WAITING,
      reconciliation_status: [
        TransactionPaymentType.COUNTER,
        TransactionPaymentType.MIDTRANS,
      ].includes(data.payment_type)
        ? null
        : STATUS.PENDING,
    });

    return;
  }

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

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

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

  get entityTarget(): any {
    return TransactionModel;
  }

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

  getResult(): BatchResult {
    return this.result;
  }
}
