package com.it.rattan.monisuo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.it.rattan.monisuo.common.Result;
import com.it.rattan.monisuo.context.UserContext;
import com.it.rattan.monisuo.entity.OrderFund;
import com.it.rattan.monisuo.service.FundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.util.Map;

/**
 * 充提接口
 */
@RestController
@RequestMapping("/api/fund")
public class FundController {

    @Autowired
    private FundService fundService;

    /**
     * 申请充值
     */
    @PostMapping("/deposit")
    public Result<Map<String, Object>> deposit(@RequestBody Map<String, Object> params) {
        Long userId = UserContext.getUserId();
        if (userId == null) {
            return Result.unauthorized("请先登录");
        }

        BigDecimal amount = new BigDecimal(params.get("amount").toString());
        String remark = (String) params.get("remark");

        if (amount.compareTo(BigDecimal.ZERO) <= 0) {
            return Result.fail("充值金额必须大于0");
        }

        try {
            Map<String, Object> result = fundService.deposit(userId, amount, remark);
            return Result.success("申请成功，等待审批", result);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 申请提现
     */
    @PostMapping("/withdraw")
    public Result<Map<String, Object>> withdraw(@RequestBody Map<String, Object> params) {
        Long userId = UserContext.getUserId();
        if (userId == null) {
            return Result.unauthorized("请先登录");
        }

        BigDecimal amount = new BigDecimal(params.get("amount").toString());
        String remark = (String) params.get("remark");

        if (amount.compareTo(BigDecimal.ZERO) <= 0) {
            return Result.fail("提现金额必须大于0");
        }

        try {
            Map<String, Object> result = fundService.withdraw(userId, amount, remark);
            return Result.success("申请成功，等待审批", result);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 取消订单
     */
    @PostMapping("/cancel")
    public Result<Void> cancel(@RequestBody Map<String, String> params) {
        Long userId = UserContext.getUserId();
        if (userId == null) {
            return Result.unauthorized("请先登录");
        }

        String orderNo = params.get("orderNo");
        if (orderNo == null || orderNo.isEmpty()) {
            return Result.fail("订单号不能为空");
        }

        try {
            fundService.cancel(userId, orderNo);
            return Result.success("取消成功", null);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 充提记录
     */
    @GetMapping("/orders")
    public Result<Map<String, Object>> getOrders(
            @RequestParam(required = false) Integer type,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "20") int pageSize) {
        Long userId = UserContext.getUserId();
        if (userId == null) {
            return Result.unauthorized("请先登录");
        }

        try {
            IPage<OrderFund> page = fundService.getOrders(userId, type, pageNum, pageSize);
            Map<String, Object> result = new java.util.HashMap<>();
            result.put("list", page.getRecords());
            result.put("total", page.getTotal());
            result.put("pageNum", page.getCurrent());
            result.put("pageSize", page.getSize());
            return Result.success(result);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
}
