/**
 * @aurora-catalyst-generated
 * @source cliter/iam/tenant.aurora.yaml
 */
import { IamTenant } from '@api/graphql';
import { IamDeleteTenantsService, IamGetTenantsService } from '@app/iam/tenant';
import { IamTenantFieldSchema } from '@app/iam/tenant/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 IamDeleteTenantsHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly deleteService: IamDeleteTenantsService,
    private readonly getService: IamGetTenantsService,
  ) {}

  @EmitEvent('iam.tenant.deleted')
  @Format(IamTenantFieldSchema)
  async main(
    queryStatement?: QueryStatement,
    constraint?: QueryStatement,
    handlerMeta?: HandlerMeta,
  ): Promise<IamTenant[]> {
    const tenants = await this.getService.main(queryStatement, constraint, handlerMeta);

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

    return tenants;
  }
}
