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.HelpArticleApplicationService;
import com.jeesharp.business.modules.setting.dto.HelpArticleSaveDTO;
import com.jeesharp.business.modules.setting.entity.HelpArticle;
import com.jeesharp.business.modules.setting.vo.HelpArticleListVO;
import com.jeesharp.business.modules.setting.vo.HelpArticleQueryVO;
import com.jeesharp.business.modules.setting.vo.HelpArticleVO;
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/helpArticle/v1")
@Tag(name = "帮助文章管理", description = "帮助文章管理相关接口，包括帮助文章的增删改查等功能")
@AllArgsConstructor
public class HelpArticleController extends BaseController {

    private final HelpArticleApplicationService helpArticleApplicationService;

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

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

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

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

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

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

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

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