package com.pandoli365.bibimbap.mapper;

import com.pandoli365.bibimbap.data.GameAssetOwner;
import com.pandoli365.bibimbap.data.GameUploadAudit;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface GameUploadAuditMapper {

    @Insert("""
            INSERT INTO game_upload_audit_log (
                actor_id, game_uuid, outcome, reject_reason,
                original_name, upload_bytes, entry_count, extracted_bytes
            ) VALUES (
                #{actorId}, #{gameUuid}, #{outcome}, #{rejectReason},
                #{originalName}, #{uploadBytes}, #{entryCount}, #{extractedBytes}
            )
            """)
    int insertAudit(GameUploadAudit audit);

    // replaceUuid 소유검증: webgl_path 가 '/game/{uuid}/...' 형태인 게임의 (id, user_id) 조회.
    // null = 대상 없음(= 교체대상 게임 미존재). 컨트롤러가 user_id 와 세션 userId 비교.
    @Select("""
            SELECT g.id AS gameId, g.user_id AS userId
            FROM games g
            WHERE g.webgl_path LIKE CONCAT('/game/', #{gameUuid}, '/%')
              AND g.is_delete IS NOT TRUE
            ORDER BY g.id ASC
            LIMIT 1
            """)
    GameAssetOwner findGameByWebglUuid(@Param("gameUuid") String gameUuid);
}
