package com.gxwebsoft.ai.controller;

import com.alibaba.fastjson.JSONObject;
import com.gxwebsoft.ai.dto.AuditContentRequest;
import com.gxwebsoft.ai.dto.export.HistoryTableExportEntity;
import com.gxwebsoft.ai.service.AuditContent11HistoryService;
import com.gxwebsoft.ai.utils.ExcelExportTool;
import com.gxwebsoft.common.core.web.ApiResult;

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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 审计内容11控制器 - 历史审计问题整改表
 */
@Slf4j
@Tag(name = "审计内容11-历史审计问题整改")
@RestController
@RequestMapping("/api/ai/auditContent11")
public class AuditContent11Controller extends BaseAuditContentController {
    
    @Autowired
    private AuditContent11HistoryService auditContent11HistoryService;
    
    /**
     * 生成历史审计问题整改表数据
     */
    @Operation(summary = "生成历史审计问题整改表")
    @PostMapping("/generateHistoryTable")
    public ApiResult<?> generateHistoryTable(@RequestBody AuditContentRequest request, HttpServletRequest servletRequest) {
        return generateTableData(request, servletRequest.getRequestURI(), 
            (params) -> auditContent11HistoryService.generateHistoryTableData(
                params.knowledgeBaseId, params.libraryKbIds, params.projectLibrary,
                params.username, params.history, params.suggestion
            ));
    }
    
    /**
     * 导出历史审计问题整改表到Excel
     */
    @Operation(summary = "导出历史审计问题整改表到Excel")
    @PostMapping("/exportHistoryTable")
    public void exportHistoryTable(@RequestBody Map<String, Object> request, HttpServletResponse response) {
        List<Map<String, Object>> dataList = (List<Map<String, Object>>) request.get("data");
        String companyName = (String) request.get("companyName");
        String auditTime = (String) request.get("auditTime");
        
        List<HistoryTableExportEntity> exportData = convertToHistoryTableEntityList(dataList);
        
        String fileName = "历史审计问题整改表_" + (companyName != null ? companyName : "未知公司");
        String sheetName = "历史审计问题整改表";
        String title = companyName != null ? 
            companyName + " - 历史审计问题整改表" + (auditTime != null ? "（" + auditTime + "）" : "") : 
            "历史审计问题整改表";
        
        ExcelExportTool.exportExcel(exportData, HistoryTableExportEntity.class, fileName, sheetName, title, response);
    }
    
    // ========== 数据转换方法 ==========
    
    private List<HistoryTableExportEntity> convertToHistoryTableEntityList(List<Map<String, Object>> originalData) {
        return originalData.stream()
            .map(this::convertToHistoryTableEntity)
            .collect(Collectors.toList());
    }
    
    private HistoryTableExportEntity convertToHistoryTableEntity(Map<String, Object> item) {
        HistoryTableExportEntity entity = new HistoryTableExportEntity();
        entity.setIndex(getStringValue(item, "index"));
        entity.setAuditYear(getStringValue(item, "auditYear"));
        entity.setAuditType(getStringValue(item, "auditType"));
        entity.setProblemFound(getStringValue(item, "problemFound"));
        entity.setRectificationRequirement(getStringValue(item, "rectificationRequirement"));
        entity.setRectificationMeasures(getStringValue(item, "rectificationMeasures"));
        entity.setRectificationStatus(getStringValue(item, "rectificationStatus"));
        entity.setCompletionDate(getStringValue(item, "completionDate"));
        entity.setResponsiblePerson(getStringValue(item, "responsiblePerson"));
        entity.setRemark(getStringValue(item, "remark"));
        entity.setWorkPaperIndex(formatWorkPaperIndex(item.get("workPaperIndex")));
        return entity;
    }
}