<?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="businessRule">

  <!-- ═══ 대시보드별 비즈니스 룰 목록 ═══ -->
  <select id="getBusinessRuleList" parameterType="map" resultType="map">
    SELECT
        RULE_ID
        , DASHBOARD_ID
        , NAME
        , DESCRIPTION
        , IS_ENABLED
        , COMPANY_CODE
        , CREATED_BY
        , CREATED_DATE
        , UPDATED_BY
        , UPDATED_DATE
    FROM BUSINESS_RULES
    WHERE IS_ACTIVE = 'Y'
    <if test='dashboard_id != null and dashboard_id != ""'>
      AND DASHBOARD_ID = #{dashboard_id}
    </if>
    <include refid="common.companyCodeFilter"/>
    <include refid="common.dynamicOrderBy"/>
    <include refid="common.pagination"/>
  </select>

  <!-- ═══ 목록 카운트 ═══ -->
  <select id="getBusinessRuleListCnt" parameterType="map" resultType="int">
    SELECT COUNT(*)
    FROM BUSINESS_RULES
    WHERE IS_ACTIVE = 'Y'
    <if test='dashboard_id != null and dashboard_id != ""'>
      AND DASHBOARD_ID = #{dashboard_id}
    </if>
    <include refid="common.companyCodeFilter"/>
  </select>

  <!-- ═══ 룰 상세 (노드 + 연결 JSONB 포함) ═══ -->
  <select id="getBusinessRuleInfo" parameterType="map" resultType="map">
    SELECT
        RULE_ID
        , DASHBOARD_ID
        , NAME
        , DESCRIPTION
        , NODES
        , CONNECTIONS
        , IS_ENABLED
        , COMPANY_CODE
        , CREATED_BY
        , CREATED_DATE
        , UPDATED_BY
        , UPDATED_DATE
    FROM BUSINESS_RULES
    WHERE RULE_ID = #{rule_id}
      AND IS_ACTIVE = 'Y'
  </select>

  <!-- ═══ 룰 등록 ═══ -->
  <insert id="insertBusinessRule" parameterType="map">
    INSERT INTO BUSINESS_RULES (
        RULE_ID
        , DASHBOARD_ID
        , NAME
        , DESCRIPTION
        , NODES
        , CONNECTIONS
        , IS_ENABLED
        , COMPANY_CODE
        , CREATED_BY
        , CREATED_DATE
    ) VALUES (
        #{rule_id}
        , #{dashboard_id}
        , #{name}
        , #{description}
        , #{nodes}::jsonb
        , #{connections}::jsonb
        , COALESCE(#{is_enabled}, true)
        , #{company_code}
        , #{user_id}
        , CURRENT_TIMESTAMP
    )
  </insert>

  <!-- ═══ 룰 수정 ═══ -->
  <update id="updateBusinessRule" parameterType="map">
    UPDATE BUSINESS_RULES
    SET
        NAME = #{name}
        , DESCRIPTION = #{description}
        , NODES = #{nodes}::jsonb
        , CONNECTIONS = #{connections}::jsonb
        , UPDATED_BY = #{user_id}
        , UPDATED_DATE = CURRENT_TIMESTAMP
    WHERE RULE_ID = #{rule_id}
      AND IS_ACTIVE = 'Y'
  </update>

  <!-- ═══ 룰 삭제 (소프트) ═══ -->
  <update id="deleteBusinessRule" parameterType="map">
    UPDATE BUSINESS_RULES
    SET IS_ACTIVE = 'D'
        , UPDATED_BY = #{user_id}
        , UPDATED_DATE = CURRENT_TIMESTAMP
    WHERE RULE_ID = #{rule_id}
  </update>

  <!-- ═══ 활성/비활성 토글 ═══ -->
  <update id="toggleBusinessRule" parameterType="map">
    UPDATE BUSINESS_RULES
    SET IS_ENABLED = NOT IS_ENABLED
        , UPDATED_BY = #{user_id}
        , UPDATED_DATE = CURRENT_TIMESTAMP
    WHERE RULE_ID = #{rule_id}
      AND IS_ACTIVE = 'Y'
  </update>

</mapper>
