package kr.co.accura.wtm.domain.timesheet.repository;

import kr.co.accura.wtm.domain.timesheet.entity.Timesheet;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDate;
import java.util.Optional;

public interface TimesheetRepository extends JpaRepository<Timesheet, Long> {

    Optional<Timesheet> findByUserIdAndWeekStartDate(Long userId, LocalDate weekStartDate);

    Page<Timesheet> findByUserId(Long userId, Pageable pageable);

    @Query("SELECT t FROM Timesheet t WHERE t.userId = :userId " +
           "AND (:from IS NULL OR t.weekStartDate >= :from) " +
           "AND (:to IS NULL OR t.weekStartDate <= :to) " +
           "ORDER BY t.weekStartDate DESC")
    Page<Timesheet> findByUserIdAndDateRange(
            @Param("userId") Long userId,
            @Param("from") LocalDate from,
            @Param("to") LocalDate to,
            Pageable pageable);
}
