package com.haha.miniapp.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.haha.common.constant.ResponseEnum;
import com.haha.common.vo.Result;
import com.haha.service.StatusQueryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/status")
@SaIgnore
public class StatusQueryController {

    private final StatusQueryService statusQueryService;

    @PostMapping("/device")
    public Result<Map<String, String>> queryDeviceStatus(@RequestBody Map<String, String> params) {
        String deviceId = params.get("deviceId");
        if (deviceId == null || deviceId.isEmpty()) {
            return ResponseEnum.PARAM_REQUIRED.toResult("设备ID不能为空");
        }

        log.debug("[状态查询] 查询设备状态 - deviceId: {}", deviceId);
        Map<String, String> result = statusQueryService.queryDeviceStatus(deviceId);

        if (result.containsKey("doorStatus") && "unknown".equals(result.get("doorStatus"))) {
            return Result.success("暂无状态数据", result);
        }
        return Result.success("查询成功", result);
    }

    @PostMapping("/recognize")
    public Result<Map<String, String>> queryRecognizeResult(@RequestBody Map<String, String> params) {
        String activityId = params.get("activityId");
        if (activityId == null || activityId.isEmpty()) {
            return ResponseEnum.PARAM_REQUIRED.toResult("活动ID不能为空");
        }

        log.info("[状态查询] 查询识别结果 - activityId: {}", activityId);
        Map<String, String> result = statusQueryService.queryRecognizeResult(activityId);

        if (result == null) {
            return ResponseEnum.NOT_FOUND.toResult("识别结果尚未生成，请稍后重试");
        }
        return Result.success("查询成功", result);
    }

    @PostMapping("/order")
    public Result<Map<String, String>> queryOrderInfo(@RequestBody Map<String, String> params) {
        String orderId = params.get("orderId");
        String activityId = params.get("activityId");

        if ((orderId == null || orderId.isEmpty()) && (activityId == null || activityId.isEmpty())) {
            return ResponseEnum.PARAM_REQUIRED.toResult("订单ID或活动ID不能同时为空");
        }

        log.debug("[状态查询] 查询订单信息 - orderId: {}, activityId: {}", orderId, activityId);
        Map<String, String> result = statusQueryService.queryOrderInfo(orderId, activityId);

        if (result == null) {
            return ResponseEnum.NOT_FOUND.toResult("订单信息尚未生成，请稍后重试");
        }
        return Result.success("查询成功", result);
    }

    @PostMapping("/all")
    public Result<Map<String, Object>> queryAllStatus(@RequestBody Map<String, String> params) {
        String deviceId = params.get("deviceId");
        if (deviceId == null || deviceId.isEmpty()) {
            return ResponseEnum.PARAM_REQUIRED.toResult("设备ID不能为空");
        }

        log.debug("[状态查询] 综合状态查询 - deviceId: {}", deviceId);
        Map<String, Object> result = statusQueryService.queryAllStatus(deviceId);
        return Result.success("查询成功", result);
    }
}
