/**
 * @aurora-catalyst-generated
 * @source cliter/o-auth/scope.aurora.yaml
 */
import { OAuthScope } from '@api/graphql';
import { OAuthDeleteScopeByIdService, OAuthFindScopeByIdService } from '@app/o-auth/scope';
import { OAuthScopeFieldSchema } from '@app/o-auth/scope/domain';
import { EmitEvent, Format, HandlerMeta } from '@aurorajs.dev/core-back';
import { QueryStatement } from '@aurorajs.dev/core-common';
import { Injectable, NotFoundException } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';

@Injectable()
export class OAuthDeleteScopeByIdHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly deleteByIdService: OAuthDeleteScopeByIdService,
    private readonly findByIdService: OAuthFindScopeByIdService,
  ) {}

  @EmitEvent('oAuth.scope.deleted')
  @Format(OAuthScopeFieldSchema)
  async main(
    id: string,
    constraint?: QueryStatement,
    handlerMeta?: HandlerMeta,
  ): Promise<OAuthScope> {
    const scope = await this.findByIdService.main(id, constraint, handlerMeta);

    if (!scope) {
      throw new NotFoundException(`OAuthScope with id: ${id}, not found`);
    }

    await this.deleteByIdService.main(id, constraint, handlerMeta);

    return scope;
  }
}
