/**
 * @aurora-catalyst-generated
 * @source cliter/o-auth/authorization-code.aurora.yaml
 */
import { OAuthAuthorizationCode } from '@api/graphql';
import {
  OAuthDeleteAuthorizationCodeByIdService,
  OAuthFindAuthorizationCodeByIdService,
} from '@app/o-auth/authorization-code';
import { OAuthAuthorizationCodeFieldSchema } from '@app/o-auth/authorization-code/domain';
import { EmitEvent, Format, HandlerMeta } from '@aurorajs.dev/core-back';
import { QueryStatement } from '@aurorajs.dev/core-common';
import { Injectable, NotFoundException } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';

@Injectable()
export class OAuthDeleteAuthorizationCodeByIdHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly deleteByIdService: OAuthDeleteAuthorizationCodeByIdService,
    private readonly findByIdService: OAuthFindAuthorizationCodeByIdService,
  ) {}

  @EmitEvent('oAuth.authorizationCode.deleted')
  @Format(OAuthAuthorizationCodeFieldSchema)
  async main(
    id: string,
    constraint?: QueryStatement,
    handlerMeta?: HandlerMeta,
  ): Promise<OAuthAuthorizationCode> {
    const authorizationCode = await this.findByIdService.main(id, constraint, handlerMeta);

    if (!authorizationCode) {
      throw new NotFoundException(`OAuthAuthorizationCode with id: ${id}, not found`);
    }

    await this.deleteByIdService.main(id, constraint, handlerMeta);

    return authorizationCode;
  }
}
