package com.sino.mci.inbound.engine;

import com.sino.mci.inbound.entity.MessageEvent;
import com.sino.mci.inbound.intent.IntentResult;
import com.sino.mci.shared.util.JsonUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class MockIntentRecognitionService implements IntentRecognitionService {

    private static final Pattern CUSTOMER_NAME_PATTERN = Pattern.compile("客户\\s*[:：]?\\s*([^\\s,，]+)");
    private static final Pattern ADDRESS_PATTERN = Pattern.compile("(?:地址|在)\\s*[:：]?\\s*([^\\s,，]+)");
    private static final Pattern PHONE_PATTERN = Pattern.compile("(1[3-9]\\d{9})");

    @Override
    public IntentResult recognize(MessageEvent event) {
        String text = extractText(event);
        if (text == null || text.isBlank()) {
            return unknownResult();
        }
        String lower = text.toLowerCase();
        if (lower.contains("救援") || lower.contains("拖车") || lower.contains("故障")) {
            Map<String, String> slots = extractSlots(text);
            List<String> missing = resolveMissingSlots(List.of("location", "contact_phone"), slots);
            return new IntentResult("rescue_request", 0.95, slots, missing, !missing.isEmpty());
        }
        if (lower.contains("咨询") || lower.contains("问一下")) {
            return new IntentResult("consult", 0.85, Map.of(), List.of(), false);
        }
        if (lower.contains("投诉") || lower.contains("不满")) {
            return new IntentResult("complaint", 0.88, Map.of(), List.of(), false);
        }
        return new IntentResult("other", 0.6, Map.of(), List.of(), false);
    }

    private String extractText(MessageEvent event) {
        String contentJson = event.getContent();
        if (contentJson == null) {
            return null;
        }
        Map<String, Object> content = JsonUtils.fromJson(contentJson, Map.class);
        if (content == null) {
            return null;
        }
        Object body = content.get("body");
        return body != null ? body.toString() : null;
    }

    private Map<String, String> extractSlots(String text) {
        Map<String, String> slots = new HashMap<>();

        Matcher customerMatcher = CUSTOMER_NAME_PATTERN.matcher(text);
        if (customerMatcher.find()) {
            slots.put("customer_name", customerMatcher.group(1).trim());
        }

        Matcher addressMatcher = ADDRESS_PATTERN.matcher(text);
        if (addressMatcher.find()) {
            slots.put("address", addressMatcher.group(1).trim());
        }

        Matcher phoneMatcher = PHONE_PATTERN.matcher(text);
        if (phoneMatcher.find()) {
            slots.put("phone", phoneMatcher.group(1).trim());
        }

        return slots;
    }

    private List<String> resolveMissingSlots(List<String> expectedSlots, Map<String, String> slots) {
        List<String> missing = new ArrayList<>();
        for (String slot : expectedSlots) {
            if (!slots.containsKey(slot) || slots.get(slot) == null || slots.get(slot).isBlank()) {
                missing.add(slot);
            }
        }
        return missing;
    }

    private IntentResult unknownResult() {
        return new IntentResult("unknown", 0.0, Map.of(), List.of(), false);
    }
}
