/**
 * @aurora-catalyst-generated
 * @source cliter/o-auth/access-token.aurora.yaml
 */
import { OAuthAccessToken } from '@api/graphql';
import {
  OAuthDeleteAccessTokenByIdService,
  OAuthFindAccessTokenByIdService,
} from '@app/o-auth/access-token';
import { OAuthAccessTokenFieldSchema } from '@app/o-auth/access-token/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 OAuthDeleteAccessTokenByIdHandler {
  constructor(
    private readonly eventEmitter: EventEmitter2,
    private readonly deleteByIdService: OAuthDeleteAccessTokenByIdService,
    private readonly findByIdService: OAuthFindAccessTokenByIdService,
  ) {}

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

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

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

    return accessToken;
  }
}
