<?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="com.erp.ai.mapper.AiLlmProviderMapper">

  <resultMap id="ProviderMap" type="com.erp.ai.model.AiLlmProvider">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="display_name" column="display_name"/>
    <result property="api_key_encrypted" column="api_key_encrypted"/>
    <result property="model_name" column="model_name"/>
    <result property="endpoint" column="endpoint"/>
    <result property="priority" column="priority"/>
    <result property="max_tokens" column="max_tokens"/>
    <result property="temperature" column="temperature"/>
    <result property="cost_per_1k_input" column="cost_per_1k_input"/>
    <result property="cost_per_1k_output" column="cost_per_1k_output"/>
    <result property="is_active" column="is_active"/>
    <result property="config" column="config"/>
    <result property="created_at" column="created_at"/>
    <result property="updated_at" column="updated_at"/>
  </resultMap>

  <sql id="ProviderColumns">
    id, name, display_name, api_key_encrypted, model_name, endpoint, priority,
    max_tokens, temperature, cost_per_1k_input, cost_per_1k_output, is_active,
    config::text AS config, created_at, updated_at
  </sql>

  <select id="listAll" resultMap="ProviderMap">
    SELECT <include refid="ProviderColumns"/>
    FROM ai_llm_providers ORDER BY priority, name
  </select>

  <select id="listActive" resultMap="ProviderMap">
    SELECT <include refid="ProviderColumns"/>
    FROM ai_llm_providers WHERE is_active = TRUE ORDER BY priority
  </select>

  <select id="getById" resultMap="ProviderMap">
    SELECT <include refid="ProviderColumns"/>
    FROM ai_llm_providers WHERE id = #{id}
  </select>

  <select id="getByName" resultMap="ProviderMap">
    SELECT <include refid="ProviderColumns"/>
    FROM ai_llm_providers WHERE name = #{name}
  </select>

  <insert id="insert" parameterType="com.erp.ai.model.AiLlmProvider" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO ai_llm_providers
      (name, display_name, api_key_encrypted, model_name, endpoint, priority,
       max_tokens, temperature, cost_per_1k_input, cost_per_1k_output, is_active, config)
    VALUES (#{name}, #{display_name}, #{api_key_encrypted}, #{model_name}, #{endpoint},
            COALESCE(#{priority}, 1),
            COALESCE(#{max_tokens}, 4096),
            COALESCE(#{temperature}, 0.70),
            COALESCE(#{cost_per_1k_input}, 0),
            COALESCE(#{cost_per_1k_output}, 0),
            COALESCE(#{is_active}, TRUE),
            COALESCE(#{config}::jsonb, '{}'::jsonb))
  </insert>

  <update id="update" parameterType="com.erp.ai.model.AiLlmProvider">
    UPDATE ai_llm_providers
    <set>
      <if test="display_name != null">display_name = #{display_name},</if>
      <if test="api_key_encrypted != null">api_key_encrypted = #{api_key_encrypted},</if>
      <if test="model_name != null">model_name = #{model_name},</if>
      <if test="endpoint != null">endpoint = #{endpoint},</if>
      <if test="priority != null">priority = #{priority},</if>
      <if test="max_tokens != null">max_tokens = #{max_tokens},</if>
      <if test="temperature != null">temperature = #{temperature},</if>
      <if test="cost_per_1k_input != null">cost_per_1k_input = #{cost_per_1k_input},</if>
      <if test="cost_per_1k_output != null">cost_per_1k_output = #{cost_per_1k_output},</if>
      <if test="is_active != null">is_active = #{is_active},</if>
      <if test="config != null">config = #{config}::jsonb,</if>
      updated_at = NOW()
    </set>
    WHERE id = #{id}
  </update>

  <delete id="delete">
    DELETE FROM ai_llm_providers WHERE id = #{id}
  </delete>

</mapper>
