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

  <resultMap id="ScheduleMap" type="com.erp.ai.model.AiAgentSchedule">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="group_id" column="group_id"/>
    <result property="cron_expression" column="cron_expression"/>
    <result property="timezone" column="timezone"/>
    <result property="input_message" column="input_message"/>
    <result property="notification" column="notification"/>
    <result property="is_active" column="is_active"/>
    <result property="last_run_at" column="last_run_at"/>
    <result property="run_count" column="run_count"/>
    <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="group_name" column="group_name"/>
  </resultMap>

  <sql id="ScheduleColumns">
    s.id, s.name, s.group_id, s.cron_expression, s.timezone, s.input_message,
    s.notification::text AS notification, s.is_active, s.last_run_at, s.run_count,
    s.company_code, s.created_by, s.created_at, s.updated_at,
    g.name AS group_name
  </sql>

  <select id="listAll" resultMap="ScheduleMap">
    SELECT <include refid="ScheduleColumns"/>
    FROM ai_agent_schedules s
    LEFT JOIN ai_agent_groups g ON s.group_id = g.id
    ORDER BY s.created_at DESC
  </select>

  <select id="listActive" resultMap="ScheduleMap">
    SELECT <include refid="ScheduleColumns"/>
    FROM ai_agent_schedules s
    LEFT JOIN ai_agent_groups g ON s.group_id = g.id
    WHERE s.is_active = TRUE
  </select>

  <select id="getById" resultMap="ScheduleMap">
    SELECT <include refid="ScheduleColumns"/>
    FROM ai_agent_schedules s
    LEFT JOIN ai_agent_groups g ON s.group_id = g.id
    WHERE s.id = #{id}
  </select>

  <insert id="insert" parameterType="com.erp.ai.model.AiAgentSchedule" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO ai_agent_schedules
      (name, group_id, cron_expression, timezone, input_message, notification, is_active, company_code, created_by)
    VALUES (#{name}, #{group_id}, #{cron_expression},
            COALESCE(#{timezone}, 'Asia/Seoul'),
            #{input_message},
            COALESCE(#{notification}::jsonb, '{}'::jsonb),
            COALESCE(#{is_active}, TRUE),
            #{company_code}, #{created_by})
  </insert>

  <update id="update" parameterType="com.erp.ai.model.AiAgentSchedule">
    UPDATE ai_agent_schedules
    <set>
      <if test="name != null">name = #{name},</if>
      <if test="cron_expression != null">cron_expression = #{cron_expression},</if>
      <if test="timezone != null">timezone = #{timezone},</if>
      <if test="input_message != null">input_message = #{input_message},</if>
      <if test="notification != null">notification = #{notification}::jsonb,</if>
      <if test="is_active != null">is_active = #{is_active},</if>
      updated_at = NOW()
    </set>
    WHERE id = #{id}
  </update>

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

  <update id="markRun">
    UPDATE ai_agent_schedules
    SET last_run_at = NOW(), run_count = run_count + 1
    WHERE id = #{id}
  </update>

</mapper>
