package com.pandoli365.bibimbap.mapper;

import com.pandoli365.bibimbap.data.GameLikeData;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface GameLikesMapper {

    @Select("""
            SELECT
                id,
                game_id AS gameId,
                user_key AS userKey,
                created_at AS createdAt
            FROM game_likes
            WHERE id = #{id}
            """)
    GameLikeData getGameLike(long id);

    @Insert("""
            INSERT INTO game_likes (
                game_id,
                user_key
            ) VALUES (
                #{gameId},
                #{userKey}
            )
            """)
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int addGameLike(GameLikeData gameLike);

    @Update("""
            UPDATE game_likes
            SET
                game_id = #{gameId},
                user_key = #{userKey}
            WHERE id = #{id}
            """)
    int updateGameLike(GameLikeData gameLike);

    // (game_id, user_key) 로 현재 좋아요 row 조회 — 토글 분기/초기 liked 판정에 사용
    @Select("SELECT id, game_id AS gameId, user_key AS userKey, created_at AS createdAt "
          + "FROM game_likes WHERE game_id = #{gameId} AND user_key = #{userKey}")
    GameLikeData findByGameAndUser(@Param("gameId") long gameId,
                                   @Param("userKey") String userKey);

    // 좋아요 취소 시 row 삭제 — 토글-off 경로에 사용
    @Delete("DELETE FROM game_likes WHERE game_id = #{gameId} AND user_key = #{userKey}")
    int deleteByGameAndUser(@Param("gameId") long gameId,
                            @Param("userKey") String userKey);
}
