<?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.AiAgentMapper">

  <resultMap id="AiAgentMap" type="com.erp.ai.model.AiAgent">
    <id property="id" column="id"/>
    <result property="agent_id" column="agent_id"/>
    <result property="name" column="name"/>
    <result property="description" column="description"/>
    <result property="model" column="model"/>
    <result property="system_prompt" column="system_prompt"/>
    <result property="tools" column="tools"/>
    <result property="config" column="config"/>
    <result property="status" column="status"/>
    <result property="company_code" column="company_code"/>
    <result property="created_by" column="created_by"/>
    <result property="created_at" column="created_at"/>
    <result property="updated_at" column="updated_at"/>
  </resultMap>

  <select id="list" parameterType="map" resultMap="AiAgentMap">
    SELECT id, agent_id, name, description, model, system_prompt,
           tools::text AS tools, config::text AS config, status, company_code, created_by, created_at, updated_at
    FROM ai_agents
    WHERE 1=1
    <choose>
      <when test="status != null">
        AND status = #{status}
      </when>
      <otherwise>
        AND status != 'archived'
      </otherwise>
    </choose>
    <if test="company_code != null">
      AND (company_code = #{company_code} OR company_code IS NULL OR company_code = '*')
    </if>
    <if test="search != null">
      AND (name ILIKE #{search} OR agent_id ILIKE #{search} OR description ILIKE #{search})
    </if>
    ORDER BY created_at DESC
  </select>

  <select id="getById" resultMap="AiAgentMap">
    SELECT id, agent_id, name, description, model, system_prompt,
           tools::text AS tools, config::text AS config, status, company_code, created_by, created_at, updated_at
    FROM ai_agents
    WHERE id = #{id}
  </select>

  <select id="getByAgentId" resultMap="AiAgentMap">
    SELECT id, agent_id, name, description, model, system_prompt,
           tools::text AS tools, config::text AS config, status, company_code, created_by, created_at, updated_at
    FROM ai_agents
    WHERE agent_id = #{agentId}
  </select>

  <insert id="insert" parameterType="com.erp.ai.model.AiAgent" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO ai_agents (agent_id, name, description, model, system_prompt, tools, config, status, company_code, created_by)
    VALUES (#{agent_id}, #{name}, #{description},
            COALESCE(#{model}, 'claude-sonnet-4-20250514'),
            #{system_prompt},
            COALESCE(#{tools}::jsonb, '[]'::jsonb),
            COALESCE(#{config}::jsonb, '{}'::jsonb),
            COALESCE(#{status}, 'active'),
            #{company_code}, #{created_by})
  </insert>

  <update id="update">
    UPDATE ai_agents
    <set>
      <if test="fields.name != null">name = #{fields.name},</if>
      <if test="fields.description != null">description = #{fields.description},</if>
      <if test="fields.model != null">model = #{fields.model},</if>
      <if test="fields.system_prompt != null">system_prompt = #{fields.system_prompt},</if>
      <if test="fields.tools != null">tools = #{fields.tools}::jsonb,</if>
      <if test="fields.config != null">config = #{fields.config}::jsonb,</if>
      <if test="fields.status != null">status = #{fields.status},</if>
      updated_at = NOW()
    </set>
    WHERE id = #{id}
  </update>

  <update id="softDelete">
    UPDATE ai_agents SET status = 'archived', updated_at = NOW() WHERE id = #{id}
  </update>

  <select id="countActive" resultType="int">
    SELECT COUNT(*) FROM ai_agents WHERE status = 'active'
  </select>

  <select id="getActiveAgents" resultMap="AiAgentMap">
    SELECT id, agent_id, name, description, model, system_prompt,
           tools::text AS tools, config::text AS config, status, company_code, created_by, created_at, updated_at
    FROM ai_agents WHERE status = 'active' ORDER BY name
  </select>

</mapper>
