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.ArticleCreateRequestDTO;
import cn.meowrain.aioj.backend.blogservice.dto.req.ArticleQueryRequestDTO;
import cn.meowrain.aioj.backend.blogservice.dto.req.ArticleUpdateRequestDTO;
import cn.meowrain.aioj.backend.blogservice.dto.resp.ArticleListResponseDTO;
import cn.meowrain.aioj.backend.blogservice.dto.resp.ArticleResponseDTO;
import cn.meowrain.aioj.backend.blogservice.service.ArticleService;
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.*;

/**
 * 文章管理控制器
 *
 * @author AIOJ
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/v1/article")
@Tag(name = "文章管理", description = "文章的创建、查询、更新、删除等接口")
public class ArticleController {

    private final ArticleService articleService;

    @PostMapping("/create")
    @Operation(summary = "创建文章", description = "创建新文章，支持Markdown格式")
    public Result<Long> createArticle(
            @Parameter(description = "文章创建信息", required = true)
            @Valid @RequestBody ArticleCreateRequestDTO request) {
        Long articleId = articleService.createArticle(request);
        return Results.success(articleId);
    }

    @PutMapping("/update")
    @Operation(summary = "更新文章", description = "更新已存在的文章信息")
    public Result<Void> updateArticle(
            @Parameter(description = "文章更新信息", required = true)
            @Valid @RequestBody ArticleUpdateRequestDTO request) {
        articleService.updateArticle(request);
        return Results.success();
    }

    @DeleteMapping("/delete/{articleId}")
    @Operation(summary = "删除文章", description = "逻辑删除指定文章")
    public Result<Void> deleteArticle(
            @Parameter(description = "文章ID", required = true)
            @PathVariable("articleId") Long articleId) {
        articleService.deleteArticle(articleId);
        return Results.success();
    }

    @GetMapping("/{articleId}")
    @Operation(summary = "查询文章详情", description = "根据文章ID查询文章详细信息")
    public Result<ArticleResponseDTO> getArticleById(
            @Parameter(description = "文章ID", required = true)
            @PathVariable("articleId") Long articleId) {
        ArticleResponseDTO article = articleService.getArticleById(articleId);
        return Results.success(article);
    }

    @GetMapping("/slug/{slug}")
    @Operation(summary = "根据slug查询文章", description = "根据文章别名查询文章详细信息")
    public Result<ArticleResponseDTO> getArticleBySlug(
            @Parameter(description = "文章别名", required = true)
            @PathVariable("slug") String slug) {
        ArticleResponseDTO article = articleService.getArticleBySlug(slug);
        return Results.success(article);
    }

    @PostMapping("/list")
    @Operation(summary = "分页查询文章列表", description = "支持多条件查询和排序")
    public Result<IPage<ArticleListResponseDTO>> getArticleList(
            @Parameter(description = "查询条件")
            @RequestBody ArticleQueryRequestDTO request) {
        IPage<ArticleListResponseDTO> page = articleService.getArticleList(request);
        return Results.success(page);
    }

    @PutMapping("/publish/{articleId}")
    @Operation(summary = "发布文章", description = "将草稿状态的文章发布")
    public Result<Void> publishArticle(
            @Parameter(description = "文章ID", required = true)
            @PathVariable("articleId") Long articleId) {
        articleService.publishArticle(articleId);
        return Results.success();
    }

    @PutMapping("/unpublish/{articleId}")
    @Operation(summary = "取消发布文章", description = "将已发布的文章转为草稿状态")
    public Result<Void> unpublishArticle(
            @Parameter(description = "文章ID", required = true)
            @PathVariable("articleId") Long articleId) {
        articleService.unpublishArticle(articleId);
        return Results.success();
    }

    @PostMapping("/view/{articleId}")
    @Operation(summary = "增加文章浏览量", description = "记录文章浏览并增加浏览计数")
    public Result<Void> incrementViewCount(
            @Parameter(description = "文章ID", required = true)
            @PathVariable("articleId") Long articleId) {
        articleService.incrementViewCount(articleId);
        return Results.success();
    }
}
