package shop.alien.store.controller;

import com.alibaba.fastjson2.JSONObject;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.store.util.ai.AiUserSessionUtil;

import java.util.Map;

/**
 * AI用户会话管理控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"AI用户会话管理"})
@CrossOrigin
@RestController
@RequestMapping("/aiUserSession")
@RequiredArgsConstructor
public class AiUserSessionController {

    private final AiUserSessionUtil aiUserSessionUtil;

    @ApiOperation("获取用户会话列表")
    @ApiOperationSupport(order = 1)
    @PostMapping("/getUserSessions")
    public R<JSONObject> getUserSessions(@RequestBody Map<String, Object> request) {
        log.info("AiUserSessionController.getUserSessions: request={}", request);
        try {
            if (request == null || !request.containsKey("user_id")) {
                return R.fail("用户ID不能为空");
            }
            //type: 0是ai客服，1是运营客服
            if (!request.containsKey("type")) {
                return R.fail("type不能为空");
            }

            Object userIdObj = request.get("user_id");
            if (userIdObj == null) {
                return R.fail("用户ID不能为空");
            }

            Integer userId;
            if (userIdObj instanceof Integer) {
                userId = (Integer) userIdObj;
            } else if (userIdObj instanceof Number) {
                userId = ((Number) userIdObj).intValue();
            } else {
                try {
                    userId = Integer.parseInt(userIdObj.toString());
                } catch (NumberFormatException e) {
                    return R.fail("用户ID格式错误");
                }
            }

            if (userId <= 0) {
                return R.fail("用户ID必须大于0");
            }

            Object typeObj = request.get("type");
            if (typeObj == null) {
                return R.fail("type不能为空");
            }

            Integer type;
            if (typeObj instanceof Integer) {
                type = (Integer) typeObj;
            } else if (typeObj instanceof Number) {
                type = ((Number) typeObj).intValue();
            } else {
                try {
                    type = Integer.parseInt(typeObj.toString());
                } catch (NumberFormatException e) {
                    return R.fail("type格式错误");
                }
            }

            // type: 0是ai客服，1是运营客服
            if (type != 0 && type != 1) {
                return R.fail("type必须为0或1，0是ai客服，1是运营客服");
            }

            JSONObject result = aiUserSessionUtil.getUserSessions(userId, type);
            if (result != null) {
                Boolean success = result.getBoolean("success");
                if (Boolean.TRUE.equals(success)) {
                    return R.data(result);
                } else {
                    String message = result.getString("message");
                    return R.fail(message != null ? message : "获取用户会话列表失败");
                }
            }
            return R.fail("获取用户会话列表失败");
        } catch (Exception e) {
            log.error("AiUserSessionController.getUserSessions ERROR: {}", e.getMessage(), e);
            return R.fail("获取用户会话列表异常：" + e.getMessage());
        }
    }
}

