package com.mosquito.project.persistence.repository;

import com.mosquito.project.persistence.entity.UserRewardEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserRewardRepository extends JpaRepository<UserRewardEntity, Long> {
    List<UserRewardEntity> findByActivityIdAndUserIdOrderByCreatedAtDesc(Long activityId, Long userId);

    List<UserRewardEntity> findByActivityId(Long activityId);

    Page<UserRewardEntity> findByActivityId(Long activityId, Pageable pageable);

    Page<UserRewardEntity> findByStatus(String status, Pageable pageable);

    Page<UserRewardEntity> findByType(String type, Pageable pageable);

    @Query("SELECT COUNT(r) FROM UserRewardEntity r WHERE r.status = :status")
    long countByStatus(@Param("status") String status);

    @Query("SELECT SUM(r.points) FROM UserRewardEntity r WHERE r.status = :status")
    Long sumPointsByStatus(@Param("status") String status);

    /**
     * 根据活动ID统计已发放奖励的总积分
     * 状态从PAID改为GRANTED，与实际发放状态保持一致
     */
    @Query("SELECT COALESCE(SUM(r.points), 0) FROM UserRewardEntity r WHERE r.activityId = :activityId AND r.status = 'GRANTED'")
    Double sumPointsByActivityIdAndStatusGranted(@Param("activityId") Long activityId);

    Page<UserRewardEntity> findByUserId(Long userId, Pageable pageable);

    // ========== 非分页查询方法（用于导出）==========
    /**
     * 根据用户ID查询（用于导出）
     */
    List<UserRewardEntity> findByUserId(Long userId);

    /**
     * 根据部门ID列表查询（用于导出）
     */
    @Query("SELECT r FROM UserRewardEntity r WHERE r.departmentId IN :departmentIds")
    List<UserRewardEntity> findByDepartmentIdIn(@Param("departmentIds") List<Long> departmentIds);
    // ==============================================

    /**
     * 根据部门ID列表查询（数据权限-部门范围）
     */
    @Query("SELECT r FROM UserRewardEntity r WHERE r.departmentId IN :departmentIds")
    Page<UserRewardEntity> findByDepartmentIdIn(@Param("departmentIds") List<Long> departmentIds, Pageable pageable);

    /**
     * 根据部门ID列表和状态查询（数据权限-部门范围）
     */
    @Query("SELECT r FROM UserRewardEntity r WHERE r.departmentId IN :departmentIds AND r.status = :status")
    Page<UserRewardEntity> findByDepartmentIdInAndStatus(@Param("departmentIds") List<Long> departmentIds, @Param("status") String status, Pageable pageable);

    /**
     * 根据部门ID列表和类型查询（数据权限-部门范围）
     */
    @Query("SELECT r FROM UserRewardEntity r WHERE r.departmentId IN :departmentIds AND r.type = :type")
    Page<UserRewardEntity> findByDepartmentIdInAndType(@Param("departmentIds") List<Long> departmentIds, @Param("type") String type, Pageable pageable);

    /**
     * 根据用户ID查询（个人数据权限）
     */
    @Query("SELECT r FROM UserRewardEntity r WHERE r.userId = :userId")
    Page<UserRewardEntity> findByUserIdWithScope(@Param("userId") Long userId, Pageable pageable);

    /**
     * 根据用户ID和状态查询（个人数据权限）
     */
    @Query("SELECT r FROM UserRewardEntity r WHERE r.userId = :userId AND r.status = :status")
    Page<UserRewardEntity> findByUserIdAndStatusWithScope(@Param("userId") Long userId, @Param("status") String status, Pageable pageable);

    /**
     * 根据用户ID和类型查询（个人数据权限）
     */
    @Query("SELECT r FROM UserRewardEntity r WHERE r.userId = :userId AND r.type = :type")
    Page<UserRewardEntity> findByUserIdAndTypeWithScope(@Param("userId") Long userId, @Param("type") String type, Pageable pageable);
}

