package com.emotion.controller;

import com.emotion.common.PageResult;
import com.emotion.common.Result;
import com.emotion.dto.request.UserStatsCreateRequest;
import com.emotion.dto.request.UserStatsIncrementRequest;
import com.emotion.dto.request.UserStatsPageRequest;
import com.emotion.dto.request.UserStatsUpdateValueRequest;
import com.emotion.dto.response.UserStatsResponse;
import com.emotion.service.UserStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 用户统计控制器
 * 
 * @author huazhongmin
 * @date 2025-07-23
 */
@RestController
@RequestMapping("/userStats")
@Validated
public class UserStatsController {

    @Autowired
    private UserStatsService userStatsService;

    /**
     * 分页查询用户统计
     */
    @GetMapping(value = "/page")
    public Result<PageResult<UserStatsResponse>> getPage(@Validated UserStatsPageRequest request) {
        PageResult<UserStatsResponse> pageResult = userStatsService.getPageWithResponse(request);
        return Result.success(pageResult);
    }

    /**
     * 创建或更新用户统计
     */
    @PostMapping(value = "/createOrUpdate")
    public Result<UserStatsResponse> createOrUpdate(@Valid @RequestBody UserStatsCreateRequest request) {
        UserStatsResponse stats = userStatsService.createOrUpdateUserStatsWithResponse(request);
        return Result.success(stats);
    }

    /**
     * 更新用户统计值
     */
    @PutMapping(value = "/updateStatsValue")
    public Result<Void> updateStatsValue(@Valid @RequestBody UserStatsUpdateValueRequest request) {
        boolean updated = userStatsService.updateStatsValue(request);
        if (!updated) {
            return Result.error("更新失败");
        }
        return Result.success();
    }

    /**
     * 增加用户统计值
     */
    @PutMapping(value = "/incrementStatsValue")
    public Result<Void> incrementStatsValue(@Valid @RequestBody UserStatsIncrementRequest request) {
        boolean updated = userStatsService.incrementStatsValue(request);
        if (!updated) {
            return Result.error("增加失败");
        }
        return Result.success();
    }

    /**
     * 重新计算用户统计
     */
    @PutMapping(value = "/recalculateUserStats")
    public Result<Void> recalculateUserStats(@RequestParam String userId) {
        boolean recalculated = userStatsService.recalculateUserStats(userId);
        if (!recalculated) {
            return Result.error("重新计算失败");
        }
        return Result.success();
    }

    /**
     * 重新计算所有用户统计
     */
    @PutMapping(value = "/recalculateAll")
    public Result<Void> recalculateAllUserStats() {
        boolean recalculated = userStatsService.recalculateAllUserStats();
        if (!recalculated) {
            return Result.error("重新计算失败");
        }
        return Result.success();
    }

    /**
     * 删除过期的统计数据
     */
    @DeleteMapping(value = "/deleteExpired")
    public Result<Void> deleteExpiredStats(@RequestParam(defaultValue = "30") Integer days) {
        boolean deleted = userStatsService.deleteExpiredStats(days);
        if (!deleted) {
            return Result.error("删除失败");
        }
        return Result.success();
    }
}