package com.gxwebsoft.house.ai;

import cn.hutool.core.util.StrUtil;
import com.gxwebsoft.house.entity.HouseAiChatRequest;
import com.gxwebsoft.house.entity.HouseAiHouseCard;
import com.gxwebsoft.house.entity.HouseAiIntent;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * AI找房会话记忆。当前为进程内短期记忆，后续可替换为Redis或数据库适配器。
 */
@Component
public class HouseAiConversationMemory {

    private final Map<String, HouseAiIntent> intentCache = new ConcurrentHashMap<>();
    private final Map<String, List<HouseAiHouseCard>> houseCache = new ConcurrentHashMap<>();

    public void save(HouseAiChatRequest request, HouseAiIntent intent) {
        String key = buildKey(request);
        if (StrUtil.isBlank(key) || intent == null || !hasHouseCondition(intent)) {
            return;
        }
        intentCache.put(key, copy(intent));
    }

    public void clear() {
        intentCache.clear();
        houseCache.clear();
    }

    public void clear(HouseAiChatRequest request) {
        String key = buildKey(request);
        if (StrUtil.isBlank(key)) {
            return;
        }
        intentCache.remove(key);
        houseCache.remove(key);
    }

    public List<HouseAiHouseCard> getHouses(HouseAiChatRequest request) {
        String key = buildKey(request);
        List<HouseAiHouseCard> cards = StrUtil.isBlank(key) ? null : houseCache.get(key);
        return cards == null ? new ArrayList<>() : new ArrayList<>(cards);
    }

    public HouseAiIntent getIntent(HouseAiChatRequest request) {
        String key = buildKey(request);
        HouseAiIntent intent = StrUtil.isBlank(key) ? null : intentCache.get(key);
        return intent == null ? null : copy(intent);
    }

    public void saveHouses(HouseAiChatRequest request, List<HouseAiHouseCard> cards) {
        String key = buildKey(request);
        if (StrUtil.isBlank(key)) {
            return;
        }
        houseCache.put(key, cards == null ? new ArrayList<>() : new ArrayList<>(cards));
    }

    private String buildKey(HouseAiChatRequest request) {
        if (request == null || StrUtil.isBlank(request.getConversationId())) {
            return "";
        }
        return request.getUserId() + ":" + request.getConversationId();
    }

    private boolean hasHouseCondition(HouseAiIntent intent) {
        return intent.getExtentMin() != null
            || intent.getExtentMax() != null
            || intent.getFloorMin() != null
            || intent.getFloorMax() != null
            || intent.getMonthlyRentMin() != null
            || intent.getMonthlyRentMax() != null
            || intent.getSalePriceMin() != null
            || intent.getSalePriceMax() != null
            || intent.getTotalPriceMin() != null
            || intent.getTotalPriceMax() != null
            || StrUtil.isNotBlank(intent.getCityKeyword())
            || StrUtil.isNotBlank(intent.getRegionKeyword())
            || StrUtil.isNotBlank(intent.getDecorationType())
            || StrUtil.isNotBlank(intent.getSupportingKeyword())
            || StrUtil.isNotBlank(intent.getToward())
            || StrUtil.isNotBlank(intent.getHouseType())
            || intent.getAirConditioningAvailable() != null
            || intent.getParkingAvailable() != null
            || StrUtil.isNotBlank(intent.getWaterBillingType())
            || StrUtil.isNotBlank(intent.getElectricityBillingType());
    }

    private HouseAiIntent copy(HouseAiIntent source) {
        HouseAiIntent target = new HouseAiIntent();
        target.setOriginalQuestion(source.getOriginalQuestion());
        target.setIntentType(source.getIntentType());
        target.setNormalizedQuestion(source.getNormalizedQuestion());
        target.setExtentMin(source.getExtentMin());
        target.setExtentMax(source.getExtentMax());
        target.setFloorMin(source.getFloorMin());
        target.setFloorMax(source.getFloorMax());
        target.setMonthlyRentMin(source.getMonthlyRentMin());
        target.setMonthlyRentMax(source.getMonthlyRentMax());
        target.setSalePriceMin(source.getSalePriceMin());
        target.setSalePriceMax(source.getSalePriceMax());
        target.setTotalPriceMin(source.getTotalPriceMin());
        target.setTotalPriceMax(source.getTotalPriceMax());
        target.setRegionKeyword(source.getRegionKeyword());
        target.setCityKeyword(source.getCityKeyword());
        target.setTradeType(source.getTradeType());
        target.setDecorationType(source.getDecorationType());
        target.setSupportingKeyword(source.getSupportingKeyword());
        target.setToward(source.getToward());
        target.setHouseType(source.getHouseType());
        target.setAirConditioningAvailable(source.getAirConditioningAvailable());
        target.setParkingAvailable(source.getParkingAvailable());
        target.setWaterBillingType(source.getWaterBillingType());
        target.setElectricityBillingType(source.getElectricityBillingType());
        target.setPropertyFeesMax(source.getPropertyFeesMax());
        target.setWaterUnitPriceMax(source.getWaterUnitPriceMax());
        target.setElectricityUnitPriceMax(source.getElectricityUnitPriceMax());
        target.setTags(source.getTags() == null ? new ArrayList<>() : new ArrayList<>(source.getTags()));
        target.setRequiredFields(source.getRequiredFields() == null
            ? new ArrayList<>() : new ArrayList<>(source.getRequiredFields()));
        return target;
    }
}
