package com.yifu.cloud.plus.v1.ekp.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yifu.cloud.plus.v1.ekp.config.WxConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author fxj
 * @Date 2025/9/18
 * @Description
 * @Version 1.0
 */
@Service
public class SmartSheetDebugService {

    @Autowired
    private WeComSmartSheetDebugger debugger;

    /**
     * 调试企业微信智能表格记录添加问题
     *
     */
    public void debugInsertIssue(WxConfig wxConfig) {
		String corpId = "wwbcb090af0dfe50e5";
		String corpSecret = "16kqEL_eU-ARwYyqLgEBWHgxm8gXVnkzv_eJMLy9Np";
		String docId = "dcTSMCj4lWV9MEP7lquZacOY8WS4g-iHxP_jVYA02tdYwb2YYc3BpDR8NrFWkEngHUg8abET4eTIpo9Zv-571NEA";
		String sheetId = "q979lj";

        System.out.println("=== 开始调试企业微信智能表格记录添加问题 ===");

        // 1. 获取并打印表格字段信息
        System.out.println("=== 表格字段信息 ===");
        List<WeComSmartSheetDebugger.FieldInfo> fields = debugger.getSheetFields(wxConfig,corpId, corpSecret, docId, sheetId);
        if (fields.isEmpty()) {
            System.out.println("未能获取到表格字段信息，请检查参数是否正确");
            return;
        }

        for (WeComSmartSheetDebugger.FieldInfo field : fields) {
            System.out.println("字段ID: " + field.getFieldId() +
                    ", 字段标题: '" + field.getTitle() + "'" +
                    ", 字段类型: " + field.getType());
        }
// 创建数据
		List<Map<String, Object>> records = new ArrayList<>();

// 第一条记录
		Map<String, Object> record1 = new HashMap<>();
		record1.put("姓名", "文本内容1");
		record1.put("部门", "文本内容2");
		record1.put("年龄", "文本内容3");
		records.add(record1);

// 第二条记录
		Map<String, Object> record2 = new HashMap<>();
		record2.put("姓名", "文本内容1");
		record2.put("部门", "文本内容2");
		record2.put("年龄", "文本内容3");
		records.add(record2);



       /* // 2. 准备要插入的数据（示例数据，请根据实际情况修改）
        List<Map<String, String>> records = new ArrayList<>();
        Map<String, Object> record = new HashMap<>();
        Map<String, Object> values = new HashMap<>();

		List<Map<String, String>> fieldContent = new ArrayList<>();
		Map<String, String> textContent = new HashMap<>();
		textContent.put("type", "text");
		textContent.put("text", "张三");
		fieldContent.add(textContent);
		textContent = new HashMap<>();
		textContent.put("type", "text");
		textContent.put("text", "李四");
		fieldContent.add(textContent);
        // 注意：这里的字段名称必须与表格中的完全一致（包括大小写和空格）
        values.put("姓名", fieldContent);
		fieldContent = new ArrayList<>();
		record = new HashMap<>();
		values = new HashMap<>();
		textContent = new HashMap<>();
		textContent.put("type", "text");
		textContent.put("text", "技术部");
		fieldContent.add(textContent);
		textContent = new HashMap<>();
		textContent.put("type", "text");
		textContent.put("text", "产品部");
		fieldContent.add(textContent);
		values.put("部门", fieldContent);
		record.put("values", values);
		records.add(record);*/

		/*fieldContent = new ArrayList<>();
		record = new HashMap<>();
		values = new HashMap<>();
		textContent = new HashMap<>();
		textContent.put("type", "text");
		textContent.put("text", "技术部");
		fieldContent.add(textContent);
		textContent = new HashMap<>();
		textContent.put("type", "text");
		textContent.put("text", "产品部");
		fieldContent.add(textContent);
		values.put("部门", fieldContent);
		record.put("values", values);
		records.add(record);*/



        // 3. 验证字段匹配性
        System.out.println("\n=== 字段匹配性验证 ===");
        List<String> mismatches = debugger.validateFields(wxConfig,corpId, corpSecret, docId, sheetId, records);
        if (mismatches.isEmpty()) {
            System.out.println("所有字段都匹配");
        } else {
            System.out.println("以下字段不匹配:");
            for (String mismatch : mismatches) {
                System.out.println("- " + mismatch);
            }
            System.out.println("请根据以上信息修正数据后重试");
            return;
        }

        // 4. 测试插入数据并查看详细响应
        System.out.println("\n=== 测试数据插入 ===");
        String response = debugger.testInsertData(wxConfig,corpId, corpSecret, docId, sheetId, records);
        System.out.println("API响应: " + response);

        System.out.println("\n=== 调试完成 ===");
    }
}