package com.common.mapper;


import com.common.entity.RuleHitTimeDTO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface NormalizeRuleStatsMapper {

    @Select("SELECT normalize_rule_id AS normalizeRuleId, " +
            "       MAX(log_time) AS maxLogTime " +
            "FROM syslog_normal_data " +
            "WHERE created_at >= #{startDate} " +
            "GROUP BY normalize_rule_id")
    List<RuleHitTimeDTO> selectMaxHitTimeFromNormalData(
            @Param("startDate") LocalDateTime startDate
    );

    @Select("SELECT normalize_rule_id AS normalizeRuleId, " +
            "       MAX(log_time) AS maxLogTime " +
            "FROM syslog_normal_alarm " +
            "WHERE created_at >= #{startDate} " +
            "GROUP BY normalize_rule_id")
    List<RuleHitTimeDTO> selectMaxHitTimeFromNormalAlarm(
            @Param("startDate") LocalDateTime startDate
    );

    @Select("SELECT id FROM dm_normalize_rule " +
            "WHERE del_flag = '0' " +
            "ORDER BY id")
    List<Long> selectActiveRuleIds();

    @Update("UPDATE dm_normalize_rule " +
            "SET updated_at = NOW(), " +
            "    first_data_saved_at = #{hitTime} " +
            "WHERE id = #{ruleId} " +
            "  AND (first_data_saved_at IS NULL OR first_data_saved_at < #{hitTime})")
    int updateRuleHitTime(
            @Param("ruleId") Long ruleId,
            @Param("hitTime") LocalDateTime hitTime
    );
}