package com.gxwebsoft.ai.controller;

import com.alibaba.fastjson.JSONObject;
import com.gxwebsoft.ai.dto.AuditContentRequest;
import com.gxwebsoft.ai.dto.export.TripleOneExportEntity;
import com.gxwebsoft.ai.dto.export.DecisionTableExportEntity;
import com.gxwebsoft.ai.service.AuditContent3TripleService;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.ai.service.AuditContent3DecisionService;
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;

/**
 * 审计内容3控制器 - 三重一大制度对比分析 & 重大经济决策调查表
 */
@Slf4j
@Tag(name = "审计内容3-三重一大制度")
@RestController
@RequestMapping("/api/ai/auditContent3")
public class AuditContent3Controller extends BaseAuditContentController {
    
    @Autowired
    private AuditContent3TripleService auditContent3TripleService;
    
    @Autowired
    private AuditContent3DecisionService auditContent3DecisionService;
    
    /**
     * 生成三重一大制度对比分析表数据
     */
    @Operation(summary = "生成三重一大制度对比分析表")
    @PostMapping("/generateTripleOneTable")
    public ApiResult<?> generateTripleOneTable(@RequestBody AuditContentRequest request, HttpServletRequest servletRequest) {
        return generateTableData(request, servletRequest.getRequestURI(), 
            (params) -> auditContent3TripleService.generateTripleOneTableData(
                params.knowledgeBaseId, params.libraryKbIds, params.projectLibrary,
                params.username, params.history, params.suggestion
            ));
    }
    
    /**
     * 生成重大经济决策调查表数据
     */
    @Operation(summary = "生成重大经济决策调查表")
    @PostMapping("/generateDecisionTable")
    public ApiResult<?> generateDecisionTable(@RequestBody AuditContentRequest request, HttpServletRequest servletRequest) {
        return generateTableData(request, servletRequest.getRequestURI(), 
            (params) -> auditContent3DecisionService.generateDecisionTableData(
                params.knowledgeBaseId, params.libraryKbIds, params.projectLibrary,
                params.username, params.history, params.suggestion, request.getData()
            ));
    }
    
    /**
     * 导出三重一大制度对比分析表到Excel
     */
    @Operation(summary = "导出三重一大制度对比分析表到Excel")
    @PostMapping("/exportTripleOneTable")
    public void exportTripleOneTable(@RequestBody Map<String, Object> request, HttpServletResponse response) {
        exportToExcel(request, response, "三重一大制度对比分析表", 
            this::convertToTripleOneEntityList, TripleOneExportEntity.class);
    }
    
    /**
     * 导出重大经济决策调查表到Excel
     */
    @Operation(summary = "导出重大经济决策调查表到Excel")
    @PostMapping("/exportDecisionTable")
    public void exportDecisionTable(@RequestBody Map<String, Object> request, HttpServletResponse response) {
        exportToExcel(request, response, "重大经济决策调查表", 
            this::convertToDecisionTableEntityList, DecisionTableExportEntity.class);
    }
    
    // ========== 数据转换方法 ==========
    
    private List<TripleOneExportEntity> convertToTripleOneEntityList(List<Map<String, Object>> originalData) {
        return originalData.stream().map(this::convertToTripleOneEntity).collect(Collectors.toList());
    }
    
    private List<DecisionTableExportEntity> convertToDecisionTableEntityList(List<Map<String, Object>> originalData) {
        return originalData.stream().map(this::convertToDecisionTableEntity).collect(Collectors.toList());
    }
    
    private TripleOneExportEntity convertToTripleOneEntity(Map<String, Object> item) {
        TripleOneExportEntity entity = new TripleOneExportEntity();
        entity.setCategory(getStringValue(item, "category"));
        entity.setPolicyContent(getStringValue(item, "policyContent"));
        entity.setGroupSystem(getStringValue(item, "groupSystem"));
        entity.setCompanyFormulation(getStringValue(item, "companyFormulation"));
        entity.setCheckEvidence(getStringValue(item, "checkEvidence"));
        entity.setTestResult(getStringValue(item, "testResult"));
        entity.setWorkPaperIndex(formatWorkPaperIndex(item.get("workPaperIndex")));
        return entity;
    }
    
    private DecisionTableExportEntity convertToDecisionTableEntity(Map<String, Object> item) {
        DecisionTableExportEntity entity = new DecisionTableExportEntity();
        entity.setIndex(getStringValue(item, "index"));
        entity.setDecisionItem(getStringValue(item, "name"));
        entity.setMeetingTime(getStringValue(item, "meetingTime"));
        entity.setDecisionAmount(getStringValue(item, "decisionAmount"));
        entity.setProcedure(getStringValue(item, "procedure"));
        entity.setExecutionStatus(getStringValue(item, "executionStatus"));
        entity.setGood(getStringValue(item, "goods"));
        entity.setNormal(getStringValue(item, "normal"));
        entity.setBad(getStringValue(item, "bad"));
        return entity;
    }
}