import { Inject, InternalServerErrorException } from '@nestjs/common';
import { QueryHandler, type IQueryHandler } from '@nestjs/cqrs';
import { DomainException, LoggerService } from '@modules/shared';
import { ADMIN_QUERY_REPOSITORY, type IAdminQueryRepository, type ModerationQueueResult } from '../../../domain/repositories/admin-query.repository';
import { GetModerationQueueQuery } from './get-moderation-queue.query';

@QueryHandler(GetModerationQueueQuery)
export class GetModerationQueueHandler implements IQueryHandler<GetModerationQueueQuery> {
  constructor(
    @Inject(ADMIN_QUERY_REPOSITORY) private readonly adminQueryRepo: IAdminQueryRepository,
    private readonly logger: LoggerService,
  ) {}

  async execute(query: GetModerationQueueQuery): Promise<ModerationQueueResult> {
    try {
      return await this.adminQueryRepo.getModerationQueue(query.page, query.limit);
    } catch (error) {
      if (error instanceof DomainException) throw error;
      this.logger.error(
        `Failed to get moderation queue: ${error instanceof Error ? error.message : String(error)}`,
        error instanceof Error ? error.stack : undefined,
        'GetModerationQueueHandler',
      );
      throw new InternalServerErrorException('Lỗi khi lấy danh sách duyệt');
    }
  }
}
