package com.zhentao.controller;

import com.zhentao.common.Result;
import com.zhentao.entity.Tag;
import com.zhentao.entity.TagCategory;
import com.zhentao.service.TagService;
import com.zhentao.service.TagCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 标签Controller
 */
@RestController
@RequestMapping("/api/tag")
public class TagController {
    
    @Autowired
    private TagService tagService;
    
    @Autowired
    private TagCategoryService tagCategoryService;
    
    /**
     * 获取所有启用的标签
     * @return 标签列表
     */
    @GetMapping("/list")
    public Result<List<Tag>> getAllTags(@RequestParam(required = false) Integer categoryId) {
        try {
            List<Tag> tags;
            if (categoryId != null) {
                tags = tagService.getActiveTagsByCategory(categoryId);
            } else {
                tags = tagService.getAllActiveTags();
            }
            return Result.success(tags);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取标签列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取所有启用的标签分类
     * @return 分类列表
     */
    @GetMapping("/categories")
    public Result<List<TagCategory>> getAllCategories() {
        try {
            List<TagCategory> categories = tagCategoryService.getAllActiveCategories();
            return Result.success(categories);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取标签分类列表失败：" + e.getMessage());
        }
    }
}

