package com.dora.controller;

import com.dora.common.Result;
import com.dora.dto.PointsConsumptionDto;
import com.dora.service.PointsConsumptionService;
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.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.*;

/**
 * 积分消费查询控制器（用户端）
 */
@Slf4j
@RestController
@RequestMapping("/user/points/consumption")
@Tag(name = "积分消费查询（用户端）", description = "用户积分消费相关查询接口")
@RequiredArgsConstructor
public class PointsConsumptionController {

    private final PointsConsumptionService pointsConsumptionService;

    /**
     * 获取积分余额
     */
    @GetMapping("/balance")
    @Operation(summary = "获取积分余额", description = "获取当前用户的积分余额和过期时间")
    public Result<PointsConsumptionDto.BalanceResponse> getBalance() {
        try {
            Long currentUserId = SecurityUtil.getCurrentUserId();
            PointsConsumptionDto.BalanceResponse balance = 
                    pointsConsumptionService.getBalance(currentUserId);
            return Result.success(balance);
        } catch (AuthenticationException e) {
            log.error("用户未登录", e);
            return Result.error(401, "请先登录");
        } catch (Exception e) {
            log.error("获取积分余额失败", e);
            return Result.error("获取积分余额失败：" + e.getMessage());
        }
    }

    /**
     * 获取积分消费记录
     */
    @GetMapping("/logs")
    @Operation(summary = "获取积分消费记录", description = "获取用户的积分消费明细记录（支持分页和类型筛选）")
    public Result<PointsConsumptionDto.ConsumptionLogPageResponse> getConsumptionLogs(
            @Parameter(description = "页码", example = "1")
            @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页数量", example = "10")
            @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "变动类型（可选：recharge-充值/consume-消费/refund-退款/admin_adjust-管理员调整）", example = "")
            @RequestParam(required = false) String changeType) {
        try {
            Long currentUserId = SecurityUtil.getCurrentUserId();
            
            PointsConsumptionDto.ConsumptionLogPageResponse logs = 
                    pointsConsumptionService.getConsumptionLogs(currentUserId, page, size, changeType);
            
            return Result.success(logs);
        } catch (AuthenticationException e) {
            log.error("用户未登录", e);
            return Result.error(401, "请先登录");
        } catch (Exception e) {
            log.error("获取积分消费记录失败", e);
            return Result.error("获取积分消费记录失败：" + e.getMessage());
        }
    }

    /**
     * 获取积分统计
     */
    @GetMapping("/stats")
    @Operation(summary = "获取积分统计", description = "获取用户的积分统计信息（累计充值、消费、退款等）")
    public Result<PointsConsumptionDto.ConsumptionStatsResponse> getConsumptionStats() {
        try {
            Long currentUserId = SecurityUtil.getCurrentUserId();
            
            PointsConsumptionDto.ConsumptionStatsResponse stats = 
                    pointsConsumptionService.getConsumptionStats(currentUserId);
            
            return Result.success(stats);
        } catch (AuthenticationException e) {
            log.error("用户未登录", e);
            return Result.error(401, "请先登录");
        } catch (Exception e) {
            log.error("获取积分统计失败", e);
            return Result.error("获取积分统计失败：" + e.getMessage());
        }
    }
}

