<?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">
<mapper namespace="buttonActionStandard">

  <sql id="selectColumns">
    action_type, action_name, action_name_eng, description, category,
    default_text, default_text_eng, default_icon, default_color, default_variant,
    confirmation_required, confirmation_message,
    validation_rules::text AS validation_rules,
    action_config::text AS action_config,
    sort_order, is_active, created_by, updated_by, created_date, updated_date
  </sql>

  <!-- 버튼 액션 목록 조회 -->
  <select id="selectButtonActions" parameterType="map" resultType="map">
    SELECT
    <include refid="selectColumns"/>

    FROM button_action_standards

    WHERE 1=1
    <if test="active != null and active != ''">
      AND is_active = #{active}
    </if>
    <if test="category != null and category != ''">
      AND category = #{category}
    </if>
    <if test="search != null and search != ''">
      AND (action_name ILIKE '%' || #{search} || '%'
        OR action_name_eng ILIKE '%' || #{search} || '%'
        OR description ILIKE '%' || #{search} || '%')
    </if>

    ORDER BY sort_order ASC, action_type ASC
  </select>

  <!-- 버튼 액션 단건 조회 -->
  <select id="selectButtonActionByType" parameterType="map" resultType="map">
    SELECT
    <include refid="selectColumns"/>

    FROM button_action_standards

    WHERE action_type = #{action_type}
    LIMIT 1
  </select>

  <!-- 버튼 액션 생성 -->
  <insert id="insertButtonAction" parameterType="map">
    INSERT INTO button_action_standards (
      action_type, action_name, action_name_eng, description, category,
      default_text, default_text_eng, default_icon, default_color, default_variant,
      confirmation_required, confirmation_message,
      validation_rules, action_config,
      sort_order, is_active, created_by, updated_by, created_date, updated_date
    ) VALUES (
      #{action_type}, #{action_name}, #{action_name_eng}, #{description}, #{category},
      #{default_text}, #{default_text_eng}, #{default_icon}, #{default_color}, #{default_variant},
      #{confirmation_required}, #{confirmation_message},
      #{validation_rules,jdbcType=OTHER}::jsonb,
      #{action_config,jdbcType=OTHER}::jsonb,
      #{sort_order}, #{is_active}, #{created_by}, #{updated_by}, NOW(), NOW()
    )
  </insert>

  <!-- 버튼 액션 수정 (동적 SET) -->
  <update id="updateButtonAction" parameterType="map">
    UPDATE button_action_standards
    <set>
      updated_date = NOW(),
      <if test="action_name != null">action_name = #{action_name},</if>
      <if test="action_name_eng != null">action_name_eng = #{action_name_eng},</if>
      <if test="description != null">description = #{description},</if>
      <if test="category != null">category = #{category},</if>
      <if test="default_text != null">default_text = #{default_text},</if>
      <if test="default_text_eng != null">default_text_eng = #{default_text_eng},</if>
      <if test="default_icon != null">default_icon = #{default_icon},</if>
      <if test="default_color != null">default_color = #{default_color},</if>
      <if test="default_variant != null">default_variant = #{default_variant},</if>
      <if test="confirmation_required != null">confirmation_required = #{confirmation_required},</if>
      <if test="confirmation_message != null">confirmation_message = #{confirmation_message},</if>
      <if test="validation_rules != null">validation_rules = #{validation_rules,jdbcType=OTHER}::jsonb,</if>
      <if test="action_config != null">action_config = #{action_config,jdbcType=OTHER}::jsonb,</if>
      <if test="sort_order != null">sort_order = #{sort_order},</if>
      <if test="is_active != null">is_active = #{is_active},</if>
      <if test="updated_by != null">updated_by = #{updated_by},</if>
    </set>

    WHERE action_type = #{action_type}
  </update>

  <!-- 버튼 액션 삭제 -->
  <delete id="deleteButtonAction" parameterType="map">
    DELETE FROM button_action_standards

    WHERE action_type = #{action_type}
  </delete>

  <!-- 정렬 순서 개별 업데이트 -->
  <update id="updateSortOrderItem" parameterType="map">
    UPDATE button_action_standards
    SET sort_order = #{sort_order},
        updated_by = #{updated_by},
        updated_date = NOW()

    WHERE action_type = #{action_type}
  </update>

  <!-- 카테고리별 카운트 조회 -->
  <select id="selectCategories" parameterType="map" resultType="map">
    SELECT category, COUNT(*) AS count

    FROM button_action_standards

    WHERE is_active = 'Y'

    GROUP BY category

    ORDER BY category ASC
  </select>

</mapper>
