package com.gxwebsoft.ai.controller;

import com.alibaba.fastjson.JSONObject;
import com.gxwebsoft.ai.dto.AuditContentRequest;
import com.gxwebsoft.ai.dto.export.InternalControlExportEntity;
import com.gxwebsoft.ai.service.AuditContent8InternalControlService;
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;

/**
 * 审计内容8控制器 - 单位层面财务管理内部控制测试表
 */
@Slf4j
@Tag(name = "审计内容8-内部控制测试")
@RestController
@RequestMapping("/api/ai/auditContent8")
public class AuditContent8Controller extends BaseAuditContentController {
    
    @Autowired
    private AuditContent8InternalControlService auditContent8InternalControlService;
    
    /**
     * 生成单位层面财务管理内部控制测试表数据
     */
    @Operation(summary = "生成单位层面财务管理内部控制测试表")
    @PostMapping("/generateInternalControlTable")
    public ApiResult<?> generateInternalControlTable(@RequestBody AuditContentRequest request, HttpServletRequest servletRequest) {
        return generateTableData(request, servletRequest.getRequestURI(), 
            (params) -> auditContent8InternalControlService.generateInternalControlTableData(
                params.knowledgeBaseId, params.libraryKbIds, params.projectLibrary,
                params.username, params.history, params.suggestion
            ));
    }
    
    /**
     * 导出单位层面财务管理内部控制测试表到Excel
     */
    @Operation(summary = "导出单位层面财务管理内部控制测试表到Excel")
    @PostMapping("/exportInternalControlTable")
    public void exportInternalControlTable(@RequestBody Map<String, Object> request, HttpServletResponse response) {
        exportToExcel(request, response, "单位层面财务管理内部控制测试表", 
            this::convertToInternalControlEntityList, InternalControlExportEntity.class);
    }
    
    // ========== 数据转换方法 ==========
    
    private List<InternalControlExportEntity> convertToInternalControlEntityList(List<Map<String, Object>> originalData) {
        return originalData.stream().map(this::convertToInternalControlEntity).collect(Collectors.toList());
    }
    
    private InternalControlExportEntity convertToInternalControlEntity(Map<String, Object> item) {
        InternalControlExportEntity entity = new InternalControlExportEntity();
        entity.setIndex(getStringValue(item, "index"));
        entity.setControlLink(getStringValue(item, "controlLink"));
        entity.setControlRequirement(getStringValue(item, "controlRequirement"));
        entity.setControlActivity(getStringValue(item, "controlActivity"));
        entity.setControlPosition(getStringValue(item, "controlPosition"));
        entity.setTestSteps(getStringValue(item, "testSteps"));
        entity.setCheckEvidence(getStringValue(item, "checkEvidence"));
        entity.setTestResult(getStringValue(item, "testResult"));
        entity.setWorkPaperIndex(formatWorkPaperIndex(item.get("workPaperIndex")));
        return entity;
    }
}