import { Inject } from '@nestjs/common';
import { type IQueryHandler, QueryHandler } from '@nestjs/cqrs';
import { type ReviewStatsData } from '../../../domain/repositories/review-read.dto';
import { REVIEW_REPOSITORY, type IReviewRepository } from '../../../domain/repositories/review.repository';
import { GetAverageRatingQuery } from './get-average-rating.query';

@QueryHandler(GetAverageRatingQuery)
export class GetAverageRatingHandler implements IQueryHandler<GetAverageRatingQuery> {
  constructor(
    @Inject(REVIEW_REPOSITORY) private readonly reviewRepo: IReviewRepository,
  ) {}

  async execute(query: GetAverageRatingQuery): Promise<ReviewStatsData> {
    return this.reviewRepo.getStats(query.targetType, query.targetId);
  }
}
