package shop.alien.lawyer.controller;

import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.vo.LawyerDashboardVO;
import shop.alien.entity.store.vo.LawyerOrderStatisticsVO;
import shop.alien.lawyer.service.LawyerStatisticsService;

/**
 * 律师统计 前端控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"律师平台-统计管理"})
@ApiSort(15)
@CrossOrigin
@RestController
@RequestMapping("/lawyer/statistics")
@RequiredArgsConstructor
public class LawyerStatisticsController {

    private final LawyerStatisticsService lawyerStatisticsService;

    /**
     * 获取律师订单统计数据
     * <p>
     * 获取律师的订单统计数据，包括本月订单量、本月收益、总订单量、总收益，
     * 以及进行中、待接单、已退款等各状态订单的统计信息
     * </p>
     *
     * @param lawyerUserId 律师用户ID，必须大于0
     * @return 统一响应结果，包含订单统计数据
     * @author system
     * @since 2025-01-XX
     */
    @ApiOperation("获取律师订单统计数据")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lawyerUserId", value = "律师用户ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getOrderStatistics")
    public R<LawyerOrderStatisticsVO> getOrderStatistics(
            @RequestParam(value = "lawyerUserId", required = true) Integer lawyerUserId) {
        log.info("获取律师订单统计数据请求开始，lawyerUserId={}", lawyerUserId);

        // 参数校验
        if (!validateLawyerUserId(lawyerUserId)) {
            log.warn("获取律师订单统计数据失败：律师用户ID为空或无效，lawyerUserId={}", lawyerUserId);
            return R.fail("律师用户ID不能为空");
        }

        try {
            R<LawyerOrderStatisticsVO> result = lawyerStatisticsService.getOrderStatistics(lawyerUserId);
            log.info("获取律师订单统计数据请求完成，lawyerUserId={}", lawyerUserId);
            return result;
        } catch (Exception e) {
            log.error("获取律师订单统计数据请求异常，lawyerUserId={}，异常信息：{}",
                    lawyerUserId, e.getMessage(), e);
            return R.fail("获取统计数据失败，请稍后重试");
        }
    }

    /**
     * 校验律师用户ID
     * <p>
     * 检查律师用户ID是否为空或无效
     * </p>
     *
     * @param lawyerUserId 律师用户ID
     * @return true表示有效，false表示无效
     */
    private boolean validateLawyerUserId(Integer lawyerUserId) {
        return lawyerUserId != null && lawyerUserId > 0;
    }

    @ApiOperation("获取律师仪表板数据（包含个人信息、订单统计和收益统计）")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lawyerUserId", value = "律师用户ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getLawyerDashboard")
    public R<LawyerDashboardVO> getLawyerDashboard(@RequestParam(value = "lawyerUserId", required = true) Integer lawyerUserId) {
        log.info("LawyerStatisticsController.getLawyerDashboard?lawyerUserId={}", lawyerUserId);
        if (lawyerUserId == null || lawyerUserId <= 0) {
            log.warn("获取律师仪表板数据失败：律师用户ID为空或无效");
            return R.fail("律师用户ID不能为空");
        }
        return lawyerStatisticsService.getLawyerDashboard(lawyerUserId);
    }
}

