package com.dora.controller;

import com.dora.dto.UserBalanceDto;
import com.dora.service.UserBalanceService;
import com.dora.util.ApiResponseUtil;
import com.dora.util.SecurityUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 用户余额管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/user/balance")
@RequiredArgsConstructor
@Tag(name = "用户余额管理", description = "用户余额相关接口")
public class UserBalanceController {

    private final UserBalanceService userBalanceService;

    /**
     * 获取用户余额信息
     */
    @GetMapping
    @Operation(summary = "获取用户余额信息", description = "获取当前用户的余额详细信息")
    public Map<String, Object> getUserBalance() {
        try {
            Long userId = SecurityUtil.getCurrentUserId();
            UserBalanceDto.UserBalanceResponse data = userBalanceService.getUserBalanceInfo(userId);
            return ApiResponseUtil.success("获取余额信息成功", data);
        } catch (Exception e) {
            log.error("获取用户余额信息失败", e);
            return ApiResponseUtil.error("获取余额信息失败: " + e.getMessage());
        }
    }

    /**
     * 获取用户余额增加明细
     */
    @GetMapping("/income-detail")
    @Operation(summary = "获取用户余额增加明细", description = "获取当前用户的余额增加明细，包括推广收益和内容收益")
    public Map<String, Object> getIncomeDetail() {
        try {
            Long userId = SecurityUtil.getCurrentUserId();
            UserBalanceDto.IncomeDetailResponse data = userBalanceService.getUserIncomeDetail(userId);
            return ApiResponseUtil.success("获取收益明细成功", data);
        } catch (Exception e) {
            log.error("获取用户收益明细失败", e);
            return ApiResponseUtil.error("获取收益明细失败: " + e.getMessage());
        }
    }

    /**
     * 获取用户余额变动记录
     */
    @GetMapping("/logs")
    @Operation(summary = "获取用户余额变动记录", description = "获取当前用户的余额变动记录，支持分页")
    public Map<String, Object> getBalanceLogs(
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "变动类型", example = "income") 
            @RequestParam(required = false) String changeType) {
        
        try {
            Long userId = SecurityUtil.getCurrentUserId();
            UserBalanceDto.BalanceLogResponse data = userBalanceService.getUserBalanceLogs(userId, page, size, changeType);
            return ApiResponseUtil.success("获取余额变动记录成功", data);
        } catch (Exception e) {
            log.error("获取用户余额变动记录失败", e);
            return ApiResponseUtil.error("获取余额变动记录失败: " + e.getMessage());
        }
    }

    /**
     * 获取用户余额统计信息
     */
    @GetMapping("/stats")
    @Operation(summary = "获取用户余额统计信息", description = "获取当前用户的余额统计信息")
    public Map<String, Object> getBalanceStats() {
        try {
            Long userId = SecurityUtil.getCurrentUserId();
            UserBalanceDto.UserBalanceResponse data = userBalanceService.getUserBalanceStats(userId);
            return ApiResponseUtil.success("获取余额统计信息成功", data);
        } catch (Exception e) {
            log.error("获取用户余额统计信息失败", e);
            return ApiResponseUtil.error("获取余额统计信息失败: " + e.getMessage());
        }
    }
} 