package com.huimv.employment.service.conversation;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * 本地意图分类兜底（上游智能体不可用时使用）。
 */
@Component
public class ConversationIntentClassifier {

    private static final String TYPE_EMPLOYMENT = "employment";
    private static final String TYPE_KNOWLEDGE = "knowledge";
    private static final String TYPE_ORDER = "order";
    private static final String TYPE_COST = "cost";
    private static final String TYPE_ATTENDANCE = "attendance";
    private static final String TYPE_EMERGENCY = "emergency";

    public String classify(String userMessage) {
        if (!StringUtils.hasText(userMessage)) {
            return TYPE_EMPLOYMENT;
        }
        String text = userMessage.trim();

        if (containsAny(text, "紧急", "事故", "工伤", "投诉")) {
            return TYPE_EMERGENCY;
        }
        if (containsAny(text, "考勤", "打卡", "签到", "到岗")) {
            return TYPE_ATTENDANCE;
        }
        if (containsAny(text, "订单", "进度", "流程", "到哪", "状态")) {
            return TYPE_ORDER;
        }
        if (containsAny(text, "成本", "测算", "工资", "发薪", "发工资", "多少钱", "费用", "税")) {
            return TYPE_COST;
        }
        if (containsAny(text, "政策", "合规", "怎么", "什么是", "如何", "规定")) {
            return TYPE_KNOWLEDGE;
        }
        if (containsAny(text, "招", "招聘", "搬运", "临时工", "用工", "小时工", "仓库", "人数")) {
            return TYPE_EMPLOYMENT;
        }
        return summarizeAsOther(text);
    }

    private static boolean containsAny(String text, String... keywords) {
        for (String keyword : keywords) {
            if (text.contains(keyword)) {
                return true;
            }
        }
        return false;
    }

    /** other 类型：截取 4-12 字作为自定义摘要 */
    private static String summarizeAsOther(String text) {
        String normalized = text.replaceAll("\\s+", "");
        if (normalized.length() <= 12) {
            return normalized;
        }
        return normalized.substring(0, 12);
    }
}
