package com.pandoli365.bibimbap.mapper;

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

@Mapper
public interface GameCommentsMapper {

    @Select("""
            SELECT
                id,
                game_id AS gameId,
                nickname,
                content,
                created_at AS createdAt,
                deleted_at AS deletedAt
            FROM game_comments
            WHERE id = #{id}
              AND is_delete IS NOT TRUE
            """)
    GameCommentData getGameComment(long id);

    @Insert("""
            INSERT INTO game_comments (
                game_id,
                nickname,
                content
            ) VALUES (
                #{gameId},
                #{nickname},
                #{content}
            )
            """)
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int addGameComment(GameCommentData gameComment);

    @Update("""
            UPDATE game_comments
            SET
                nickname = #{nickname},
                content = #{content},
                deleted_at = #{deletedAt},
                is_delete = CASE WHEN #{deletedAt} IS NULL THEN false ELSE true END
            WHERE id = #{id}
              AND is_delete IS NOT TRUE
            """)
    int updateGameComment(GameCommentData gameComment);
}
