import { Inject } from '@nestjs/common';
import { type IQueryHandler, QueryHandler } from '@nestjs/cqrs';
import { type ReviewItemData } from '../../../domain/repositories/review-read.dto';
import { REVIEW_REPOSITORY, type IReviewRepository, type PaginatedResult } from '../../../domain/repositories/review.repository';
import { GetReviewsByTargetQuery } from './get-reviews-by-target.query';

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

  async execute(query: GetReviewsByTargetQuery): Promise<PaginatedResult<ReviewItemData>> {
    return this.reviewRepo.findByTarget(
      query.targetType,
      query.targetId,
      query.page,
      query.limit,
    );
  }
}
