package com.magistr.app.repository;

import com.magistr.app.model.AcademicCalendarSubject;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Collection;
import java.util.List;

public interface AcademicCalendarSubjectRepository extends JpaRepository<AcademicCalendarSubject, Long> {

    @Query("""
            select calendarSubject
            from AcademicCalendarSubject calendarSubject
            join fetch calendarSubject.academicCalendar calendar
            join fetch calendarSubject.subject subject
            where calendar.id = :calendarId
            order by calendarSubject.semesterNumber asc, lower(subject.name) asc
            """)
    List<AcademicCalendarSubject> findByCalendarIdWithSubject(@Param("calendarId") Long calendarId);

    @Query("""
            select calendarSubject
            from AcademicCalendarSubject calendarSubject
            join fetch calendarSubject.academicCalendar calendar
            join fetch calendarSubject.subject subject
            where calendar.id in :calendarIds
            order by calendar.id asc, calendarSubject.semesterNumber asc, lower(subject.name) asc
            """)
    List<AcademicCalendarSubject> findByCalendarIdInWithSubject(@Param("calendarIds") Collection<Long> calendarIds);

    void deleteByAcademicCalendar_Id(Long calendarId);

    boolean existsByAcademicCalendarIdAndSemesterNumberGreaterThan(Long calendarId, Integer semesterNumber);
}
