package com.cms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cms.entity.SysHelp;
import com.cms.entity.SysHelpCategory;
import com.cms.entity.resp.MessageResult;
import com.cms.service.SysHelpCategoryService;
import com.cms.service.SysHelpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 帮助中心 - 公开查询接口（无需登录）
 */
@RestController
@RequestMapping("/help")
@Slf4j
public class HelpController {

    @Autowired
    private SysHelpService sysHelpService;

    @Autowired
    private SysHelpCategoryService sysHelpCategoryService;

    /**
     * 查询所有启用的分类
     * GET /help/categories?scene=HELP
     */
    @GetMapping("/categories")
    public MessageResult categories(@RequestParam(required = false) String scene) {
        List<SysHelpCategory> list = (scene != null && !scene.isEmpty())
                ? sysHelpCategoryService.listEnabledByScene(scene)
                : sysHelpCategoryService.listEnabled();
        return MessageResult.success("SUCCESS", list);
    }

    /**
     * 按分类分页查询文章
     * GET /help/page?categoryCode=FAQ&lang=zh_CN&pageNo=1&pageSize=15
     */
    @GetMapping("/page")
    public MessageResult page(
            @RequestParam(required = false) String categoryCode,
            @RequestParam(required = false) String lang,
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "15") int pageSize
    ) {
        IPage<SysHelp> result = sysHelpService.pageByCategory(pageNo, pageSize, categoryCode, lang);
        return MessageResult.success("SUCCESS", result);
    }

    /**
     * 按分类全量查询（适合协议、免责等少量内容场景）
     * GET /help/list?categoryCode=PRIVACY&lang=en_US
     */
    @GetMapping("/list")
    public MessageResult list(
            @RequestParam(required = false) String categoryCode,
            @RequestParam(required = false) String lang
    ) {
        List<SysHelp> list = sysHelpService.listByCategory(categoryCode, lang);
        return MessageResult.success("SUCCESS", list);
    }

    /**
     * 文章详情
     * GET /help/detail?id=123
     */
    @GetMapping("/detail")
    public MessageResult detail(@RequestParam Long id) {
        SysHelp help = sysHelpService.getDetail(id);
        if (help == null || help.getStatus() != 0) {
            return MessageResult.error("文章不存在或已下线");
        }
        return MessageResult.success("SUCCESS", help);
    }
}
