package com.magistr.app.repository;

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

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

public interface AcademicCalendarDayRepository extends JpaRepository<AcademicCalendarDay, Long> {

    @Query("""
            select day
            from AcademicCalendarDay day
            join fetch day.activityType
            where day.academicCalendar.id = :calendarId
            order by day.courseNumber asc, day.date asc
            """)
    List<AcademicCalendarDay> findByCalendarIdWithActivity(@Param("calendarId") Long calendarId);

    @Query("""
            select day
            from AcademicCalendarDay day
            join fetch day.activityType
            where day.academicCalendar.id = :calendarId
              and day.courseNumber = :courseNumber
              and day.date = :date
            """)
    Optional<AcademicCalendarDay> findByCalendarIdAndCourseNumberAndDate(
            @Param("calendarId") Long calendarId,
            @Param("courseNumber") Integer courseNumber,
            @Param("date") LocalDate date
    );

    boolean existsByActivityTypeId(Long activityTypeId);

    @Modifying
    @Query("delete from AcademicCalendarDay day where day.academicCalendar.id = :calendarId")
    void deleteByCalendarId(@Param("calendarId") Long calendarId);
}
