import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Review } from '@/entities/review.entity';

@Injectable()
export class MerchantReviewService {
  constructor(
    @InjectRepository(Review)
    private reviewRepo: Repository<Review>,
  ) {}

  async getSellerReviews(
    merchantId: number,
    page: number = 1,
    limit: number = 10,
    roomId?: number,
  ) {
    const qb = this.reviewRepo
      .createQueryBuilder('review')
      .leftJoinAndSelect('review.user', 'user')
      .leftJoinAndSelect('review.room', 'room')
      .leftJoinAndSelect('review.order', 'order')
      .where('review.merchantId = :merchantId', { merchantId });

    if (roomId) {
      qb.andWhere('review.roomId = :roomId', { roomId });
    }

    qb.orderBy('review.createdAt', 'DESC')
      .skip((page - 1) * limit)
      .take(limit);

    const [list, total] = await qb.getManyAndCount();

    return {
      list,
      total,
      page,
      pageSize: limit,
      totalPages: Math.ceil(total / limit),
    };
  }
}
