package com.pandoli365.bibimbap.mapper;

import com.pandoli365.bibimbap.data.JamScoreData;
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 java.util.List;

@Mapper
public interface JamScoresMapper {

    @Insert("""
            INSERT INTO jam_scores (
                jam_id,
                game_id,
                judge_user_id,
                criterion_key,
                score
            ) VALUES (
                #{jamId},
                #{gameId},
                #{judgeUserId},
                #{criterionKey},
                #{score}
            )
            ON CONFLICT (jam_id, game_id, judge_user_id, criterion_key)
            DO UPDATE SET score = EXCLUDED.score, updated_at = now()
            """)
    int upsertScore(@Param("jamId") long jamId,
                    @Param("gameId") long gameId,
                    @Param("judgeUserId") long judgeUserId,
                    @Param("criterionKey") String criterionKey,
                    @Param("score") int score);

    @Select("""
            SELECT
                criterion_key AS criterionKey,
                score,
                judge_user_id AS judgeUserId,
                updated_at AS updatedAt
            FROM jam_scores
            WHERE jam_id = #{jamId}
              AND game_id = #{gameId}
              AND judge_user_id = #{judgeUserId}
            ORDER BY criterion_key
            """)
    List<JamScoreData> listByJudge(@Param("jamId") long jamId,
                                   @Param("gameId") long gameId,
                                   @Param("judgeUserId") long judgeUserId);
}
