package com.gxwebsoft.ai.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.ai.dto.AuditContentRequest;
import com.gxwebsoft.ai.dto.export.EightRegExportEntity;
import com.gxwebsoft.ai.dto.export.ExpenseExportEntity;
import com.gxwebsoft.ai.dto.export.LeaderListExportEntity;
import com.gxwebsoft.ai.entity.AiCloudDoc;
import com.gxwebsoft.ai.entity.AiCloudFile;
import com.gxwebsoft.ai.service.AiHistoryService;
import com.gxwebsoft.ai.utils.ExcelExportTool;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.pwl.entity.PwlProjectLibrary;
import com.gxwebsoft.pwl.service.PwlProjectLibraryService;
import com.gxwebsoft.ai.service.AiCloudDocService;
import com.gxwebsoft.ai.service.AiCloudFileService;
import com.gxwebsoft.ai.service.AuditContent1EightRegService;
import com.gxwebsoft.ai.service.AuditContent1ExpenseService;
import com.gxwebsoft.ai.service.AuditContent1LeaderListService;
import com.gxwebsoft.ai.service.KnowledgeBaseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 审计内容1控制器 - 八项规定对比分析
 */
@Slf4j
@Tag(name = "审计内容1-八项规定")
@RestController
@RequestMapping("/api/ai/auditContent1")
public class AuditContent1Controller extends BaseController {
    
	@Autowired
	private AuditContent1LeaderListService auditContent1LeaderListService;
    
    @Autowired
    private AuditContent1ExpenseService auditContent1ExpenseService;
	
    @Autowired
    private AuditContent1EightRegService auditContent1EightRegService;
    
    @Autowired
    private AiCloudDocService aiCloudDocService;
    
    @Autowired
    private AiCloudFileService aiCloudFileService;
    
    @Autowired
    private KnowledgeBaseService knowledgeBaseService;
    
    @Autowired
    private PwlProjectLibraryService pwlProjectLibraryService;
    
    @Autowired
    private AiHistoryService aiHistoryService;
    
    // 历史记录有效期（分钟）
    private static final int HISTORY_EXPIRE_MINUTES = 10;
    
    /**
     * 生成领导班子名单数据
     */
    @Operation(summary = "生成领导班子名单")
    @PostMapping("/generateLeaderListTable")
    public ApiResult<?> generateLeaderListTable(@RequestBody AuditContentRequest request, HttpServletRequest servletRequest) {
        return generateTableData(request, servletRequest.getRequestURI(), 
            (params) -> auditContent1LeaderListService.generateLeaderListTableData(
                params.knowledgeBaseId, params.libraryKbIds, params.projectLibrary,
                params.username, params.history, params.suggestion
            ));
    }

    /**
     * 生成支出情况表数据
     */
    @Operation(summary = "生成支出情况表")
    @PostMapping("/generateExpenseTable")
    public ApiResult<?> generateExpenseTable(@RequestBody AuditContentRequest request, HttpServletRequest servletRequest) {
        return generateTableData(request, servletRequest.getRequestURI(), 
            (params) -> auditContent1ExpenseService.generateExpenseTableData(
                params.knowledgeBaseId, params.libraryKbIds, params.projectLibrary,
                params.username, params.history, params.suggestion
            ));
    }
    
    /**
     * 生成八项规定对比分析表数据
     */
    @Operation(summary = "生成八项规定对比分析表")
    @PostMapping("/generateEightRegTable")
    public ApiResult<?> generateEightRegTable(@RequestBody AuditContentRequest request, HttpServletRequest servletRequest) {
        return generateTableData(request, servletRequest.getRequestURI(), 
            (params) -> auditContent1EightRegService.generateEightRegTableData(
                params.knowledgeBaseId, params.libraryKbIds, params.projectLibrary,
                params.username, params.history, params.suggestion
            ));
    }
    
    /**
     * 通用的表格数据生成方法
     */
    private ApiResult<?> generateTableData(AuditContentRequest request, String interfaceName, Function<GenerateParams, JSONObject> generateFunction) {
        final User loginUser = getLoginUser();
        String requestHistory = request.getHistory();
        request.setHistory("");
        
        // 检查历史记录
        String requestHash = generateRequestHash(request, interfaceName);
        var history = aiHistoryService.getValidHistory(requestHash, interfaceName, HISTORY_EXPIRE_MINUTES);
        if (history != null) {
            log.info("返回历史数据，请求哈希: {}", requestHash);
            return success(JSONObject.parseObject(history.getResponseData()));
        }
        request.setHistory(requestHistory);
        
        String kbIdTmp = "";
        String libraryKbIds = "";
        
        try {
            // 创建临时知识库（如果需要）
            if (hasUploadedFiles(request)) {
                kbIdTmp = createTempKnowledgeBase(request);
            }
            
            // 查询项目库信息
            libraryKbIds = getLibraryKbIds(request.getLibraryIds());
            
            // 生成数据
            String knowledgeBaseId = getKnowledgeBaseId(kbIdTmp, request.getKbIds());
            GenerateParams params = new GenerateParams(knowledgeBaseId, libraryKbIds, request.getProjectLibrary(), loginUser.getUsername(), request.getHistory(), request.getSuggestion());
            
            JSONObject result = generateFunction.apply(params);
            
            if(result.getBoolean("success")) {
                // 保存到历史记录
                saveToHistory(request, interfaceName, requestHash, result, loginUser);
            }
            
            return success(result);
        } catch (Exception e) {
            log.error("生成八项规定表格数据失败，接口: {}", interfaceName, e);
            return fail("生成八项规定表格数据失败: " + e.getMessage());
        } finally {
            cleanupTempKnowledgeBase(kbIdTmp);
        }
    }
    
