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

  <resultMap id="AiAgentGroupMap" type="com.erp.ai.model.AiAgentGroup">
    <id property="id" column="id"/>
    <result property="group_id" column="group_id"/>
    <result property="name" column="name"/>
    <result property="description" column="description"/>
    <result property="execution_mode" column="execution_mode"/>
    <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"/>
    <result property="member_count" column="member_count"/>
  </resultMap>

  <select id="list" resultMap="AiAgentGroupMap">
    SELECT g.id, g.group_id, g.name, g.description, g.execution_mode, g.status,
           g.company_code, g.created_by, g.created_at, g.updated_at,
           (SELECT COUNT(*) FROM ai_agent_group_members WHERE group_id = g.id) AS member_count
    FROM ai_agent_groups g
    WHERE g.status != 'archived'
    <if test="companyCode != null">
      AND (g.company_code = #{companyCode} OR g.company_code IS NULL OR g.company_code = '*')
    </if>
    ORDER BY g.created_at DESC
  </select>

  <select id="getById" resultMap="AiAgentGroupMap">
    SELECT id, group_id, name, description, execution_mode, status,
           company_code, created_by, created_at, updated_at
    FROM ai_agent_groups WHERE id = #{id}
  </select>

  <select id="getByGroupId" resultMap="AiAgentGroupMap">
    SELECT id, group_id, name, description, execution_mode, status,
           company_code, created_by, created_at, updated_at
    FROM ai_agent_groups WHERE group_id = #{groupId} AND status = 'active'
  </select>

  <insert id="insert" parameterType="com.erp.ai.model.AiAgentGroup" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO ai_agent_groups (group_id, name, description, execution_mode, status, company_code, created_by)
    VALUES (#{group_id}, #{name}, #{description},
            COALESCE(#{execution_mode}, 'mixed'),
            COALESCE(#{status}, 'active'),
            #{company_code}, #{created_by})
  </insert>

  <update id="update" parameterType="com.erp.ai.model.AiAgentGroup">
    UPDATE ai_agent_groups
    <set>
      <if test="name != null">name = #{name},</if>
      <if test="description != null">description = #{description},</if>
      <if test="execution_mode != null">execution_mode = #{execution_mode},</if>
      <if test="status != null">status = #{status},</if>
      updated_at = NOW()
    </set>
    WHERE id = #{id}
  </update>

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

  <update id="touchUpdatedAt">
    UPDATE ai_agent_groups SET updated_at = NOW() WHERE id = #{id}
  </update>

</mapper>
