package com.magistr.app.repository;

import com.magistr.app.model.TimeSlotScope;
import jakarta.persistence.LockModeType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface TimeSlotScopeRepository extends JpaRepository<TimeSlotScope, Long> {

    List<TimeSlotScope> findAllByOrderByDisplayOrderAscNameAsc();

    Optional<TimeSlotScope> findByCode(String code);

    Optional<TimeSlotScope> findFirstByApplyModeOrderByDisplayOrderAsc(String applyMode);

    Optional<TimeSlotScope> findByApplyModeAndDayOfWeek(String applyMode, Integer dayOfWeek);

    List<TimeSlotScope> findByApplyModeOrderByDisplayOrderAsc(String applyMode);

    @Lock(LockModeType.PESSIMISTIC_WRITE)
    @Query("select scope from TimeSlotScope scope where scope.id = :id")
    Optional<TimeSlotScope> findByIdForUpdate(@Param("id") Long id);
}
