package com.gxwebsoft.ai.controller;

import com.gxwebsoft.ai.dto.KnowledgeBaseRequest;
import com.gxwebsoft.ai.service.KnowledgeBaseService;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.enterprise.entity.Enterprise;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "知识库")
@RestController
@RequestMapping("/api/ai/knowledgeBase")
public class KnowledgeBaseController extends BaseController {

    @Autowired
    private KnowledgeBaseService knowledgeBaseService;

    @Operation(summary = "查询知识库")
    @GetMapping("/query")
    public ApiResult<?> query(KnowledgeBaseRequest req) {
        try {
            return success(knowledgeBaseService.queryKnowledgeBase(req));
        } catch (Exception e) {
            return fail("查询失败:" + e.getMessage());
        }
    }

    @Operation(summary = "创建知识库")
    @PostMapping("/create")
    public ApiResult<?> create(@RequestParam String companyName, @RequestParam String companyCode) {
        try {
            String indexId = knowledgeBaseService.createKnowledgeBase(companyName, companyCode);
            return success(indexId);
        } catch (Exception e) {
            return fail("创建失败:" + e.getMessage());
        }
    }
    
    @Operation(summary = "查询知识库下的文档列表")
    @GetMapping("/documents")
    public ApiResult<?> listDocuments(String kbId, Integer pageSize, Integer pageNumber) {
        try {
        	Map<String,Object> map = knowledgeBaseService.listDocuments(kbId, pageSize, pageNumber);
        	List<Object> data = Convert.toList(Object.class, map.get("data"));
        	Long total = MapUtil.getLong(map, "total");
        	return success(new PageResult<Object>(data, total));
        } catch (Exception e) {
            return fail("查询文档列表失败:" + e.getMessage());
        }
    }

    @Operation(summary = "删除知识库下的文档")
    @DeleteMapping("/document")
    public ApiResult<?> deleteDocument(String kbId, String fileIds) {
        try {
            boolean result = knowledgeBaseService.deleteIndexDocument(kbId, fileIds);
            if (result) {
                return success("删除成功");
            } else {
                return fail("删除失败");
            }
        } catch (Exception e) {
            return fail("删除文档失败: " + e.getMessage());
        }
    }
    
    @Operation(summary = "上传文档到知识库")
    @PostMapping("/upload")
    public ApiResult<?> uploadDocuments(@RequestParam String kbId, @RequestParam("files") MultipartFile[] files) {
        try {
            if (files == null || files.length == 0) {
                return fail("请选择要上传的文件");
            }
            
            for (MultipartFile file : files) {
                if (file.isEmpty()) {
                    return fail("文件不能为空: " + file.getOriginalFilename());
                }
                if (file.getSize() > 100 * 1024 * 1024) {
                    return fail("文件大小不能超过100MB: " + file.getOriginalFilename());
                }
            }
            
            List<String> fileIds = knowledgeBaseService.uploadDocuments(kbId, files);
            boolean result = knowledgeBaseService.submitDocuments(kbId, fileIds);

            if (result) {
                return success("成功上传 " + files.length + " 个文件");
            } else {
                return success("部分文件上传成功");
            }
        } catch (Exception e) {
            return fail("上传失败: " + e.getMessage());
        }
    }
}