    /**
     * 生成请求哈希
     */
    private String generateRequestHash(AuditContentRequest request, String interfaceName) {
        String requestJson = JSONObject.toJSONString(request);
        return DigestUtil.md5Hex(interfaceName + ":" + requestJson);
    }
    
    /**
     * 保存到历史记录
     */
    private void saveToHistory(AuditContentRequest request, String interfaceName, String requestHash, JSONObject result, User loginUser) {
        try {
            aiHistoryService.saveHistory(requestHash, interfaceName, JSONObject.toJSONString(request), result.toJSONString(), loginUser.getUserId(), loginUser.getUsername(), loginUser.getTenantId());
        } catch (Exception e) {
            log.warn("保存历史记录失败", e);
        }
    }
    
    /**
     * 检查是否有上传的文件
     */
    private boolean hasUploadedFiles(AuditContentRequest request) {
        return !request.getDocList().isEmpty() || !request.getFileList().isEmpty();
    }
    
    /**
     * 获取知识库ID
     */
    private String getKnowledgeBaseId(String tempKbId, String requestKbIds) {
        return StrUtil.isNotBlank(tempKbId) ? tempKbId : requestKbIds;
    }
    
    /**
     * 获取项目库KB IDs
     */
    private String getLibraryKbIds(String libraryIds) {
        if (StrUtil.isBlank(libraryIds)) {
            return "";
        }
        List<String> idList = StrUtil.split(libraryIds, ',');
        List<PwlProjectLibrary> ret = pwlProjectLibraryService.list(new LambdaQueryWrapper<PwlProjectLibrary>().in(PwlProjectLibrary::getId, idList));
        return ret.stream().map(PwlProjectLibrary::getKbId).filter(StrUtil::isNotBlank).collect(Collectors.joining(","));
    }
    
    /**
     * 创建临时知识库并提交文档
     */
    private String createTempKnowledgeBase(AuditContentRequest request) {
        String kbIdTmp = knowledgeBaseService.createKnowledgeBaseTemp();
        // 收集文档ID
        Set<Integer> docIds = request.getDocList().stream().flatMap(docId -> aiCloudDocService.getSelfAndChildren(docId).stream()).map(AiCloudDoc::getId).collect(Collectors.toSet());
        // 查询相关文件
        List<AiCloudFile> fileList = getRelatedFiles(docIds, request.getFileList());
        // 提取文件ID并提交到知识库
        Set<String> kbFileIds = fileList.stream().map(AiCloudFile::getFileId).collect(Collectors.toSet());
        if (!kbFileIds.isEmpty()) {
            knowledgeBaseService.submitDocuments(kbIdTmp, new ArrayList<>(kbFileIds));
        }
        return kbIdTmp;
    }
    
    /**
     * 获取相关文件列表
     */
    private List<AiCloudFile> getRelatedFiles(Set<Integer> docIds, List<Integer> fileList) {
        LambdaQueryWrapper<AiCloudFile> queryWrapper = new LambdaQueryWrapper<AiCloudFile>()
                .in(!docIds.isEmpty(), AiCloudFile::getDocId, docIds)
                .or(!fileList.isEmpty())
                .in(!fileList.isEmpty(), AiCloudFile::getId, fileList);
        return aiCloudFileService.list(queryWrapper);
    }
    
    /**
     * 清理临时知识库
     */
    private void cleanupTempKnowledgeBase(String kbId) {
        if (StrUtil.isNotBlank(kbId)) {
            try {
                knowledgeBaseService.deleteIndex(kbId);
            } catch (Exception e) {
                log.warn("删除临时知识库失败: {}", kbId, e);
            }
        }
    }
    
    /**
     * 导出八项规定对比分析表到Excel
     */
    @Operation(summary = "导出八项规定对比分析表到Excel")
    @PostMapping("/exportEightRegTable")
    public void exportEightRegTable(@RequestBody Map<String, Object> request, HttpServletResponse response) {
        exportToExcel(request, response, "八项规定对比分析表", 
            this::convertToEightRegEntityList, EightRegExportEntity.class);
    }
    
    /**
     * 通用的Excel导出方法
     */
    private <T> void exportToExcel(Map<String, Object> request, HttpServletResponse response, 
                                  String sheetName, Function<List<Map<String, Object>>, List<T>> converter, 
                                  Class<T> entityClass) {
        List<Map<String, Object>> dataList = (List<Map<String, Object>>) request.get("data");
        String companyName = (String) request.get("companyName");
        
        List<T> exportData = converter.apply(dataList);
        
        String fileName = sheetName + "_" + (companyName != null ? companyName : "未知公司");
        String title = companyName != null ? companyName + " - " + sheetName : sheetName;
        
        ExcelExportTool.exportExcel(exportData, entityClass, fileName, sheetName, title, response);
    }
    
