package com.emotion.controller;

import com.emotion.common.PageResult;
import com.emotion.common.Result;
import com.emotion.dto.request.achievement.*;
import com.emotion.dto.response.achievement.AchievementResponse;
import com.emotion.service.AchievementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 成就控制器
 * 
 * @author huazhongmin
 * @date 2025-09-08
 */
@RestController
@RequestMapping("/achievement")
@Tag(name = "成就管理", description = "成就的增删改查功能")
public class AchievementController {

    @Autowired
    private AchievementService achievementService;

    /**
     * 分页查询成就
     */
    @Operation(summary = "分页查询成就", description = "分页查询成就列表")
    @GetMapping("/page")
    public Result<PageResult<AchievementResponse>> getPage(@Validated AchievementPageRequest request) {
        PageResult<AchievementResponse> pageResult = achievementService.getPageWithResponse(request);
        return Result.success(pageResult);
    }

    /**
     * 根据ID获取成就
     */
    @Operation(summary = "根据ID获取成就", description = "根据ID获取成就详情")
    @GetMapping("/detail")
    public Result<AchievementResponse> getById(@RequestParam String id) {
        AchievementResponse response = achievementService.getAchievementResponseById(id);
        if (response == null) {
            return Result.notFound("成就不存在");
        }
        return Result.success(response);
    }

    /**
     * 创建成就
     */
    @Operation(summary = "创建成就", description = "创建新的成就")
    @PostMapping("/create")
    public Result<AchievementResponse> create(@RequestBody @Validated AchievementCreateRequest request) {
        AchievementResponse response = achievementService.createAchievementWithResponse(request);
        if (response == null) {
            return Result.error("创建失败");
        }
        return Result.success("创建成功", response);
    }

    /**
     * 更新成就
     */
    @Operation(summary = "更新成就", description = "更新指定成就")
    @PutMapping("/update")
    public Result<AchievementResponse> update(@RequestBody @Validated AchievementUpdateRequest request) {
        AchievementResponse response = achievementService.updateAchievementWithResponse(request);
        if (response == null) {
            return Result.error("更新失败");
        }
        return Result.success("更新成功", response);
    }

    /**
     * 删除成就
     */
    @Operation(summary = "删除成就", description = "删除指定成就")
    @DeleteMapping("/delete")
    public Result<Void> delete(@RequestParam String id) {
        boolean deleted = achievementService.removeById(id);
        if (!deleted) {
            return Result.error("删除失败");
        }
        return Result.success();
    }

    /**
     * 根据分类查询成就
     */
    @Operation(summary = "根据分类查询成就", description = "根据分类查询成就列表")
    @GetMapping("/byCategory")
    public Result<List<AchievementResponse>> getByCategory(@RequestParam String category) {
        List<AchievementResponse> responses = achievementService.getByCategoryWithResponse(category);
        return Result.success(responses);
    }

    /**
     * 根据稀有度查询成就
     */
    @Operation(summary = "根据稀有度查询成就", description = "根据稀有度查询成就列表")
    @GetMapping("/byRarity")
    public Result<List<AchievementResponse>> getByRarity(@RequestParam String rarity) {
        List<AchievementResponse> responses = achievementService.getByRarityWithResponse(rarity);
        return Result.success(responses);
    }

    /**
     * 查询已解锁的成就
     */
    @Operation(summary = "查询已解锁的成就", description = "查询已解锁的成就列表")
    @GetMapping("/unlocked")
    public Result<List<AchievementResponse>> getUnlockedAchievements() {
        List<AchievementResponse> responses = achievementService.getUnlockedAchievementsWithResponse();
        return Result.success(responses);
    }

    /**
     * 查询未解锁的成就
     */
    @Operation(summary = "查询未解锁的成就", description = "查询未解锁的成就列表")
    @GetMapping("/locked")
    public Result<List<AchievementResponse>> getLockedAchievements() {
        List<AchievementResponse> responses = achievementService.getLockedAchievementsWithResponse();
        return Result.success(responses);
    }

    /**
     * 统计已解锁成就数量
     */
    @Operation(summary = "统计已解锁成就数量", description = "统计已解锁成就数量")
    @GetMapping("/unlockedCount")
    public Result<Long> countUnlockedAchievements() {
        Long count = achievementService.countUnlockedAchievements();
        return Result.success(count);
    }

    /**
     * 统计未解锁成就数量
     */
    @Operation(summary = "统计未解锁成就数量", description = "统计未解锁成就数量")
    @GetMapping("/lockedCount")
    public Result<Long> countLockedAchievements() {
        Long count = achievementService.countLockedAchievements();
        return Result.success(count);
    }

    /**
     * 解锁成就
     */
    @Operation(summary = "解锁成就", description = "解锁指定成就")
    @PutMapping("/unlock")
    public Result<Void> unlockAchievement(@RequestBody @Validated AchievementUnlockRequest request) {
        boolean unlocked = achievementService.unlockAchievement(request.getId(), LocalDateTime.now());
        if (!unlocked) {
            return Result.error("解锁失败");
        }
        return Result.success();
    }

    /**
     * 更新成就进度
     */
    @Operation(summary = "更新成就进度", description = "更新指定成就的进度")
    @PutMapping("/updateProgress")
    public Result<Void> updateProgress(@RequestBody @Validated AchievementProgressUpdateRequest request) {
        boolean updated = achievementService.updateProgress(request.getId(), request.getProgress());
        if (!updated) {
            return Result.error("更新进度失败");
        }
        return Result.success();
    }

    /**
     * 查询最近解锁的成就
     */
    @Operation(summary = "查询最近解锁的成就", description = "查询最近解锁的成就列表")
    @GetMapping("/recent")
    public Result<List<AchievementResponse>> getRecentlyUnlocked(@RequestParam(defaultValue = "10") Integer limit) {
        List<AchievementResponse> responses = achievementService.getRecentlyUnlockedWithResponse(limit);
        return Result.success(responses);
    }
}