package com.emoon.tongue.example;

import com.emoon.tongue.config.TongueImageCheckProperties;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


/**
 * 配置使用示例类
 * 展示如何使用舌象图片检查规则配置
 *
 * @author emoon
 * @date 2024-12-15
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class ConfigUsageExample {

    private final TongueImageCheckProperties tongueImageCheckProperties;

    /**
     * 在组件初始化后执行，展示配置的使用方法
     */
    @PostConstruct
    public void demonstrateConfigUsage() {
        log.info("=== 舌象图片检查规则配置使用示例 ===");

        // 1. 获取所有规则
        var rules = tongueImageCheckProperties.getRules();
        log.info("共加载了{}条检查规则", rules.size());

        // 2. 遍历规则并访问不同字段
        for (int i = 0; i < rules.size(); i++) {
            TongueImageCheckProperties.CheckRule rule = rules.get(i);

            log.info("规则{}详情:", i + 1);
            log.info("  错误码: {}", rule.getErrorCode());
            log.info("  错误类型: {}", rule.getErrorType());
            log.info("  判断标准: {}", rule.getJudgeStandard());
            log.info("  优化建议: {}", rule.getOptimizeSuggestion());
            log.info("---");
        }

        // 3. 根据错误码查找规则
        findRuleByErrorCode(1001);
        findRuleByErrorCode(9999); // 不存在的错误码
    }

    /**
     * 根据错误码查找规则的示例方法
     *
     * @param errorCode 错误码
     */
    private void findRuleByErrorCode(Integer errorCode) {
        log.info("查找错误码为{}的规则", errorCode);

        TongueImageCheckProperties.CheckRule foundRule = tongueImageCheckProperties.getRules().stream()
                .filter(rule -> errorCode.equals(rule.getErrorCode()))
                .findFirst()
                .orElse(null);

        if (foundRule != null) {
            log.info("找到匹配规则:");
            log.info("  错误类型: {}", foundRule.getErrorType());
            log.info("  优化建议: {}", foundRule.getOptimizeSuggestion());
        } else {
            log.warn("未找到错误码为{}的规则", errorCode);
        }
    }
}
