package com.mvrent.carrental.repository;

import com.mvrent.carrental.model.Review;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface ReviewRepository extends JpaRepository<Review, Long> {

    List<Review> findByCarIdAndApprovedTrueOrderByCreatedAtDesc(Long carId);

    List<Review> findAllByOrderByCreatedAtDesc();

    boolean existsByBookingId(Long bookingId);

    @Query("SELECT AVG(r.rating) FROM Review r WHERE r.car.id = :carId AND r.approved = true")
    Double averageForCar(@Param("carId") Long carId);

    @Query("SELECT COUNT(r) FROM Review r WHERE r.car.id = :carId AND r.approved = true")
    long countApprovedForCar(@Param("carId") Long carId);
}
