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

@Injectable()
export class CancelRefundManager extends BaseUpdateStatusManager<RefundEntity> {
  getResult(): string {
    return `Success active data ${this.result.code}`;
  }

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

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

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

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

  get entityTarget(): any {
    return RefundModel;
  }

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