package org.dromara.ai.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.ai.domain.bo.AiSensitiveWordBo;
import org.dromara.ai.domain.vo.AiSensitiveWordVo;
import org.dromara.ai.service.AiSensitiveWordService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * AI敏感词管理
 *
 * @author pengles
 */
@Tag(name = "管理端-AI敏感词管理", description = "AI敏感词管理接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai/sensitive/word")
public class AdminAiSensitiveWordController extends BaseController {

    private final AiSensitiveWordService aiSensitiveWordService;

    /**
     * 查询AI敏感词列表
     */
    @Operation(summary = "查询AI敏感词列表", description = "分页查询AI敏感词列表，支持多条件搜索")
    @SaCheckPermission("ai:sensitive:word:list")
    @GetMapping("/list")
    public TableDataInfo<AiSensitiveWordVo> list(@Parameter(description = "敏感词查询条件") AiSensitiveWordBo bo,
                                                  @Parameter(description = "分页参数") PageQuery pageQuery) {
        return aiSensitiveWordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出AI敏感词列表
     */
    @Operation(summary = "导出AI敏感词列表", description = "导出AI敏感词到Excel")
    @SaCheckPermission("ai:sensitive:word:export")
    @Log(title = "AI敏感词", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Parameter(description = "敏感词查询条件") AiSensitiveWordBo bo,
                       @Parameter(description = "响应对象") HttpServletResponse response) {
        List<AiSensitiveWordVo> list = aiSensitiveWordService.queryList(bo);
        ExcelUtil.exportExcel(list, "AI敏感词", AiSensitiveWordVo.class, response);
    }

    /**
     * 获取AI敏感词详细信息
     */
    @Operation(summary = "获取AI敏感词详情", description = "根据ID获取详细信息")
    @SaCheckPermission("ai:sensitive:word:query")
    @GetMapping("/{id}")
    public R<AiSensitiveWordVo> getInfo(@Parameter(description = "敏感词ID", required = true)
                                         @NotNull(message = "主键不能为空")
                                         @PathVariable Long id) {
        return R.ok(aiSensitiveWordService.queryById(id));
    }

    /**
     * 新增AI敏感词
     */
    @Operation(summary = "新增AI敏感词", description = "新增AI敏感词")
    @SaCheckPermission("ai:sensitive:word:add")
    @Log(title = "AI敏感词", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Valid @RequestBody AiSensitiveWordBo bo) {
        return toAjax(aiSensitiveWordService.insertByBo(bo));
    }

    /**
     * 修改AI敏感词
     */
    @Operation(summary = "修改AI敏感词", description = "修改AI敏感词")
    @SaCheckPermission("ai:sensitive:word:edit")
    @Log(title = "AI敏感词", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Valid @RequestBody AiSensitiveWordBo bo) {
        return toAjax(aiSensitiveWordService.updateByBo(bo));
    }

    /**
     * 批量删除AI敏感词
     */
    @Operation(summary = "批量删除AI敏感词", description = "根据ID数组批量删除AI敏感词")
    @SaCheckPermission("ai:sensitive:word:remove")
    @Log(title = "AI敏感词", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@Parameter(description = "敏感词ID数组", required = true)
                           @NotNull(message = "主键不能为空")
                           @PathVariable Long[] ids) {
        return toAjax(aiSensitiveWordService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 启用/禁用敏感词
     */
    @Operation(summary = "启用/禁用敏感词", description = "启用或禁用敏感词")
    @SaCheckPermission("ai:sensitive:word:edit")
    @Log(title = "AI敏感词", businessType = BusinessType.UPDATE)
    @PutMapping("/changeEnabled")
    public R<Void> changeEnabled(@Parameter(description = "敏感词ID", required = true)
                                  @NotNull(message = "敏感词ID不能为空")
                                  @RequestParam Long id,
                                  @Parameter(description = "启用状态(0=禁用 1=启用)", required = true)
                                  @NotNull(message = "启用状态不能为空")
                                  @RequestParam Integer enabled) {
        return toAjax(aiSensitiveWordService.changeEnabled(id, enabled));
    }

}