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

  <sql id="externalCallConfigSearchCondition">
    <if test="call_type != null and call_type != ''">
      AND CALL_TYPE = #{call_type}
    </if>
    <if test="api_type != null and api_type != ''">
      AND API_TYPE = #{api_type}
    </if>
    <if test="is_active != null and is_active != ''">
      AND IS_ACTIVE = #{is_active}
    </if>
    <if test="search != null and search != ''">
      AND (CONFIG_NAME ILIKE '%' || #{search} || '%'
           OR DESCRIPTION ILIKE '%' || #{search} || '%')
    </if>
  </sql>

  <select id="getExternalCallConfigList" parameterType="map" resultType="map">
    SELECT
        ID
        , CONFIG_NAME
        , CALL_TYPE
        , API_TYPE
        , CONFIG_DATA::TEXT AS CONFIG_DATA
        , DESCRIPTION
        , COMPANY_CODE
        , IS_ACTIVE
        , CREATED_BY
        , UPDATED_BY
        , CREATED_DATE
        , UPDATED_DATE
    FROM EXTERNAL_CALL_CONFIGS
    WHERE 1=1
    <include refid="common.companyCodeFilter"/>
    <include refid="externalCallConfigSearchCondition"/>
    ORDER BY IS_ACTIVE DESC, CREATED_DATE DESC
    <include refid="common.pagination"/>
  </select>

  <select id="getExternalCallConfigListCnt" parameterType="map" resultType="int">
    SELECT COUNT(*)
    FROM EXTERNAL_CALL_CONFIGS
    WHERE 1=1
    <include refid="common.companyCodeFilter"/>
    <include refid="externalCallConfigSearchCondition"/>
  </select>

  <select id="getExternalCallConfigInfo" parameterType="map" resultType="map">
    SELECT
        ID
        , CONFIG_NAME
        , CALL_TYPE
        , API_TYPE
        , CONFIG_DATA::TEXT AS CONFIG_DATA
        , DESCRIPTION
        , COMPANY_CODE
        , IS_ACTIVE
        , CREATED_BY
        , UPDATED_BY
        , CREATED_DATE
        , UPDATED_DATE
    FROM EXTERNAL_CALL_CONFIGS
    WHERE ID = #{id}
  </select>

  <insert id="insertExternalCallConfig" parameterType="map" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
    INSERT INTO EXTERNAL_CALL_CONFIGS (
        CONFIG_NAME
        , CALL_TYPE
        , API_TYPE
        , CONFIG_DATA
        , DESCRIPTION
        , COMPANY_CODE
        , IS_ACTIVE
        , CREATED_BY
        , UPDATED_BY
        , CREATED_DATE
        , UPDATED_DATE
    ) VALUES (
        #{config_name}
        , #{call_type}
        , #{api_type}
        , <choose>
            <when test="config_data != null">#{config_data}::JSONB</when>
            <otherwise>'{}'::JSONB</otherwise>
          </choose>
        , #{description}
        , #{company_code}
        , <choose>
            <when test="is_active != null">#{is_active}</when>
            <otherwise>'Y'</otherwise>
          </choose>
        , #{created_by}
        , #{updated_by}
        , NOW()
        , NOW()
    )
  </insert>

  <update id="updateExternalCallConfig" parameterType="map">
    UPDATE EXTERNAL_CALL_CONFIGS
    <set>
      <if test="config_name != null">CONFIG_NAME = #{config_name},</if>
      <if test="call_type != null">CALL_TYPE = #{call_type},</if>
      <if test="api_type != null">API_TYPE = #{api_type},</if>
      <if test="config_data != null">CONFIG_DATA = #{config_data}::JSONB,</if>
      <if test="description != null">DESCRIPTION = #{description},</if>
      <if test="company_code != null">COMPANY_CODE = #{company_code},</if>
      <if test="is_active != null">IS_ACTIVE = #{is_active},</if>
      <if test="updated_by != null">UPDATED_BY = #{updated_by},</if>
      UPDATED_DATE = NOW()
    </set>
    WHERE ID = #{id}
  </update>

  <!-- 논리 삭제 (is_active = 'N') - Node.js와 동일한 패턴 -->
  <update id="deleteExternalCallConfig" parameterType="map">
    UPDATE EXTERNAL_CALL_CONFIGS
    SET IS_ACTIVE = 'N',
        <if test="updated_by != null">UPDATED_BY = #{updated_by},</if>
        UPDATED_DATE = NOW()
    WHERE ID = #{id}
    <if test="company_code != null and company_code != &quot;*&quot;">
      AND (COMPANY_CODE = #{company_code} OR COMPANY_CODE = '*')
    </if>
  </update>

</mapper>
