/**
 * @aurora-catalyst-generated
 * @source cliter/o-auth/application-client.aurora.yaml
 */
import { OAuthApplicationClient } from '@api/graphql';
import {
  OAuthDeleteApplicationClientByIdService,
  OAuthFindApplicationClientByIdService,
} from '@app/o-auth/application-client';
import { OAuthApplicationClientFieldSchema } from '@app/o-auth/application-client/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 OAuthDeleteApplicationClientByIdHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly deleteByIdService: OAuthDeleteApplicationClientByIdService,
    private readonly findByIdService: OAuthFindApplicationClientByIdService,
  ) {}

  @EmitEvent('oAuth.applicationClient.deleted')
  @Format(OAuthApplicationClientFieldSchema)
  async main(
    applicationId: string,
    clientId: string,
    constraint?: QueryStatement,
    handlerMeta?: HandlerMeta,
  ): Promise<OAuthApplicationClient> {
    const applicationClient = await this.findByIdService.main(
      applicationId,
      clientId,
      constraint,
      handlerMeta,
    );

    if (!applicationClient) {
      throw new NotFoundException(
        `OAuthApplicationClient with applicationId: ${applicationId}, clientId: ${clientId}, not found`,
      );
    }

    await this.deleteByIdService.main(applicationId, clientId, constraint, handlerMeta);

    return applicationClient;
  }
}
