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.AssetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 资产接口
 */
@RestController
@RequestMapping("/api/asset")
public class AssetController {

    @Autowired
    private AssetService assetService;

    /**
     * 资产总览
     */
    @GetMapping("/overview")
    public Result<Map<String, Object>> getOverview() {
        Long userId = UserContext.getUserId();
        if (userId == null) {
            return Result.unauthorized("请先登录");
        }

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

    /**
     * 资金账户
     */
    @GetMapping("/fund")
    public Result<Map<String, Object>> getFundAccount() {
        Long userId = UserContext.getUserId();
        if (userId == null) {
            return Result.unauthorized("请先登录");
        }

        try {
            Map<String, Object> result = new java.util.HashMap<>();
            result.put("fund", assetService.getOrCreateFundAccount(userId));
            return Result.success(result);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 交易账户
     */
    @GetMapping("/trade")
    public Result<Map<String, Object>> getTradeAccount() {
        Long userId = UserContext.getUserId();
        if (userId == null) {
            return Result.unauthorized("请先登录");
        }

        try {
            List<Map<String, Object>> positions = assetService.getTradeAccount(userId);
            Map<String, Object> result = new java.util.HashMap<>();
            result.put("positions", positions);
            return Result.success(result);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 资金划转
     */
    @PostMapping("/transfer")
    public Result<Void> transfer(@RequestBody Map<String, Object> params) {
        Long userId = UserContext.getUserId();
        if (userId == null) {
            return Result.unauthorized("请先登录");
        }

        Integer direction = (Integer) params.get("direction");
        BigDecimal amount = new BigDecimal(params.get("amount").toString());

        if (direction == null || (direction != 1 && direction != 2)) {
            return Result.fail("请选择划转方向");
        }
        if (amount.compareTo(BigDecimal.ZERO) <= 0) {
            return Result.fail("划转金额必须大于0");
        }

        try {
            assetService.transfer(userId, direction, amount);
            return Result.success("划转成功", null);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 资金流水
     */
    @GetMapping("/flow")
    public Result<Map<String, Object>> getFlows(
            @RequestParam(required = false) Integer flowType,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "20") int pageSize) {
        Long userId = UserContext.getUserId();
        if (userId == null) {
            return Result.unauthorized("请先登录");
        }

        try {
            List<?> flows = assetService.getFlows(userId, flowType, pageNum, pageSize);
            Map<String, Object> result = new java.util.HashMap<>();
            result.put("list", flows);
            return Result.success(result);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
}
