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

  @EmitEvent('oAuth.client.created')
  @ApplySchema(OAuthClientFieldSchema)
  async main(payload: OAuthCreateClientInput, handlerMeta?: HandlerMeta): Promise<OAuthClient> {
    await this.createService.main(payload, handlerMeta);

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