package com.it.rattan.monisuo.controller;

import com.it.rattan.monisuo.common.Result;
import com.it.rattan.monisuo.context.UserContext;
import com.it.rattan.monisuo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

/**
 * 用户接口
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result<Map<String, Object>> register(@RequestBody Map<String, String> params) {
        String username = params.get("username");
        String password = params.get("password");

        if (username == null || username.trim().isEmpty()) {
            return Result.fail("用户名不能为空");
        }
        if (password == null || password.length() < 6) {
            return Result.fail("密码长度至少6位");
        }

        try {
            Map<String, Object> result = userService.register(username.trim(), password);
            return Result.success("注册成功", result);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<Map<String, Object>> login(@RequestBody Map<String, String> params) {
        String username = params.get("username");
        String password = params.get("password");

        if (username == null || username.trim().isEmpty()) {
            return Result.fail("用户名不能为空");
        }
        if (password == null || password.isEmpty()) {
            return Result.fail("密码不能为空");
        }

        try {
            Map<String, Object> result = userService.login(username.trim(), password);
            return Result.success("登录成功", result);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/info")
    public Result<Map<String, Object>> getUserInfo() {
        Long userId = UserContext.getUserId();
        if (userId == null) {
            return Result.unauthorized("请先登录");
        }

        try {
            Map<String, Object> result = userService.getUserInfo(userId);
            return Result.success(result);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 上传KYC资料
     */
    @PostMapping("/kyc")
    public Result<Void> uploadKyc(@RequestBody Map<String, String> params) {
        Long userId = UserContext.getUserId();
        if (userId == null) {
            return Result.unauthorized("请先登录");
        }

        String idCardFront = params.get("idCardFront");
        String idCardBack = params.get("idCardBack");

        if (idCardFront == null || idCardFront.isEmpty()) {
            return Result.fail("请上传身份证正面照");
        }
        if (idCardBack == null || idCardBack.isEmpty()) {
            return Result.fail("请上传身份证反面照");
        }

        try {
            userService.uploadKyc(userId, idCardFront, idCardBack);
            return Result.success("上传成功", null);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    public Result<Void> logout() {
        // 客户端清除Token即可
        return Result.success("退出成功", null);
    }
}
