/**
 * @aurora-catalyst-generated
 * @source cliter/o-auth/authorization-code.aurora.yaml
 */
import { OAuthAuthorizationCode, OAuthCreateAuthorizationCodeInput } from '@api/graphql';
import {
  OAuthCreateAuthorizationCodeService,
  OAuthFindAuthorizationCodeByIdService,
} from '@app/o-auth/authorization-code';
import { OAuthAuthorizationCodeFieldSchema } from '@app/o-auth/authorization-code/domain';
import { ApplySchema, EmitEvent, HandlerMeta } from '@aurorajs.dev/core-back';
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';

@Injectable()
export class OAuthCreateAuthorizationCodeHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly createService: OAuthCreateAuthorizationCodeService,
    private readonly findByIdService: OAuthFindAuthorizationCodeByIdService,
  ) {}

  @EmitEvent('oAuth.authorizationCode.created')
  @ApplySchema(OAuthAuthorizationCodeFieldSchema)
  async main(
    payload: OAuthCreateAuthorizationCodeInput,
    handlerMeta?: HandlerMeta,
  ): Promise<OAuthAuthorizationCode> {
    await this.createService.main(payload, handlerMeta);

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