import {
  HttpStatus,
  Injectable,
  UnprocessableEntityException,
} from '@nestjs/common';
import { BaseUpdateStatusManager } from 'src/core/modules/domain/usecase/managers/base-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 { STATUS } from 'src/core/strings/constants/base.constants';

@Injectable()
export class CancelTransactionManager extends BaseUpdateStatusManager<TransactionEntity> {
  getResult(): string {
    return `Success ${this.dataStatus} data ${this.result.invoice_code}`;
  }

  async validateProcess(): Promise<void> {
    if (![STATUS.EXPIRED, STATUS.PENDING].includes(this.oldData.status)) {
      throw new UnprocessableEntityException({
        statusCode: HttpStatus.UNPROCESSABLE_ENTITY,
        message: `Gagal! hanya tranksaksi dengan status ${STATUS.PENDING} dan ${STATUS.EXPIRED} yang dapat di${this.dataStatus}`,
        error: 'Unprocessable Entity',
      });
    }
    return;
  }

  async beforeProcess(): Promise<void> {
    Object.assign(this.data, {
      status: STATUS.CANCEL,
    });
    return;
  }

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

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

  get entityTarget(): any {
    return TransactionModel;
  }

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