package com.pandoli365.bibimbap.mapper;

import com.pandoli365.bibimbap.data.JamJudgeData;
import org.apache.ibatis.annotations.Delete;
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 JamJudgesMapper {

    @Insert("""
            INSERT INTO jam_judges (
                jam_id,
                user_id,
                assigned_by
            ) VALUES (
                #{jamId},
                #{userId},
                #{assignedBy}
            )
            """)
    int insert(@Param("jamId") long jamId, @Param("userId") long userId, @Param("assignedBy") long assignedBy);

    @Delete("""
            DELETE FROM jam_judges
            WHERE jam_id = #{jamId}
              AND user_id = #{userId}
            """)
    int delete(@Param("jamId") long jamId, @Param("userId") long userId);

    @Select("""
            SELECT EXISTS(
                SELECT 1
                FROM jam_judges
                WHERE jam_id = #{jamId}
                  AND user_id = #{userId}
            )
            """)
    boolean exists(@Param("jamId") long jamId, @Param("userId") long userId);

    @Select("""
            SELECT
                jj.id,
                jj.jam_id AS jamId,
                jj.user_id AS userId,
                jj.assigned_by AS assignedBy,
                jj.created_at AS createdAt,
                u.display_name AS displayName
            FROM jam_judges jj
            JOIN users u ON u.id = jj.user_id AND u.is_delete IS NOT TRUE
            WHERE jj.jam_id = #{jamId}
            ORDER BY jj.created_at, jj.id
            """)
    List<JamJudgeData> listByJam(long jamId);
}
