package com.gxwebsoft.ai.controller;

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

/**
 * 审计内容4控制器 - 目标责任制完成情况审计
 */
@Slf4j
@Tag(name = "审计内容4-目标责任制完成情况")
@RestController
@RequestMapping("/api/ai/auditContent4")
public class AuditContent4Controller extends BaseAuditContentController {
    
    @Autowired
    private AuditContent4TargetService auditContent4TargetService;
    
    /**
     * 生成目标责任制完成情况审计表数据
     */
    @Operation(summary = "生成目标责任制完成情况审计表")
    @PostMapping("/generateTargetTable")
    public ApiResult<?> generateTargetTable(@RequestBody AuditContentRequest request, HttpServletRequest servletRequest) {
        return generateTableData(request, servletRequest.getRequestURI(), 
            (params) -> auditContent4TargetService.generateTargetAuditTableData(
                params.knowledgeBaseId, params.libraryKbIds, params.projectLibrary,
                params.username, params.history, params.suggestion
            ));
    }
    
    /**
     * 导出目标责任制完成情况审计表到Excel
     */
    @Operation(summary = "导出目标责任制完成情况审计表到Excel")
    @PostMapping("/exportTargetTable")
    public void exportTargetTable(@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");
        
        List<TargetAuditExportEntity> exportData = convertToExportEntityList(dataList);
        
        String fileName = "目标责任制完成情况审计表_" + (companyName != null ? companyName : "未知公司");
        String title = companyName != null ? companyName + " - 目标责任制完成情况审计表" : "目标责任制完成情况审计表";
        String sheetName = "目标责任审计";
        
        ExcelExportTool.exportExcel(exportData, TargetAuditExportEntity.class, fileName, sheetName, title, response);
    }
    
    /**
     * 数据转换
     */
    private List<TargetAuditExportEntity> convertToExportEntityList(List<Map<String, Object>> originalData) {
        return originalData.stream().map(this::convertToExportEntity).collect(Collectors.toList());
    }
    
    private TargetAuditExportEntity convertToExportEntity(Map<String, Object> item) {
        TargetAuditExportEntity entity = new TargetAuditExportEntity();
        entity.setIndex(getStringValue(item, "index"));
        entity.setYear(getStringValue(item, "year"));
        entity.setSuperiorFile(getStringValue(item, "superiorFile"));
        entity.setSuperiorCompletion(getStringValue(item, "superiorCompletion"));
        entity.setSuperiorReason(getStringValue(item, "superiorReason"));
        entity.setSelfPlan(getStringValue(item, "selfPlan"));
        entity.setSelfCompletion(getStringValue(item, "selfCompletion"));
        entity.setSelfReason(getStringValue(item, "selfReason"));
        entity.setRemark(getStringValue(item, "remark"));
        entity.setWorkPaperIndex(formatWorkPaperIndex(item.get("workPaperIndex")));
        return entity;
    }
}