/**
 * @aurora-catalyst-generated
 * @source cliter/o-auth/application.aurora.yaml
 */
import { OAuthApplication, OAuthCreateApplicationInput } from '@api/graphql';
import {
  OAuthCreateApplicationService,
  OAuthFindApplicationByIdService,
} from '@app/o-auth/application';
import { OAuthApplicationFieldSchema } from '@app/o-auth/application/domain';
import { ApplySchema, EmitEvent, HandlerMeta } from '@aurorajs.dev/core-back';
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';

@Injectable()
export class OAuthCreateApplicationHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly createService: OAuthCreateApplicationService,
    private readonly findByIdService: OAuthFindApplicationByIdService,
  ) {}

  @EmitEvent('oAuth.application.created')
  @ApplySchema(OAuthApplicationFieldSchema)
  async main(
    payload: OAuthCreateApplicationInput,
    handlerMeta?: HandlerMeta,
  ): Promise<OAuthApplication> {
    await this.createService.main(payload, handlerMeta);

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