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

@Injectable()
export class DeleteTransactionManager extends BaseDeleteManager<TransactionEntity> {
  getResult(): string {
    return `Success`;
  }

  async validateProcess(): Promise<void> {
    const allowDelete = [
      STATUS.DRAFT,
      STATUS.ACTIVE,
      STATUS.CANCEL,
      STATUS.PENDING,
      STATUS.EXPIRED,
    ].includes(this.data.status);

    if (!allowDelete) {
      throw new UnprocessableEntityException({
        statusCode: HttpStatus.UNPROCESSABLE_ENTITY,
        message: `Gagal! data dengan status ${this.data.status} tidak bisa di hapus!`,
        error: 'Unprocessable Entity',
      });
    }
    return;
  }

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

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

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

  get entityTarget(): any {
    return TransactionModel;
  }

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