<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
  RenderJobMapper — M5 렌더잡 내구 이력·쿼터 정본(Redis 는 큐/실시간). 성공(DONE) 시에만 쿼터 차감(§6-5).
  파라미터 계약: insertJob params {jobId, eventId, boothId, shotPreset, status,
                                  imageUrl?, schemaHash?, modelVersion?, errorMessage?}.
-->
<mapper namespace="com.zioinfo.kintex.module.m5.mapper.RenderJobMapper">

    <!-- 잡 생성 기록(발행 시). 재발행 안전 위해 ON CONFLICT DO NOTHING. -->
    <insert id="insertJob">
        INSERT INTO render_job (id, event_id, booth_id, shot_preset, status,
                                image_url, schema_hash, model_version, error_message, created_at, updated_at)
        VALUES (#{params.jobId}, #{params.eventId}, #{params.boothId}, #{params.shotPreset},
                COALESCE(#{params.status}, 'QUEUED'),
                #{params.imageUrl}, #{params.schemaHash}, #{params.modelVersion},
                #{params.errorMessage}, now(), now())
        ON CONFLICT (id) DO NOTHING
    </insert>

    <!-- 상태·이미지·오류 갱신(워커 콜백). -->
    <update id="updateStatus">
        UPDATE render_job
        SET status        = #{status},
            image_url     = #{imageUrl},
            error_message = #{errorMessage},
            updated_at    = now()
        WHERE id = #{jobId}
    </update>

    <!-- 부스 단위 이력 목록(SCR-12 갤러리). shotPreset=null → 전체. -->
    <select id="findByBooth" resultType="map">
        SELECT id            AS "jobId",
               booth_id      AS "boothId",
               shot_preset   AS "shotPreset",
               status,
               image_url     AS "imageUrl",
               schema_hash   AS "schemaHash",
               model_version AS "modelVersion",
               error_message AS "errorMessage",
               to_char(created_at AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"') AS "createdAt"
        FROM render_job
        WHERE booth_id = #{boothId}
        <if test="shotPreset != null">
          AND shot_preset = #{shotPreset}
        </if>
        ORDER BY created_at DESC
    </select>

    <!-- 부스 완료(DONE) 렌더 이력(SCR-06 이력 갤러리). 이미지 URL 있는 성공 결과만·최신순. -->
    <select id="findHistoryByBooth" resultType="map">
        SELECT id            AS "jobId",
               booth_id      AS "boothId",
               shot_preset   AS "shotPreset",
               status,
               image_url     AS "imageUrl",
               schema_hash   AS "schemaHash",
               model_version AS "modelVersion",
               to_char(created_at AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"') AS "createdAt"
        FROM render_job
        WHERE booth_id = #{boothId}
          AND status = 'DONE'
          AND image_url IS NOT NULL
        ORDER BY created_at DESC
    </select>

    <!-- 행사별 성공(DONE) 건수(쿼터 정본 산정). -->
    <select id="countSucceededByEvent" resultType="int">
        SELECT COUNT(*)
        FROM render_job
        WHERE event_id = #{eventId}
          AND status = 'DONE'
    </select>

</mapper>
