/**
 * @aurora-catalyst-generated
 * @source cliter/o-auth/client.aurora.yaml
 */
import { OAuthClient } from '@api/graphql';
import { OAuthDeleteClientByIdService, OAuthFindClientByIdService } from '@app/o-auth/client';
import { OAuthClientFieldSchema } from '@app/o-auth/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 OAuthDeleteClientByIdHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly deleteByIdService: OAuthDeleteClientByIdService,
    private readonly findByIdService: OAuthFindClientByIdService,
  ) {}

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

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

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

    return client;
  }
}
