import java.sql.*;
import org.json.*;
import java.nio.file.*;

public class NLQBatchTest {
    public static void main(String[] args) {
        System.out.println("=== NLQ 批量测试 ===\n");

        try {
            // 读取配置文件
            String configPath = "nlqtest/nlqtest-config.json";
            String content = new String(Files.readAllBytes(Paths.get(configPath)));
            JSONObject config = new JSONObject(content);

            JSONObject jdbc = config.getJSONObject("jdbc");
            String driver = jdbc.getString("driver");
            String url = jdbc.getString("url");
            String user = jdbc.optString("user", "root");
            String password = jdbc.optString("password", "root");
            int maxRows = config.optInt("maxRows", 3);

            System.out.println("驱动: " + driver);
            System.out.println("URL: " + url);
            System.out.println("maxRows: " + maxRows);
            System.out.println();

            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url, user, password);
            Statement stmt = conn.createStatement();

            JSONArray testCases = config.getJSONArray("testCases");
            int total = 0, pass = 0, fail = 0;

            for (int i = 0; i < testCases.length(); i++) {
                JSONObject category = testCases.getJSONObject(i);
                String catName = category.getString("category");
                JSONArray cases = category.getJSONArray("cases");

                System.out.println("--- " + catName + " ---");

                for (int j = 0; j < cases.length(); j++) {
                    String nlq = cases.getString(j);
                    total++;
                    System.out.println("\n[" + total + "] " + nlq);

                    try {
                        // 第一步：解析规范文本
                        ResultSet rs = stmt.executeQuery("query " + nlq);
                        String mql = null;
                        String parseError = null;
                        if (rs.next()) {
                            JSONObject json = new JSONObject(rs.getString(1));
                            mql = json.optString("mql", "");
                            parseError = json.optString("error", "");
                        }
                        rs.close();

                        if (parseError != null && parseError.length() > 0) {
                            System.out.println("  [解析报错] " + parseError);
                            fail++;
                            continue;
                        }
                        if (mql == null || mql.length() == 0) {
                            System.out.println("  [解析失败] 未生成DQL");
                            fail++;
                            continue;
                        }

                        System.out.println("  DQL: " + mql);

                        // 第二步：执行 DQL
                        ResultSet drs = stmt.executeQuery(mql);
                        ResultSetMetaData meta = drs.getMetaData();
                        int cols = meta.getColumnCount();

                        StringBuilder header = new StringBuilder("  列: ");
                        for (int c = 1; c <= cols; c++) {
                            if (c > 1) header.append(" | ");
                            header.append(meta.getColumnLabel(c));
                        }
                        System.out.println(header.toString());

                        int rows = 0;
                        while (drs.next() && rows < maxRows) {
                            StringBuilder row = new StringBuilder("  行" + (rows + 1) + ": ");
                            for (int c = 1; c <= cols; c++) {
                                if (c > 1) row.append(" | ");
                                String val = drs.getString(c);
                                row.append(val == null ? "NULL" : val);
                            }
                            System.out.println(row.toString());
                            rows++;
                        }
                        if (rows == 0) {
                            System.out.println("  (空结果)");
                        } else {
                            System.out.println("  ...(前" + maxRows + "条)");
                        }
                        drs.close();
                        pass++;

                    } catch (Exception e) {
                        System.out.println("  [执行报错] " + e.getMessage());
                        fail++;
                    }
                }
                System.out.println();
            }

            System.out.println("=== 测试结束 ===");
            System.out.println("总计: " + total + ", 通过: " + pass + ", 失败: " + fail);

            conn.close();
        } catch (Exception e) {
            System.out.println("[致命报错] " + e.getMessage());
            e.printStackTrace();
        }
    }
}
