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.CategoryCreateRequestDTO;
import cn.meowrain.aioj.backend.blogservice.dto.req.CategoryUpdateRequestDTO;
import cn.meowrain.aioj.backend.blogservice.dto.resp.CategoryResponseDTO;
import cn.meowrain.aioj.backend.blogservice.service.CategoryService;
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/category")
@Tag(name = "文章分类管理", description = "文章分类的创建、查询、更新、删除等接口")
public class CategoryController {

    private final CategoryService categoryService;

    @PostMapping("/create")
    @Operation(summary = "创建分类", description = "创建新的文章分类")
    public Result<Long> createCategory(
            @Parameter(description = "分类创建信息", required = true)
            @Valid @RequestBody CategoryCreateRequestDTO request) {
        Long categoryId = categoryService.createCategory(request);
        return Results.success(categoryId);
    }

    @PutMapping("/update")
    @Operation(summary = "更新分类", description = "更新已存在的分类信息")
    public Result<Void> updateCategory(
            @Parameter(description = "分类更新信息", required = true)
            @Valid @RequestBody CategoryUpdateRequestDTO request) {
        categoryService.updateCategory(request);
        return Results.success();
    }

    @DeleteMapping("/delete/{categoryId}")
    @Operation(summary = "删除分类", description = "删除指定分类")
    public Result<Void> deleteCategory(
            @Parameter(description = "分类ID", required = true)
            @PathVariable("categoryId") Long categoryId) {
        categoryService.deleteCategory(categoryId);
        return Results.success();
    }

    @GetMapping("/{categoryId}")
    @Operation(summary = "查询分类详情", description = "根据分类ID查询分类详细信息")
    public Result<CategoryResponseDTO> getCategoryById(
            @Parameter(description = "分类ID", required = true)
            @PathVariable("categoryId") Long categoryId) {
        CategoryResponseDTO category = categoryService.getCategoryById(categoryId);
        return Results.success(category);
    }

    @GetMapping("/slug/{slug}")
    @Operation(summary = "根据slug查询分类", description = "根据分类别名查询分类详细信息")
    public Result<CategoryResponseDTO> getCategoryBySlug(
            @Parameter(description = "分类别名", required = true)
            @PathVariable("slug") String slug) {
        CategoryResponseDTO category = categoryService.getCategoryBySlug(slug);
        return Results.success(category);
    }

    @GetMapping("/list/all")
    @Operation(summary = "查询所有分类", description = "获取所有分类列表")
    public Result<List<CategoryResponseDTO>> getAllCategories() {
        List<CategoryResponseDTO> categories = categoryService.getAllCategories();
        return Results.success(categories);
    }

    @GetMapping("/list/enabled")
    @Operation(summary = "查询启用的分类", description = "获取所有启用状态的分类列表")
    public Result<List<CategoryResponseDTO>> getEnabledCategories() {
        List<CategoryResponseDTO> categories = categoryService.getEnabledCategories();
        return Results.success(categories);
    }
}
