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

@Injectable()
export class IamDeleteTenantsAccountsHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly deleteService: IamDeleteTenantsAccountsService,
    private readonly getService: IamGetTenantsAccountsService,
  ) {}

  @EmitEvent('iam.tenantAccount.deleted')
  @Format(IamTenantAccountFieldSchema)
  async main(
    queryStatement?: QueryStatement,
    constraint?: QueryStatement,
    handlerMeta?: HandlerMeta,
  ): Promise<IamTenantAccount[]> {
    const tenantsAccounts = await this.getService.main(queryStatement, constraint, handlerMeta);

    await this.deleteService.main(queryStatement, constraint, handlerMeta);

    return tenantsAccounts;
  }
}
