/**
 * @aurora-catalyst-generated
 * @source cliter/iam/bounded-context.aurora.yaml
 */
import { IamBoundedContext, IamCreateBoundedContextInput } from '@api/graphql';
import {
  IamCreateBoundedContextService,
  IamFindBoundedContextByIdService,
} from '@app/iam/bounded-context';
import { IamBoundedContextFieldSchema } from '@app/iam/bounded-context/domain';
import { ApplySchema, EmitEvent, HandlerMeta } from '@aurorajs.dev/core-back';
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';

@Injectable()
export class IamCreateBoundedContextHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly createService: IamCreateBoundedContextService,
    private readonly findByIdService: IamFindBoundedContextByIdService,
  ) {}

  @EmitEvent('iam.boundedContext.created')
  @ApplySchema(IamBoundedContextFieldSchema)
  async main(
    payload: IamCreateBoundedContextInput,
    handlerMeta?: HandlerMeta,
  ): Promise<IamBoundedContext> {
    await this.createService.main(payload, handlerMeta);

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