package com.magistr.app.repository;

import com.magistr.app.model.StudentGroup;
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 GroupRepository extends JpaRepository<StudentGroup, Long> {

    List<StudentGroup> findByEducationFormId(Long educationFormId);

    List<StudentGroup> findByDepartmentId(Long departmentId);

    List<StudentGroup> findByStatusNot(String status);

    List<StudentGroup> findByDepartmentIdAndStatusNot(Long departmentId, String status);

    @Lock(LockModeType.PESSIMISTIC_WRITE)
    @Query("""
            select studentGroup
            from StudentGroup studentGroup
            join fetch studentGroup.educationForm
            join fetch studentGroup.speciality
            join fetch studentGroup.specialtyProfile
            where studentGroup.id = :id
            """)
    Optional<StudentGroup> findByIdForUpdate(@Param("id") Long id);
}
