/**
 * @aurora-catalyst-generated
 * @source cliter/iam/permission-role.aurora.yaml
 */
import { IamCreatePermissionRoleInput, IamPermissionRole } from '@api/graphql';
import {
  IamCreatePermissionRoleService,
  IamFindPermissionRoleByIdService,
} from '@app/iam/permission-role';
import { IamPermissionRoleFieldSchema } from '@app/iam/permission-role/domain';
import { ApplySchema, EmitEvent, HandlerMeta } from '@aurorajs.dev/core-back';
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';

@Injectable()
export class IamCreatePermissionRoleHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly createService: IamCreatePermissionRoleService,
    private readonly findByIdService: IamFindPermissionRoleByIdService,
  ) {}

  @EmitEvent('iam.permissionRole.created')
  @ApplySchema(IamPermissionRoleFieldSchema)
  async main(
    payload: IamCreatePermissionRoleInput,
    handlerMeta?: HandlerMeta,
  ): Promise<IamPermissionRole> {
    await this.createService.main(payload, handlerMeta);

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