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

  <resultMap id="ConvMap" type="com.erp.ai.model.AiAgentConversation">
    <id property="id" column="id"/>
    <result property="conversation_id" column="conversation_id"/>
    <result property="agent_id" column="agent_id"/>
    <result property="user_id" column="user_id"/>
    <result property="api_key_id" column="api_key_id"/>
    <result property="title" column="title"/>
    <result property="message_count" column="message_count"/>
    <result property="total_tokens" column="total_tokens"/>
    <result property="status" column="status"/>
    <result property="metadata" column="metadata"/>
    <result property="created_at" column="created_at"/>
    <result property="updated_at" column="updated_at"/>
    <result property="agent_name" column="agent_name"/>
    <result property="display_name" column="display_name"/>
  </resultMap>

  <select id="list" resultMap="ConvMap">
    SELECT c.id, c.conversation_id, c.agent_id, c.user_id, c.api_key_id, c.title,
           c.message_count, c.total_tokens, c.status, c.metadata::text AS metadata,
           c.created_at, c.updated_at,
           a.name AS agent_name,
           COALESCE(a.name, c.metadata->>'group_name') AS display_name
    FROM ai_agent_conversations c
    LEFT JOIN ai_agents a ON c.agent_id = a.id
    <where>
      <if test="agentId != null">c.agent_id = #{agentId}</if>
    </where>
    ORDER BY c.updated_at DESC
    LIMIT #{limit} OFFSET #{offset}
  </select>

  <select id="count" resultType="int">
    SELECT COUNT(*) FROM ai_agent_conversations
    <where>
      <if test="agentId != null">agent_id = #{agentId}</if>
    </where>
  </select>

  <select id="getById" resultMap="ConvMap">
    SELECT c.id, c.conversation_id, c.agent_id, c.user_id, c.api_key_id, c.title,
           c.message_count, c.total_tokens, c.status, c.metadata::text AS metadata,
           c.created_at, c.updated_at, a.name AS agent_name
    FROM ai_agent_conversations c
    LEFT JOIN ai_agents a ON c.agent_id = a.id
    WHERE c.id = #{id}
  </select>

  <insert id="insert" parameterType="com.erp.ai.model.AiAgentConversation" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO ai_agent_conversations (conversation_id, agent_id, user_id, api_key_id, title, metadata)
    VALUES (#{conversation_id}, #{agent_id}, #{user_id}, #{api_key_id}, #{title},
            COALESCE(#{metadata}::jsonb, '{}'::jsonb))
  </insert>

  <update id="updateMeta">
    UPDATE ai_agent_conversations
    SET title = #{title}, metadata = #{metadata}::jsonb, updated_at = NOW()
    WHERE id = #{id}
  </update>

  <update id="incrementStats">
    UPDATE ai_agent_conversations
    SET message_count = message_count + 1, total_tokens = total_tokens + #{tokens}, updated_at = NOW()
    WHERE id = #{id}
  </update>

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

</mapper>