    /**
     * 参数包装类
     */
    private static class GenerateParams {
        final String knowledgeBaseId;
        final String libraryKbIds;
        final String projectLibrary;
        final String username;
        final String history;
        final String suggestion;
        
        GenerateParams(String knowledgeBaseId, String libraryKbIds, String projectLibrary, String username, String history, String suggestion) {
            this.knowledgeBaseId = knowledgeBaseId;
            this.libraryKbIds = libraryKbIds;
            this.projectLibrary = projectLibrary;
            this.username = username;
            this.history = history;
            this.suggestion = suggestion;
        }
    }
    
    // ========== 数据转换方法 ==========
    
    private List<EightRegExportEntity> convertToEightRegEntityList(List<Map<String, Object>> originalData) {
        return originalData.stream().map(this::convertToEightRegEntity).collect(Collectors.toList());
    }
    
    private EightRegExportEntity convertToEightRegEntity(Map<String, Object> item) {
        EightRegExportEntity entity = new EightRegExportEntity();
        entity.setTitle(getStringValue(item, "title"));
        entity.setContent(getStringValue(item, "content"));
        entity.setTestContent(getStringValue(item, "testContent"));
        entity.setResult(getStringValue(item, "result"));
        entity.setWorkPaperIndex(formatWorkPaperIndex(item.get("workPaperIndex")));
        return entity;
    }
    
    private String getStringValue(Map<String, Object> map, String key) {
        Object value = map.get(key);
        return value != null ? value.toString() : "";
    }
    
    private String formatWorkPaperIndex(Object workPaperIndex) {
        if (workPaperIndex == null) {
            return "";
        }
        
        if (workPaperIndex instanceof List) {
            List<?> list = (List<?>) workPaperIndex;
            return String.join(", ", list.stream()
                    .map(Object::toString)
                    .collect(Collectors.toList()));
        }
        
        return workPaperIndex.toString();
    }
    
    
    /**
     * 导出领导班子名单到Excel
     */
    @Operation(summary = "导出领导班子名单到Excel")
    @PostMapping("/exportLeaderListTable")
    public void exportLeaderListTable(@RequestBody Map<String, Object> request, HttpServletResponse response) {
        exportToExcel(request, response, "领导班子名单", this::convertToLeaderListEntityList, LeaderListExportEntity.class);
    }

    // 添加数据转换方法
    private List<LeaderListExportEntity> convertToLeaderListEntityList(List<Map<String, Object>> originalData) {
        return originalData.stream().map(this::convertToLeaderListEntity).collect(Collectors.toList());
    }

    private LeaderListExportEntity convertToLeaderListEntity(Map<String, Object> item) {
        LeaderListExportEntity entity = new LeaderListExportEntity();
        entity.setUnit(getStringValue(item, "unit"));
        entity.setName(getStringValue(item, "name"));
        entity.setDepartment(getStringValue(item, "department"));
        entity.setPartyPosition(getStringValue(item, "partyPosition"));
        entity.setAdminPosition(getStringValue(item, "adminPosition"));
        entity.setTenurePeriod(getStringValue(item, "tenurePeriod"));
        entity.setMainResponsibilities(getStringValue(item, "mainResponsibilities"));
        entity.setRemark(getStringValue(item, "remark"));
        entity.setWorkPaperIndex(formatWorkPaperIndex(item.get("workPaperIndex")));
        return entity;
    }
    

    /**
     * 导出支出情况表到Excel
     */
    @Operation(summary = "导出支出情况表到Excel")
    @PostMapping("/exportExpenseTable")
    public void exportExpenseTable(@RequestBody Map<String, Object> request, HttpServletResponse response) {
        exportToExcel(request, response, "支出情况表", 
            this::convertToExpenseEntityList, ExpenseExportEntity.class);
    }

    // ========== 数据转换方法 ==========

    private List<ExpenseExportEntity> convertToExpenseEntityList(List<Map<String, Object>> originalData) {
        return originalData.stream().map(this::convertToExpenseEntity).collect(Collectors.toList());
    }

    private ExpenseExportEntity convertToExpenseEntity(Map<String, Object> item) {
        ExpenseExportEntity entity = new ExpenseExportEntity();
        entity.setExpenseType(getStringValue(item, "expenseType"));
        entity.setYear(getStringValue(item, "year"));
        entity.setFinalStatementAmount(getStringValue(item, "finalStatementAmount"));
        entity.setInitialBudgetAmount(getStringValue(item, "initialBudgetAmount"));
        entity.setChangePercentage(getStringValue(item, "changePercentage"));
        entity.setBudgetRatio(getStringValue(item, "budgetRatio"));
        entity.setRemark(getStringValue(item, "remark"));
        entity.setDataSource(getStringValue(item, "dataSource"));
        entity.setWorkPaperIndex(formatWorkPaperIndex(item.get("workPaperIndex")));
        return entity;
    }
}