<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zioinfo.esn.rag.RagSettingMapper">

  <!-- 테넌트 격리: 모든 쿼리에 tenant_code 조건 강제 -->
  <select id="findByTenant" resultType="java.util.Map">
    SELECT setting_key AS "settingKey", setting_value AS "settingValue"
    FROM esn_rag_setting
    WHERE tenant_code = #{tenantCode}
    ORDER BY setting_key ASC
  </select>

  <select id="findValue" resultType="java.lang.String">
    SELECT setting_value
    FROM esn_rag_setting
    WHERE tenant_code = #{tenantCode} AND setting_key = #{settingKey}
  </select>

  <update id="upsert">
    INSERT INTO esn_rag_setting (tenant_code, setting_key, setting_value, updated_at)
    VALUES (#{tenantCode}, #{settingKey}, #{settingValue}, NOW())
    ON CONFLICT (tenant_code, setting_key) DO UPDATE
      SET setting_value = EXCLUDED.setting_value,
          updated_at    = NOW()
  </update>

</mapper>
