package org.xyzh.achievement.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.achievement.AchievementService;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.core.event.AchievementEvent;
import org.xyzh.common.core.page.PageRequest;
import org.xyzh.common.dto.usercenter.TbAchievement;
import org.xyzh.common.dto.usercenter.TbUserAchievement;
import org.xyzh.common.dto.usercenter.TbUserAchievementProgress;
import org.xyzh.common.vo.AchievementVO;

import java.util.Map;

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

    @Autowired
    private AchievementService achievementService;

    // ==================== 成就定义管理（管理员）====================

    /**
     * 创建成就
     */
    @PostMapping("/achievement")
    public ResultDomain<TbAchievement> createAchievement(@RequestBody TbAchievement achievement) {
        return achievementService.createAchievement(achievement);
    }

    /**
     * 更新成就
     */
    @PutMapping("/achievement")
    public ResultDomain<TbAchievement> updateAchievement(@RequestBody TbAchievement achievement) {
        return achievementService.updateAchievement(achievement);
    }

    /**
     * 删除成就
     */
    @DeleteMapping("/achievement")
    public ResultDomain<Boolean> deleteAchievement(@RequestBody TbAchievement achievement) {
        return achievementService.deleteAchievement(achievement.getAchievementID());
    }

    /**
     * 获取所有成就列表
     */
    @PostMapping("/list")
    public ResultDomain<TbAchievement> getAllAchievements(TbAchievement filter) {
        return achievementService.getAllAchievements(filter);
    }

    /**
     * 分页查询成就
     */
    @PostMapping("/page")
    public ResultDomain<TbAchievement> getAchievementPage(@RequestBody PageRequest<TbAchievement> pageRequest) {
        return achievementService.getAchievementPage(pageRequest.getFilter(), pageRequest.getPageParam());
    }

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

    // ==================== 用户成就查询 ====================

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

    /**
     * 获取当前用户的成就列表（含进度信息）
     * 返回所有成就，包括：
     * - 已获得的成就：显示获得时间
     * - 未获得的成就：显示当前进度
     */
    @GetMapping("/my")
    public ResultDomain<AchievementVO> getMyAchievements(@RequestParam(required = false, value = "type") Integer type) {
        return achievementService.getMyAchievementsWithProgress(type);
    }

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

    // ==================== 成就授予（管理员）====================

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

    /**
     * 撤销用户成就
     */
    @DeleteMapping("/revoke")
    public ResultDomain<Boolean> revokeAchievement(
            @RequestParam(name = "userID") String userID,
            @RequestParam(name = "achievementID") String achievementID) {
        return achievementService.revokeAchievement(userID, achievementID);
    }

    // ==================== 成就进度查询 ====================

    /**
     * 获取用户成就进度
     */
    @GetMapping("/progress/{userID}")
    public ResultDomain<TbUserAchievementProgress> getUserAchievementProgress(
            @PathVariable(name = "userID") String userID,
            @RequestParam(required = false, value = "achievementID") String achievementID) {
        return achievementService.getUserAchievementProgress(userID, achievementID);
    }

    // ==================== 成就检测 ====================

    /**
     * 处理成就事件（内部接口，由其他服务调用）
     */
    @PostMapping("/event/process")
    public ResultDomain<TbUserAchievement> processAchievementEvent(@RequestBody AchievementEvent event) {
        return achievementService.processAchievementEvent(event);
    }

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

    /**
     * 批量检查用户可获得的成就
     */
    @GetMapping("/available/{userID}")
    public ResultDomain<TbAchievement> checkAvailableAchievements(@PathVariable String userID) {
        return achievementService.checkAvailableAchievements(userID);
    }

    // ==================== 成就统计 ====================

    /**
     * 获取用户成就统计
     */
    @GetMapping("/statistics/{userID}")
    public ResultDomain<Map<String, Object>> getUserAchievementStatistics(@PathVariable String userID) {
        return achievementService.getUserAchievementStatistics(userID);
    }

    /**
     * 获取成就排行榜
     */
    @GetMapping("/ranking")
    public ResultDomain<Map<String, Object>> getAchievementRanking(
            @RequestParam(required = false, defaultValue = "10") Integer limit) {
        return achievementService.getAchievementRanking(limit);
    }

    /**
     * 获取最近获得成就的用户
     */
    @PostMapping("/recent")
    public ResultDomain<TbUserAchievement> getRecentAchievers(@RequestBody PageRequest<TbUserAchievement> pageRequest) {
        return achievementService.getRecentAchievers(pageRequest.getFilter(), pageRequest.getPageParam());
    }
}

