package org.xyzh.usercenter.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xyzh.api.usercenter.achievement.UserAchievementService;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.dto.usercenter.TbAchievement;
import org.xyzh.common.dto.usercenter.TbUserAchievement;

/**
 * @description 用户成就控制器
 * @filename UserAchievementController.java
 * @author yslg
 * @copyright xyzh
 * @since 2025-10-15
 */
@RestController
@RequestMapping("/usercenter/achievement")
public class UserAchievementController {
    private static final Logger logger = LoggerFactory.getLogger(UserAchievementController.class);

    @Autowired
    private UserAchievementService userAchievementService;

    /**
     * 获取所有成就列表
     */
    @GetMapping("/list")
    public ResultDomain<TbAchievement> getAllAchievements(
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) Integer level) {
        return userAchievementService.getAllAchievements(type, level);
    }

    /**
     * 获取用户已获得的成就
     */
    @GetMapping("/user/{userID}")
    public ResultDomain<TbUserAchievement> getUserAchievements(
            @PathVariable String userID,
            @RequestParam(required = false) Integer type) {
        return userAchievementService.getUserAchievements(userID, type);
    }

    /**
     * 检查用户是否已获得成就
     */
    @GetMapping("/check/{userID}/{achievementID}")
    public ResultDomain<Boolean> hasAchievement(
            @PathVariable String userID,
            @PathVariable String achievementID) {
        return userAchievementService.hasAchievement(userID, achievementID);
    }

    /**
     * 授予用户成就
     */
    @PostMapping("/grant")
    public ResultDomain<TbUserAchievement> grantAchievement(
            @RequestParam String userID,
            @RequestParam String achievementID) {
        return userAchievementService.grantAchievement(userID, achievementID);
    }

    /**
     * 获取成就详情
     */
    @GetMapping("/detail/{achievementID}")
    public ResultDomain<TbAchievement> getAchievementDetail(@PathVariable String achievementID) {
        return userAchievementService.getAchievementDetail(achievementID);
    }

    /**
     * 检查用户是否满足成就条件
     */
    @GetMapping("/condition/{userID}/{achievementID}")
    public ResultDomain<Boolean> checkAchievementCondition(
            @PathVariable String userID,
            @PathVariable String achievementID) {
        return userAchievementService.checkAchievementCondition(userID, achievementID);
    }
}
