package com.jeesharp.business.modules.setting.controller;

import cn.hutool.core.util.StrUtil;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jeesharp.business.modules.setting.application.HelpCategoryApplicationService;
import com.jeesharp.business.modules.setting.dto.HelpCategorySaveDTO;
import com.jeesharp.business.modules.setting.entity.HelpCategory;
import com.jeesharp.business.modules.setting.vo.HelpCategoryListVO;
import com.jeesharp.business.modules.setting.vo.HelpCategoryQueryVO;
import com.jeesharp.business.modules.setting.vo.HelpCategoryVO;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import com.jeesharp.framework.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 帮助分类管理 Controller（后台管理）
 *
 * @author jeesharp
 * @version 2026-02-05
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/setting/helpCategory/v1")
@Tag(name = "帮助分类管理", description = "帮助分类管理相关接口，包括帮助分类的增删改查等功能")
@AllArgsConstructor
public class HelpCategoryController extends BaseController {

    private final HelpCategoryApplicationService helpCategoryApplicationService;

    // ==================== 基础 CRUD ====================

    /**
     * 获取帮助分类详情
     */
    @ApiOperationSupport(order = 1)
    @Operation(summary = "1、获取帮助分类详情", description = "根据ID获取帮助分类的详细信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "操作成功"),
            @ApiResponse(responseCode = "403", description = "无权限访问")
    })
    @PreAuthorize("@ss.hasPermission('setting:helpCategory:info')")
    @GetMapping(value = "info")
    public AjaxResult<HelpCategoryVO> info(
            @Parameter(description = "ID", required = true)
            @RequestParam("id") String id) {
        return AjaxResult.okData(helpCategoryApplicationService.getHelpCategory(id));
    }

    /**
     * 获取帮助分类列表（分页）
     */
    @ApiOperationSupport(order = 2)
    @Operation(summary = "2、获取帮助分类列表", description = "分页查询帮助分类列表，支持多条件筛选")
    @PreAuthorize("@ss.hasPermission('setting:helpCategory:list')")
    @PostMapping(value = "list")
    public AjaxResult<PageRespVO<HelpCategoryListVO>> list(@RequestBody HelpCategoryQueryVO query) {
        PageRespVO<HelpCategoryListVO> page = helpCategoryApplicationService.findPage(new Page<>(query), query);
        return AjaxResult.okData(page);
    }

    /**
     * 保存帮助分类
     */
    @ApiOperationSupport(order = 3)
    @Operation(summary = "3、保存帮助分类", description = "新增或更新帮助分类信息，ID为空时新增，否则更新")
    @PreAuthorize("@ss.hasPermission('setting:helpCategory:save')")
    @PostMapping(value = "save")
    public AjaxResult<Void> save(@RequestBody @Valid HelpCategorySaveDTO dto) {
        helpCategoryApplicationService.save(dto);
        return AjaxResult.ok("保存成功");
    }

    /**
     * 删除帮助分类
     */
    @ApiOperationSupport(order = 4)
    @Operation(summary = "4、删除帮助分类", description = "批量删除帮助分类，多个ID用逗号分隔")
    @PreAuthorize("@ss.hasPermission('setting:helpCategory:delete')")
    @GetMapping(value = "delete")
    public AjaxResult<Void> delete(
            @Parameter(description = "ID，多个用逗号分隔", required = true)
            @RequestParam("ids") String ids) {
        helpCategoryApplicationService.delete(ids);
        return AjaxResult.ok("删除成功");
    }

    // ==================== 状态管理 ====================

    /**
     * 禁用帮助分类
     */
    @ApiOperationSupport(order = 5)
    @Operation(summary = "5、禁用帮助分类", description = "禁用指定的帮助分类")
    @PreAuthorize("@ss.hasPermission('setting:helpCategory:disable')")
    @GetMapping(value = "disable")
    public AjaxResult<Void> disable(
            @Parameter(description = "ID", required = true)
            @RequestParam("id") String id) {
        helpCategoryApplicationService.disable(id);
        return AjaxResult.ok("禁用成功");
    }

    /**
     * 启用帮助分类
     */
    @ApiOperationSupport(order = 6)
    @Operation(summary = "6、启用帮助分类", description = "启用指定的帮助分类")
    @PreAuthorize("@ss.hasPermission('setting:helpCategory:enable')")
    @GetMapping(value = "enable")
    public AjaxResult<Void> enable(
            @Parameter(description = "ID", required = true)
            @RequestParam("id") String id) {
        helpCategoryApplicationService.enable(id);
        return AjaxResult.ok("启用成功");
    }
}
