/**
 * @aurora-catalyst-generated
 * @source cliter/o-auth/application-client.aurora.yaml
 */
import { OAuthApplicationClient, OAuthCreateApplicationClientInput } from '@api/graphql';
import {
  OAuthCreateApplicationClientService,
  OAuthFindApplicationClientByIdService,
} from '@app/o-auth/application-client';
import { OAuthApplicationClientFieldSchema } from '@app/o-auth/application-client/domain';
import { ApplySchema, EmitEvent, HandlerMeta } from '@aurorajs.dev/core-back';
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';

@Injectable()
export class OAuthCreateApplicationClientHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly createService: OAuthCreateApplicationClientService,
    private readonly findByIdService: OAuthFindApplicationClientByIdService,
  ) {}

  @EmitEvent('oAuth.applicationClient.created')
  @ApplySchema(OAuthApplicationClientFieldSchema)
  async main(
    payload: OAuthCreateApplicationClientInput,
    handlerMeta?: HandlerMeta,
  ): Promise<OAuthApplicationClient> {
    await this.createService.main(payload, handlerMeta);

    return await this.findByIdService.main(
      payload.applicationId,
      payload.clientId,
      {},
      handlerMeta,
    );
  }
}
