package com.mvrent.carrental.repository;

import com.mvrent.carrental.model.Booking;
import com.mvrent.carrental.model.BookingStatus;
import com.mvrent.carrental.model.CarOwnership;
import com.mvrent.carrental.model.Driver;
import com.mvrent.carrental.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import java.time.LocalDate;
import java.util.List;

public interface BookingRepository extends JpaRepository<Booking, Long>,
        JpaSpecificationExecutor<Booking> {

    List<Booking> findByUserOrderByCreatedAtDesc(User user);

    List<Booking> findByDriverOrderByCreatedAtDesc(Driver driver);

    long countByDriver(Driver driver);

    List<Booking> findByCarVendorIdOrderByCreatedAtDesc(Long vendorId);

    long countByCarVendorId(Long vendorId);

    List<Booking> findByCarVendorIdAndStatusInAndPickupDateBetweenOrderByPickupDate(
            Long vendorId, List<BookingStatus> statuses, LocalDate start, LocalDate end);

    List<Booking> findByRevenueOwnerAndStatusIn(CarOwnership owner, List<BookingStatus> statuses);

    List<Booking> findAllByOrderByCreatedAtDesc();

    boolean existsByCarIdAndStatusInAndPickupDateLessThanEqualAndReturnDateGreaterThanEqual(
            Long carId,
            List<BookingStatus> statuses,
            LocalDate returnDate,
            LocalDate pickupDate
    );

    boolean existsByDriverIdAndStatusInAndPickupDateLessThanEqualAndReturnDateGreaterThanEqual(
            Long driverId,
            List<BookingStatus> statuses,
            LocalDate returnDate,
            LocalDate pickupDate
    );
}
