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

  <resultMap id="UsageLogMap" type="com.erp.ai.model.AiAgentUsageLog">
    <id property="id" column="id"/>
    <result property="user_id" column="user_id"/>
    <result property="api_key_id" column="api_key_id"/>
    <result property="agent_id" column="agent_id"/>
    <result property="conversation_id" column="conversation_id"/>
    <result property="provider_name" column="provider_name"/>
    <result property="model_name" column="model_name"/>
    <result property="prompt_tokens" column="prompt_tokens"/>
    <result property="completion_tokens" column="completion_tokens"/>
    <result property="total_tokens" column="total_tokens"/>
    <result property="cost_usd" column="cost_usd"/>
    <result property="response_time_ms" column="response_time_ms"/>
    <result property="success" column="success"/>
    <result property="error_message" column="error_message"/>
    <result property="request_path" column="request_path"/>
    <result property="ip_address" column="ip_address"/>
    <result property="company_code" column="company_code"/>
    <result property="created_at" column="created_at"/>
  </resultMap>

  <insert id="insert" parameterType="com.erp.ai.model.AiAgentUsageLog">
    INSERT INTO ai_agent_usage_logs
      (user_id, api_key_id, agent_id, conversation_id, provider_name, model_name,
       prompt_tokens, completion_tokens, total_tokens, cost_usd, response_time_ms,
       success, error_message, request_path, ip_address, company_code)
    VALUES (#{user_id}, #{api_key_id}, #{agent_id}, #{conversation_id},
            #{provider_name}, #{model_name},
            COALESCE(#{prompt_tokens}, 0),
            COALESCE(#{completion_tokens}, 0),
            COALESCE(#{total_tokens}, 0),
            COALESCE(#{cost_usd}, 0),
            #{response_time_ms},
            COALESCE(#{success}, TRUE),
            #{error_message}, #{request_path},
            <choose>
              <when test="ip_address != null">#{ip_address}::inet</when>
              <otherwise>NULL</otherwise>
            </choose>,
            #{company_code})
  </insert>

  <select id="getTodayAggregate" resultType="map">
    SELECT COALESCE(SUM(total_tokens), 0) AS tokens,
           COUNT(*) AS requests,
           COALESCE(SUM(cost_usd), 0) AS cost
    FROM ai_agent_usage_logs
    WHERE created_at &gt;= CURRENT_DATE
  </select>

  <select id="getMonthAggregate" resultType="map">
    SELECT COALESCE(SUM(total_tokens), 0) AS tokens,
           COUNT(*) AS requests,
           COALESCE(SUM(cost_usd), 0) AS cost
    FROM ai_agent_usage_logs
    WHERE created_at &gt;= DATE_TRUNC('month', CURRENT_DATE)
  </select>

  <select id="list" resultMap="UsageLogMap">
    SELECT id, user_id, api_key_id, agent_id, conversation_id, provider_name, model_name,
           prompt_tokens, completion_tokens, total_tokens, cost_usd, response_time_ms,
           success, error_message, request_path, ip_address::text AS ip_address,
           company_code, created_at
    FROM ai_agent_usage_logs
    ORDER BY created_at DESC
    LIMIT #{limit} OFFSET #{offset}
  </select>

  <select id="count" resultType="int">
    SELECT COUNT(*) FROM ai_agent_usage_logs
  </select>

  <select id="getDailyUsage" resultType="map">
    SELECT TO_CHAR(DATE(created_at), 'YYYY-MM-DD') AS date,
           SUM(total_tokens) AS tokens,
           COUNT(*) AS requests,
           SUM(cost_usd) AS cost
    FROM ai_agent_usage_logs
    WHERE created_at &gt;= CURRENT_DATE - (#{days} || ' days')::interval
    GROUP BY DATE(created_at)
    ORDER BY DATE(created_at)
  </select>

  <select id="getMonthTokensByApiKey" resultType="long">
    SELECT COALESCE(SUM(total_tokens), 0)
    FROM ai_agent_usage_logs
    WHERE api_key_id = #{apiKeyId}
      AND created_at &gt;= DATE_TRUNC('month', CURRENT_DATE)
  </select>

</mapper>
