/**
 * @aurora-catalyst-generated
 * @source cliter/iam/tenant-account.aurora.yaml
 */
import { IamCreateTenantAccountInput, IamTenantAccount } from '@api/graphql';
import {
  IamCreateTenantAccountService,
  IamFindTenantAccountByIdService,
} from '@app/iam/tenant-account';
import { IamTenantAccountFieldSchema } from '@app/iam/tenant-account/domain';
import { ApplySchema, EmitEvent, HandlerMeta } from '@aurorajs.dev/core-back';
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';

@Injectable()
export class IamCreateTenantAccountHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly createService: IamCreateTenantAccountService,
    private readonly findByIdService: IamFindTenantAccountByIdService,
  ) {}

  @EmitEvent('iam.tenantAccount.created')
  @ApplySchema(IamTenantAccountFieldSchema)
  async main(
    payload: IamCreateTenantAccountInput,
    handlerMeta?: HandlerMeta,
  ): Promise<IamTenantAccount> {
    await this.createService.main(payload, handlerMeta);

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