# Copyright (c) 2025 Patrick Motsch
# All rights reserved.
"""Test KPI path extraction"""
import json
import sys
import os

# Add gateway directory to path
_gateway_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
if _gateway_path not in sys.path:
    sys.path.insert(0, _gateway_path)

from modules.services.serviceAi.subJsonResponseHandling import JsonResponseHandler

# Test JSON matching the actual response
test_json = {
    "metadata": {
        "split_strategy": "single_document",
        "source_documents": [],
        "extraction_method": "ai_generation"
    },
    "documents": [
        {
            "id": "doc_1",
            "title": "Prime Numbers Table",
            "filename": "prime_numbers.json",
            "sections": [
                {
                    "id": "section_prime_numbers_table",
                    "content_type": "table",
                    "elements": [
                        {
                            "headers": ["Column 1", "Column 2"],
                            "rows": [
                                [2, 3, 5, 7, 11],
                                [13, 17, 19, 23, 29]
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

# Test path from KPI definition
path = "documents[0].sections[0].elements[0].rows"

print(f"Testing path: {path}")
print(f"JSON structure: documents[0].sections[0].elements[0].rows")
print()

try:
    value = JsonResponseHandler._extractValueByPath(test_json, path)
    print(f"✅ Extracted value: {type(value)}")
    print(f"   Value: {value}")
    
    if isinstance(value, list):
        count = len(value)
        print(f"   Count: {count}")
    else:
        print(f"   Not a list!")
        
except Exception as e:
    print(f"❌ Error: {e}")
    import traceback
    traceback.print_exc()

