package com.emoon.knowledge.controller;

import java.util.List;

import com.emoon.common.core.domain.R;
import com.emoon.common.core.validate.validate.AddGroup;
import com.emoon.common.core.validate.validate.EditGroup;
import com.emoon.common.excel.utils.ExcelUtil;
import com.emoon.common.idempotent.annotation.RepeatSubmit;
import com.emoon.common.log.annotation.Log;
import com.emoon.common.log.enums.BusinessType;
import com.emoon.common.mybatis.core.page.PageQuery;
import com.emoon.common.mybatis.core.page.TableDataInfo;
import com.emoon.common.web.core.BaseController;
import com.emoon.knowledge.domain.bo.KnowledgeAttachBo;
import com.emoon.knowledge.domain.bo.KnowledgeInfoUploadBo;
import com.emoon.knowledge.domain.vo.KnowledgeAttachVo;
import com.emoon.knowledge.service.IKnowledgeAttachService;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;


/**
 * 【请填写功能名称】
 *
 * @author wangkang
 * @date 2025-11-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/knowledgeAttach")
public class KnowledgeAttachController extends BaseController {

    private final IKnowledgeAttachService knowledgeAttachService;
    private final com.emoon.knowledge.service.VectorStoreService vectorStoreService;

    /**
     * 查询【请填写功能名称】列表
     */
    @SaCheckPermission("system:attach:list")
    @GetMapping("/list")
    public TableDataInfo<KnowledgeAttachVo> list(KnowledgeAttachBo bo, PageQuery pageQuery) {
        return knowledgeAttachService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @SaCheckPermission("system:attach:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KnowledgeAttachBo bo, HttpServletResponse response) {
        List<KnowledgeAttachVo> list = knowledgeAttachService.queryList(bo);
        ExcelUtil.exportExcel(list, "【请填写功能名称】", KnowledgeAttachVo.class, response);
    }

    /**
     * 获取【请填写功能名称】详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:attach:query")
    @GetMapping("/{id}")
    public R<KnowledgeAttachVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(knowledgeAttachService.queryById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @SaCheckPermission("system:attach:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody KnowledgeAttachBo bo) {
        return toAjax(knowledgeAttachService.insertByBo(bo));
    }

    /**
     * 修改【请填写功能名称】
     */
    @SaCheckPermission("system:attach:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody KnowledgeAttachBo bo) {
        return toAjax(knowledgeAttachService.updateByBo(bo));
    }

    /**
     * 删除知识库附件
     *
     * @param docId 文档ID
     */
    @SaCheckPermission("system:attach:remove")
    @Log(title = "知识库附件", businessType = BusinessType.DELETE)
    @PostMapping("/remove/{docId}")
    public R<Void> removeAttach(@NotEmpty(message = "文档ID不能为空")
                                @PathVariable String docId) {
        vectorStoreService.removeByDocId(docId);
        knowledgeAttachService.removeKnowledgeAttach(docId);
        return R.ok();
    }

    /**
     * 上传知识库附件
     */
    @SaCheckPermission("system:attach:add")
    @Log(title = "知识库附件", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public R<String> upload(KnowledgeInfoUploadBo bo) throws Exception {
        knowledgeAttachService.upload(bo);
        return R.ok("上传知识库附件成功!");
    }
}
