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.TagCreateRequestDTO;
import cn.meowrain.aioj.backend.blogservice.dto.req.TagUpdateRequestDTO;
import cn.meowrain.aioj.backend.blogservice.dto.resp.TagResponseDTO;
import cn.meowrain.aioj.backend.blogservice.service.TagService;
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/tag")
@Tag(name = "文章标签管理", description = "文章标签的创建、查询、更新、删除等接口")
public class TagController {

    private final TagService tagService;

    @PostMapping("/create")
    @Operation(summary = "创建标签", description = "创建新的文章标签")
    public Result<Long> createTag(
            @Parameter(description = "标签创建信息", required = true)
            @Valid @RequestBody TagCreateRequestDTO request) {
        Long tagId = tagService.createTag(request);
        return Results.success(tagId);
    }

    @PutMapping("/update")
    @Operation(summary = "更新标签", description = "更新已存在的标签信息")
    public Result<Void> updateTag(
            @Parameter(description = "标签更新信息", required = true)
            @Valid @RequestBody TagUpdateRequestDTO request) {
        tagService.updateTag(request);
        return Results.success();
    }

    @DeleteMapping("/delete/{tagId}")
    @Operation(summary = "删除标签", description = "删除指定标签")
    public Result<Void> deleteTag(
            @Parameter(description = "标签ID", required = true)
            @PathVariable("tagId") Long tagId) {
        tagService.deleteTag(tagId);
        return Results.success();
    }

    @GetMapping("/{tagId}")
    @Operation(summary = "查询标签详情", description = "根据标签ID查询标签详细信息")
    public Result<TagResponseDTO> getTagById(
            @Parameter(description = "标签ID", required = true)
            @PathVariable("tagId") Long tagId) {
        TagResponseDTO tag = tagService.getTagById(tagId);
        return Results.success(tag);
    }

    @GetMapping("/slug/{slug}")
    @Operation(summary = "根据slug查询标签", description = "根据标签别名查询标签详细信息")
    public Result<TagResponseDTO> getTagBySlug(
            @Parameter(description = "标签别名", required = true)
            @PathVariable("slug") String slug) {
        TagResponseDTO tag = tagService.getTagBySlug(slug);
        return Results.success(tag);
    }

    @GetMapping("/list/all")
    @Operation(summary = "查询所有标签", description = "获取所有标签列表")
    public Result<List<TagResponseDTO>> getAllTags() {
        List<TagResponseDTO> tags = tagService.getAllTags();
        return Results.success(tags);
    }

    @GetMapping("/list/enabled")
    @Operation(summary = "查询启用的标签", description = "获取所有启用状态的标签列表")
    public Result<List<TagResponseDTO>> getEnabledTags() {
        List<TagResponseDTO> tags = tagService.getEnabledTags();
        return Results.success(tags);
    }

    @GetMapping("/article/{articleId}")
    @Operation(summary = "查询文章的标签", description = "根据文章ID查询该文章的所有标签")
    public Result<List<TagResponseDTO>> getTagsByArticleId(
            @Parameter(description = "文章ID", required = true)
            @PathVariable("articleId") Long articleId) {
        List<TagResponseDTO> tags = tagService.getTagsByArticleId(articleId);
        return Results.success(tags);
    }
}
