package com.mvrent.carrental.repository;

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

import java.math.BigDecimal;
import java.util.List;

public interface PaymentRepository extends JpaRepository<Payment, Long> {

    List<Payment> findByBookingIdOrderByPaidAtDescIdDesc(Long bookingId);

    @Query("SELECT COALESCE(SUM(p.amount), 0) FROM Payment p WHERE p.booking.id = :bookingId")
    BigDecimal sumForBooking(@Param("bookingId") Long bookingId);

    @Query("SELECT p.booking.id, COALESCE(SUM(p.amount), 0) FROM Payment p GROUP BY p.booking.id")
    List<Object[]> sumGroupedByBooking();
}
