package com.mosquito.project.persistence.repository;

import com.mosquito.project.persistence.entity.LinkClickEntity;
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.time.OffsetDateTime;
import java.util.List;

@Repository
public interface LinkClickRepository extends JpaRepository<LinkClickEntity, Long> {

    List<LinkClickEntity> findByActivityId(Long activityId);

    List<LinkClickEntity> findByActivityIdAndCreatedAtBetween(Long activityId, OffsetDateTime startTime, OffsetDateTime endTime);

    List<LinkClickEntity> findByCreatedAtBetween(OffsetDateTime startTime, OffsetDateTime endTime);

    List<LinkClickEntity> findByCode(String code);

    @Query(value = "SELECT l.code, COUNT(*) as cnt, l.inviter_user_id FROM link_clicks l " +
           "WHERE l.activity_id = :activityId " +
           "GROUP BY l.code, l.inviter_user_id " +
           "ORDER BY cnt DESC LIMIT :limit",
           nativeQuery = true)
    List<Object[]> findTopSharedLinksByActivityId(@Param("activityId") Long activityId, @Param("limit") int limit);

    @Query("SELECT COUNT(DISTINCT l.ip) FROM LinkClickEntity l " +
           "WHERE l.activityId = :activityId " +
           "AND l.createdAt BETWEEN :startTime AND :endTime")
    long countUniqueVisitorsByActivityIdAndDateRange(
        @Param("activityId") Long activityId,
        @Param("startTime") OffsetDateTime startTime,
        @Param("endTime") OffsetDateTime endTime
    );

    long countByActivityId(Long activityId);

    /**
     * 按活动ID和日期范围统计PV（总浏览次数）
     */
    @Query("SELECT COUNT(l) FROM LinkClickEntity l WHERE l.activityId = :activityId " +
           "AND l.createdAt BETWEEN :startTime AND :endTime")
    long countViewsByActivityIdAndDateRange(
        @Param("activityId") Long activityId,
        @Param("startTime") OffsetDateTime startTime,
        @Param("endTime") OffsetDateTime endTime
    );

    /**
     * 按IP去重统计UV
     */
    @Query("SELECT COUNT(DISTINCT l.ip) FROM LinkClickEntity l")
    long countUniqueVisitors();

    /**
     * 按活动ID和IP去重统计UV
     */
    @Query("SELECT COUNT(DISTINCT l.ip) FROM LinkClickEntity l WHERE l.activityId = :activityId")
    long countUniqueVisitorsByActivityId(@Param("activityId") Long activityId);

    /**
     * 统计有效分享数（有inviterUserId的记录，表示归因成功的分享点击）
     */
    @Query("SELECT COUNT(l) FROM LinkClickEntity l WHERE l.activityId = :activityId " +
           "AND l.inviterUserId IS NOT NULL " +
           "AND l.createdAt BETWEEN :startTime AND :endTime")
    long countSharesByActivityIdAndDateRange(
        @Param("activityId") Long activityId,
        @Param("startTime") OffsetDateTime startTime,
        @Param("endTime") OffsetDateTime endTime
    );

    /**
     * 统计最近时间段的独立访客数
     */
    @Query("SELECT COUNT(DISTINCT l.ip) FROM LinkClickEntity l WHERE l.createdAt >= :since")
    long countRecentVisitors(@Param("since") OffsetDateTime since);
}

