package com.infoepoch.pms.agent.domain.itworkbench.model;

import com.infoepoch.pms.agent.domain.itworkbench.enums.WorkbenchIntent;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 工作台智能体 - 会话短记忆
 */
public record WorkbenchConversationState(
        String sessionId,
            WorkbenchIntent lastIntent,
        Map<String, Object> conditions,
        LocalDateTime updatedAt,
        Map<String, TypeData> typeDataMap
) {
    public WorkbenchConversationState {
        conditions = sanitize(conditions);
        typeDataMap = typeDataMap == null ? new LinkedHashMap<>() : new LinkedHashMap<>(typeDataMap);
        updatedAt = updatedAt == null ? LocalDateTime.now() : updatedAt;
    }

    public WorkbenchConversationState(String sessionId) {
        this(sessionId, null, Collections.emptyMap(), LocalDateTime.now(), new LinkedHashMap<>());
    }

    public static WorkbenchConversationState initial(String sessionId, WorkbenchIntent intent, Map<String, Object> conditions) {
        return new WorkbenchConversationState(sessionId, intent, conditions, LocalDateTime.now(), new LinkedHashMap<>());
    }

    public static WorkbenchConversationState initial(String sessionId, WorkbenchIntent intent, Map<String, Object> conditions, String dataType, String dataJson) {
        WorkbenchConversationState state = new WorkbenchConversationState(sessionId, intent, conditions, LocalDateTime.now(), new LinkedHashMap<>());
        state.getTypeDataMap().put(dataType, new TypeData(dataType, intent, dataJson, Collections.emptyMap()));
        return state;
    }

    public WorkbenchConversationState update(WorkbenchIntent intent, Map<String, Object> conditions) {
        return new WorkbenchConversationState(sessionId, intent, conditions, LocalDateTime.now(), typeDataMap);
    }

    public Map<String, TypeData> getTypeDataMap() {
        return typeDataMap;
    }

    public void saveDataForType(String dataType, WorkbenchIntent intent, String dataJson, Map<String, Object> conditions) {
        typeDataMap.put(dataType, new TypeData(dataType, intent, dataJson, sanitize(conditions)));
    }

    public boolean hasDataForType(String dataType) {
        TypeData typeData = typeDataMap.get(dataType);
        return typeData != null && typeData.hasData();
    }

    public TypeData getDataForType(String dataType) {
        return typeDataMap.get(dataType);
    }

    private static Map<String, Object> sanitize(Map<String, Object> conditions) {
        if (conditions == null || conditions.isEmpty()) {
            return Collections.emptyMap();
        }
        LinkedHashMap<String, Object> sanitized = new LinkedHashMap<>();
        conditions.forEach((key, value) -> {
            if (key != null && value != null) {
                sanitized.put(key, value);
            }
        });
        return Map.copyOf(sanitized);
    }

    /**
     * 类型数据记录
     */
    public record TypeData(
            String dataType,
            WorkbenchIntent intent,
            String dataJson,
            Map<String, Object> conditions
    ) {
        public TypeData {
            conditions = conditions == null ? Collections.emptyMap() : Map.copyOf(conditions);
        }

        public boolean hasData() {
            return dataJson != null && !dataJson.isBlank();
        }
    }
}
