package com.pandoli365.bibimbap.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface GameViewsMapper {

    @Select("""
            SELECT EXISTS(
                SELECT 1
                FROM game_views
                WHERE game_id = #{gameId}
                  AND viewer_key = #{viewerKey}
                  AND viewed_at >= now() - interval '24 hours'
            )
            """)
    boolean existsRecentView(@Param("gameId") long gameId, @Param("viewerKey") String viewerKey);

    @Insert("""
            INSERT INTO game_views (game_id, viewer_key)
            VALUES (#{gameId}, #{viewerKey})
            """)
    void insertView(@Param("gameId") long gameId, @Param("viewerKey") String viewerKey);

    @Update("UPDATE games SET view_count = view_count + 1 WHERE id = #{gameId}")
    void incrementViewCount(long gameId);
}
