package com.magistr.app.repository;

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

import java.util.List;

public interface ScheduleRuleRepository extends JpaRepository<ScheduleRule, Long> {

    @Query("""
            select distinct r
            from ScheduleRule r
            join r.groups g
            left join fetch r.subject
            left join fetch r.semester sem
            left join fetch sem.academicYear
            left join fetch r.groups groups
            left join fetch r.slots slots
            left join fetch slots.timeSlot
            left join fetch slots.subgroups slotSubgroups
            left join fetch slotSubgroups.studentGroup
            left join fetch slots.teacher
            left join fetch slots.classroom
            left join fetch slots.lessonType
            where g.id = :groupId
              and sem.id = :semesterId
              and r.status <> 'ARCHIVED'
            """)
    List<ScheduleRule> findByGroupIdAndSemesterId(
            @Param("groupId") Long groupId,
            @Param("semesterId") Long semesterId
    );

    @Query("""
            select distinct r
            from ScheduleRule r
            join r.slots teacherSlot
            left join fetch r.subject
            left join fetch r.semester sem
            left join fetch sem.academicYear
            left join fetch r.groups groups
            left join fetch r.slots slots
            left join fetch slots.timeSlot
            left join fetch slots.subgroups slotSubgroups
            left join fetch slotSubgroups.studentGroup
            left join fetch slots.teacher
            left join fetch slots.classroom
            left join fetch slots.lessonType
            where teacherSlot.teacher.id = :teacherId
              and sem.id = :semesterId
              and r.status <> 'ARCHIVED'
            """)
    List<ScheduleRule> findByTeacherIdAndSemesterId(
            @Param("teacherId") Long teacherId,
            @Param("semesterId") Long semesterId
    );

    @Query("""
            select distinct r
            from ScheduleRule r
            left join fetch r.subject
            left join fetch r.semester sem
            left join fetch sem.academicYear
            left join fetch r.groups groups
            left join fetch r.slots slots
            left join fetch slots.timeSlot
            left join fetch slots.subgroups slotSubgroups
            left join fetch slotSubgroups.studentGroup
            left join fetch slots.teacher
            left join fetch slots.classroom
            left join fetch slots.lessonType
            where r.status <> 'ARCHIVED'
            order by r.id desc
            """)
    List<ScheduleRule> findAllWithDetails();

    @Query("""
            select distinct r
            from ScheduleRule r
            left join fetch r.subject
            left join fetch r.semester sem
            left join fetch sem.academicYear
            left join fetch r.groups groups
            left join fetch r.slots slots
            left join fetch slots.timeSlot
            left join fetch slots.subgroups slotSubgroups
            left join fetch slotSubgroups.studentGroup
            left join fetch slots.teacher
            left join fetch slots.classroom
            left join fetch slots.lessonType
            where r.id = :id
            """)
    java.util.Optional<ScheduleRule> findByIdWithDetails(@Param("id") Long id);
}
