package org.xyzh.ai.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.xyzh.api.ai.file.AiUploadFileService;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.core.page.PageDomain;
import org.xyzh.common.core.page.PageParam;
import org.xyzh.common.core.page.PageRequest;
import org.xyzh.common.dto.ai.TbAiUploadFile;

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

/**
 * @description AI文件上传控制器
 * @filename AiFileUploadController.java
 * @author AI Assistant
 * @copyright xyzh
 * @since 2025-11-04
 */
@Slf4j
@RestController
@RequestMapping("/ai/file")
public class AiFileUploadController {

    @Autowired
    private AiUploadFileService uploadFileService;

    /**
     * @description 上传文件用于对话（图文多模态）
     * @param file 文件
     * @param agentId 智能体ID
     * @return ResultDomain<Map<String, Object>> 返回Dify文件信息
     * @author AI Assistant
     * @since 2025-11-06
     */
    @PostMapping("/upload-for-chat")
    public ResultDomain<java.util.Map<String, Object>> uploadFileForChat(
            @RequestParam("file") MultipartFile file,
            @RequestParam("agentId") String agentId) {
        log.info("上传对话文件: fileName={}, agentId={}", file.getOriginalFilename(), agentId);
        return uploadFileService.uploadFileForChat(file, agentId);
    }

    /**
     * @description 上传文件到知识库
     * @param knowledgeId 知识库ID
     * @param file 文件
     * @param indexingTechnique 索引方式（可选）
     * @return ResultDomain<TbAiUploadFile>
     * @author AI Assistant
     * @since 2025-11-04
     */
    @PostMapping("/upload")
    public ResultDomain<TbAiUploadFile> uploadFile(
            @RequestParam(name = "knowledgeId") String knowledgeId,
            @RequestParam(name = "file") MultipartFile file,
            @RequestParam(name = "indexingTechnique", required = false) String indexingTechnique) {
        log.info("上传文件到知识库: knowledgeId={}, fileName={}", knowledgeId, file.getOriginalFilename());
        return uploadFileService.uploadToKnowledge(knowledgeId, file, indexingTechnique);
    }

    /**
     * @description 批量上传文件
     * @param knowledgeId 知识库ID
     * @param files 文件列表
     * @param indexingTechnique 索引方式（可选）
     * @return ResultDomain<TbAiUploadFile>
     * @author AI Assistant
     * @since 2025-11-04
     */
    @PostMapping("/upload/batch")
    public ResultDomain<TbAiUploadFile> batchUploadFiles(
            @RequestParam(name = "knowledgeId") String knowledgeId,
            @RequestParam(name = "files") MultipartFile[] files,
            @RequestParam(name = "indexingTechnique", required = false) String indexingTechnique) {
        log.info("批量上传文件: knowledgeId={}, fileCount={}", knowledgeId, files.length);
        return uploadFileService.batchUploadToKnowledge(knowledgeId, Arrays.asList(files), indexingTechnique);
    }

    /**
     * @description 获取文件信息
     * @param fileId 文件ID
     * @return ResultDomain<TbAiUploadFile>
     * @author AI Assistant
     * @since 2025-11-04
     */
    @GetMapping("/{fileId}")
    public ResultDomain<TbAiUploadFile> getFile(@PathVariable(name = "fileId") String fileId) {
        log.info("获取文件信息: fileId={}", fileId);
        return uploadFileService.getFileById(fileId);
    }

    /**
     * @description 查询消息关联的文件列表
     * @param messageId 消息ID
     * @return ResultDomain<TbAiUploadFile>
     * @author AI Assistant
     * @since 2025-11-06
     */
    @GetMapping("/message/{messageId}")
    public ResultDomain<TbAiUploadFile> getMessageFiles(@PathVariable(name = "messageId") String messageId) {
        log.info("查询消息文件列表: messageId={}", messageId);
        return uploadFileService.listFilesByMessageId(messageId);
    }

    /**
     * @description 查询知识库的文件列表
     * @param knowledgeId 知识库ID
     * @return ResultDomain<TbAiUploadFile>
     * @author AI Assistant
     * @since 2025-11-04
     */
    @GetMapping("/list")
    public ResultDomain<TbAiUploadFile> listFiles(@RequestParam(name = "knowledgeId") String knowledgeId) {
        log.info("查询知识库文件列表: knowledgeId={}", knowledgeId);
        return uploadFileService.listFilesByKnowledge(knowledgeId);
    }

    /**
     * @description 分页查询文件列表
     * @param pageRequest 分页请求（包含filter和pageParam）
     * @return PageDomain<TbAiUploadFile>
     * @author AI Assistant
     * @since 2025-11-04
     */
    @PostMapping("/page")
    public PageDomain<TbAiUploadFile> pageFiles(@RequestBody PageRequest<TbAiUploadFile> pageRequest) {
        log.info("分页查询文件列表");
        return uploadFileService.pageFiles(pageRequest.getFilter(), pageRequest.getPageParam());
    }

    /**
     * @description 删除文件
     * @param fileId 文件ID
     * @return ResultDomain<Boolean>
     * @author AI Assistant
     * @since 2025-11-04
     */
    @DeleteMapping("/{fileId}")
    public ResultDomain<Boolean> deleteFile(@PathVariable(name = "fileId") String fileId) {
        log.info("删除文件: fileId={}", fileId);
        return uploadFileService.deleteFile(fileId);
    }

    /**
     * @description 查询文件处理状态（从Dify同步）
     * @param fileId 文件ID
     * @return ResultDomain<TbAiUploadFile>
     * @author AI Assistant
     * @since 2025-11-04
     */
    @GetMapping("/{fileId}/status")
    public ResultDomain<TbAiUploadFile> getFileStatus(@PathVariable(name = "fileId") String fileId) {
        log.info("查询文件处理状态: fileId={}", fileId);
        return uploadFileService.getFileStatus(fileId);
    }

    /**
     * @description 同步文件状态
     * @param fileId 文件ID
     * @return ResultDomain<TbAiUploadFile>
     * @author AI Assistant
     * @since 2025-11-04
     */
    @PostMapping("/{fileId}/sync")
    public ResultDomain<TbAiUploadFile> syncFileStatus(@PathVariable(name = "fileId") String fileId) {
        log.info("同步文件状态: fileId={}", fileId);
        return uploadFileService.syncFileStatus(fileId);
    }

    /**
     * @description 批量同步知识库的所有文件状态
     * @param knowledgeId 知识库ID
     * @return ResultDomain<TbAiUploadFile>
     * @author AI Assistant
     * @since 2025-11-04
     */
    @PostMapping("/sync/knowledge/{knowledgeId}")
    public ResultDomain<TbAiUploadFile> syncKnowledgeFiles(@PathVariable(name = "knowledgeId") String knowledgeId) {
        log.info("批量同步知识库文件状态: knowledgeId={}", knowledgeId);
        return uploadFileService.syncKnowledgeFiles(knowledgeId);
    }
}
