package cn.novalon.gym.manage.groupcourse.dao;

import cn.novalon.gym.manage.groupcourse.entity.GroupCourseRecommendEntity;
import org.springframework.data.domain.Sort;
import org.springframework.data.r2dbc.repository.Modifying;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.r2dbc.repository.R2dbcRepository;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;

@Repository
public interface GroupCourseRecommendDao extends R2dbcRepository<GroupCourseRecommendEntity, Long> {

    Mono<GroupCourseRecommendEntity> findByIdAndDeletedAtIsNull(Long id);

    Flux<GroupCourseRecommendEntity> findAllByDeletedAtIsNull();

    Flux<GroupCourseRecommendEntity> findAllByDeletedAtIsNull(Sort sort);

    Flux<GroupCourseRecommendEntity> findByCourseIdAndDeletedAtIsNull(Long courseId);

    Mono<GroupCourseRecommendEntity> findByCourseIdAndDeletedAtIsNullAndIsActiveTrue(Long courseId);

    Flux<GroupCourseRecommendEntity> findByIsActiveTrueAndDeletedAtIsNull();

    Flux<GroupCourseRecommendEntity> findByIsActiveTrueAndDeletedAtIsNull(Sort sort);

    @Modifying
    @Query("UPDATE group_course_recommend SET is_active = :isActive, updated_at = :updatedAt WHERE id = :id AND deleted_at IS NULL")
    Mono<Integer> updateActiveStatus(Long id, Boolean isActive, LocalDateTime updatedAt);

    @Modifying
    @Query("UPDATE group_course_recommend SET deleted_at = :deletedAt WHERE id = :id")
    Mono<Integer> softDelete(Long id, LocalDateTime deletedAt);

    @Modifying
    @Query("UPDATE group_course_recommend SET deleted_at = :deletedAt WHERE course_id = :courseId")
    Mono<Integer> softDeleteByCourseId(Long courseId, LocalDateTime deletedAt);
}