#!/bin/bash

# NER 服务端到端测试脚本
# 测试流程：NER 服务健康检查 -> 实体提取 -> 关系抽取 -> 文档 NER

set -e

# 配置
NER_SERVICE_URL="${NER_SERVICE_URL:-http://localhost:8001}"
JAVA_SERVICE_URL="${JAVA_SERVICE_URL:-http://localhost:5232}"

echo "=========================================="
echo "NER 服务端到端测试"
echo "=========================================="
echo "NER 服务地址: $NER_SERVICE_URL"
echo "Java 服务地址: $JAVA_SERVICE_URL"
echo ""

# 测试 1: Python NER 服务健康检查
echo ">> 测试 1: Python NER 服务健康检查"
response=$(curl -s -X GET "$NER_SERVICE_URL/health")
echo "响应: $response"
if echo "$response" | grep -q '"status":"ok"'; then
    echo "✅ Python NER 服务健康检查通过"
else
    echo "❌ Python NER 服务健康检查失败"
    exit 1
fi
echo ""

# 测试 2: 实体提取
echo ">> 测试 2: 实体提取"
response=$(curl -s -X POST "$NER_SERVICE_URL/ner/extract" \
    -H "Content-Type: application/json" \
    -d '{
        "documentId": "test-doc-001",
        "text": "2024年5月15日，成都检测公司在成都市高新区完成了环境监测项目的检测工作，项目负责人张经理使用了噪音检测设备进行测量，测量结果显示噪音值为50分贝。",
        "extractRelations": true
    }')
echo "响应: $response"
if echo "$response" | grep -q '"success":true'; then
    entity_count=$(echo "$response" | grep -o '"entityCount":[0-9]*' | grep -o '[0-9]*')
    relation_count=$(echo "$response" | grep -o '"relationCount":[0-9]*' | grep -o '[0-9]*')
    echo "✅ 实体提取成功: entityCount=$entity_count, relationCount=$relation_count"
else
    echo "❌ 实体提取失败"
    exit 1
fi
echo ""

# 测试 3: 关系抽取
echo ">> 测试 3: 关系抽取"
response=$(curl -s -X POST "$NER_SERVICE_URL/ner/relations" \
    -H "Content-Type: application/json" \
    -d '{
        "documentId": "test-doc-002",
        "text": "成都检测公司负责环境监测项目",
        "entities": [
            {
                "name": "成都检测公司",
                "type": "ORG",
                "value": "成都检测公司",
                "position": {"charStart": 0, "charEnd": 6, "line": 1},
                "tempId": "e1"
            },
            {
                "name": "环境监测项目",
                "type": "PROJECT",
                "value": "环境监测项目",
                "position": {"charStart": 8, "charEnd": 14, "line": 1},
                "tempId": "e2"
            }
        ]
    }')
echo "响应: $response"
if echo "$response" | grep -q '"success":true'; then
    echo "✅ 关系抽取成功"
else
    echo "❌ 关系抽取失败"
    exit 1
fi
echo ""

# 测试 4: Java NER 服务健康检查（如果 Java 服务已启动）
echo ">> 测试 4: Java 服务健康检查"
if curl -s -f "$JAVA_SERVICE_URL/actuator/health" > /dev/null 2>&1; then
    response=$(curl -s "$JAVA_SERVICE_URL/actuator/health")
    echo "响应: $response"
    echo "✅ Java 服务健康检查通过"
    
    # 测试 5: 通过 Java 服务调用 NER
    echo ""
    echo ">> 测试 5: 通过 Java 服务调用 NER"
    response=$(curl -s -X POST "$JAVA_SERVICE_URL/api/ner/extract" \
        -H "Content-Type: application/json" \
        -d '{
            "documentId": "test-doc-003",
            "text": "2024年6月1日，湖北环保局在武汉市武昌区开展了水质检测工作。"
        }')
    echo "响应: $response"
    if echo "$response" | grep -q '"code":200'; then
        echo "✅ 通过 Java 服务调用 NER 成功"
    else
        echo "⚠️  通过 Java 服务调用 NER 可能失败（请检查日志）"
    fi
else
    echo "⚠️  Java 服务未启动，跳过 Java 服务测试"
fi
echo ""

echo "=========================================="
echo "测试完成！"
echo "=========================================="
