package com.haha.service.impl;

import com.haha.common.constant.RedisConstants;
import com.haha.service.StatusQueryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 状态查询服务实现
 * 封装设备状态、识别结果、订单信息的Redis查询逻辑
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class StatusQueryServiceImpl implements StatusQueryService {

    private final StringRedisTemplate redisTemplate;

    @Override
    public Map<String, String> queryDeviceStatus(String deviceId) {
        String statusKey = RedisConstants.HAHA_DEVICE_STATUS_KEY + deviceId;
        Map<Object, Object> statusData = redisTemplate.opsForHash().entries(statusKey);

        if (statusData.isEmpty()) {
            Map<String, String> emptyResult = new HashMap<>();
            emptyResult.put("deviceId", deviceId);
            emptyResult.put("doorStatus", "unknown");
            return emptyResult;
        }

        Map<String, String> result = new HashMap<>();
        result.put("deviceId", statusData.getOrDefault("deviceId", "").toString());
        result.put("activityId", statusData.getOrDefault("activityId", "").toString());
        result.put("doorStatus", statusData.getOrDefault("doorStatus", "unknown").toString());
        result.put("openType", statusData.getOrDefault("openType", "").toString());
        result.put("userId", statusData.getOrDefault("userId", "").toString());
        result.put("timestamp", statusData.getOrDefault("timestamp", "").toString());

        log.debug("[状态查询] 设备状态查询成功 - deviceId: {}, doorStatus: {}", deviceId, result.get("doorStatus"));
        return result;
    }

    @Override
    public Map<String, String> queryRecognizeResult(String activityId) {
        String resultKey = RedisConstants.HAHA_RECOGNIZE_RESULT_KEY + activityId;
        Map<Object, Object> resultData = redisTemplate.opsForHash().entries(resultKey);

        if (resultData.isEmpty()) {
            return null;
        }

        Map<String, String> result = new HashMap<>();
        resultData.forEach((k, v) -> result.put(k.toString(), v != null ? v.toString() : ""));

        log.info("[状态查询] 识别结果查询成功 - activityId: {}, nobuy: {}", activityId, result.get("nobuy"));
        return result;
    }

    @Override
    public Map<String, String> queryOrderInfo(String orderId, String activityId) {
        String queryKey = (orderId != null && !orderId.isEmpty()) ? orderId : activityId;
        String orderKey = RedisConstants.HAHA_ORDER_INFO_KEY + queryKey;
        Map<Object, Object> orderData = redisTemplate.opsForHash().entries(orderKey);

        if (orderData.isEmpty()) {
            return null;
        }

        Map<String, String> result = new HashMap<>();
        orderData.forEach((k, v) -> result.put(k.toString(), v != null ? v.toString() : ""));

        log.debug("[状态查询] 订单信息查询成功 - queryKey: {}, totalAmount: {}", queryKey, result.get("totalAmount"));
        return result;
    }

    @Override
    public Map<String, Object> queryAllStatus(String deviceId) {
        Map<String, Object> result = new HashMap<>();

        Map<String, String> deviceStatus = queryDeviceStatus(deviceId);
        if (!deviceStatus.containsKey("doorStatus") || !"unknown".equals(deviceStatus.get("doorStatus"))) {
            result.put("deviceStatus", deviceStatus);

            String activityId = deviceStatus.get("activityId");
            if (activityId != null && !activityId.isEmpty()) {
                Map<String, String> recognizeResult = queryRecognizeResult(activityId);
                if (recognizeResult != null) {
                    result.put("recognizeResult", recognizeResult);
                }

                Map<String, String> orderInfo = queryOrderInfo(null, activityId);
                if (orderInfo != null) {
                    result.put("orderInfo", orderInfo);
                }
            }
        } else {
            result.put("deviceStatus", deviceStatus);
        }

        log.debug("[状态查询] 综合状态查询成功 - deviceId: {}, 包含数据: {}", deviceId, result.keySet());
        return result;
    }
}
