package cn.meowrain.aioj.backend.blogservice.controller;

import cn.meowrain.aioj.backend.framework.core.web.Result;
import cn.meowrain.aioj.backend.framework.core.web.Results;
import cn.meowrain.aioj.backend.blogservice.dto.req.CollectionFolderCreateRequestDTO;
import cn.meowrain.aioj.backend.blogservice.dto.req.CollectionRequestDTO;
import cn.meowrain.aioj.backend.blogservice.dto.resp.ArticleListResponseDTO;
import cn.meowrain.aioj.backend.blogservice.dto.resp.CollectionFolderResponseDTO;
import cn.meowrain.aioj.backend.blogservice.service.CollectionService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收藏管理控制器
 *
 * @author AIOJ
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/v1/collection")
@Tag(name = "收藏管理", description = "文章收藏、收藏夹管理等接口")
public class CollectionController {

    private final CollectionService collectionService;

    @PostMapping("/collect")
    @Operation(summary = "收藏文章", description = "将文章添加到收藏夹")
    public Result<Void> collectArticle(
            @Parameter(description = "收藏信息", required = true)
            @Valid @RequestBody CollectionRequestDTO request) {
        collectionService.collectArticle(request);
        return Results.success();
    }

    @DeleteMapping("/uncollect/{articleId}")
    @Operation(summary = "取消收藏文章", description = "取消收藏指定文章")
    public Result<Void> uncollectArticle(
            @Parameter(description = "文章ID", required = true)
            @PathVariable("articleId") Long articleId) {
        collectionService.uncollectArticle(articleId);
        return Results.success();
    }

    @GetMapping("/check/{articleId}")
    @Operation(summary = "查询收藏状态", description = "查询当前用户是否收藏了指定文章")
    public Result<Boolean> isCollected(
            @Parameter(description = "文章ID", required = true)
            @PathVariable("articleId") Long articleId) {
        Long currentUserId = cn.meowrain.aioj.backend.framework.core.utils.ContextHolderUtils.getCurrentUserId();
        Boolean collected = collectionService.isCollected(currentUserId, articleId);
        return Results.success(collected);
    }

    @PostMapping("/folder/create")
    @Operation(summary = "创建收藏夹", description = "创建新的收藏夹")
    public Result<Long> createCollectionFolder(
            @Parameter(description = "收藏夹创建信息", required = true)
            @Valid @RequestBody CollectionFolderCreateRequestDTO request) {
        Long folderId = collectionService.createCollectionFolder(request);
        return Results.success(folderId);
    }

    @DeleteMapping("/folder/delete/{folderId}")
    @Operation(summary = "删除收藏夹", description = "删除指定收藏夹")
    public Result<Void> deleteCollectionFolder(
            @Parameter(description = "收藏夹ID", required = true)
            @PathVariable("folderId") Long folderId) {
        collectionService.deleteCollectionFolder(folderId);
        return Results.success();
    }

    @GetMapping("/folder/list")
    @Operation(summary = "查询收藏夹列表", description = "获取当前用户的所有收藏夹")
    public Result<List<CollectionFolderResponseDTO>> getCollectionFolders() {
        List<CollectionFolderResponseDTO> folders = collectionService.getCollectionFolders();
        return Results.success(folders);
    }

    @GetMapping("/list")
    @Operation(summary = "分页查询收藏列表", description = "获取当前用户收藏的文章列表")
    public Result<IPage<ArticleListResponseDTO>> getCollectedArticles(
            @Parameter(description = "页码", required = true)
            @RequestParam("current") Integer current,
            @Parameter(description = "每页数量", required = true)
            @RequestParam("size") Integer size,
            @Parameter(description = "收藏夹ID（可选）")
            @RequestParam(value = "folderId", required = false) Long folderId) {
        IPage<ArticleListResponseDTO> page = collectionService.getCollectedArticles(current, size, folderId);
        return Results.success(page);
    }
}
