# Copyright (c) 2025 Patrick Motsch
# All rights reserved.
"""Test full KPI extraction and validation flow"""
import json
import sys
import os
import pytest

# 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
from modules.datamodels.datamodelAi import JsonAccumulationState

# Load actual JSON response
json_file = os.path.join(
    os.path.dirname(__file__),
    "..", "..", "..", "local", "debug", "prompts",
    "20251130-211706-078-document_generation_response.txt"
)

if not os.path.exists(json_file):
    pytest.skip(f"Test data file not found: {json_file}", allow_module_level=True)

with open(json_file, 'r', encoding='utf-8') as f:
    content = f.read()

# Extract JSON
from modules.shared.jsonUtils import extractJsonString
extracted = extractJsonString(content)
parsedJson = json.loads(extracted)

# KPI definition from the response
kpiDefinitions = [{
    "id": "prime_numbers_count",
    "description": "Number of prime numbers generated and organized in the table",
    "jsonPath": "documents[0].sections[0].elements[0].rows",
    "targetValue": 4000
}]

print("="*60)
print("KPI EXTRACTION AND VALIDATION TEST")
print("="*60)

# Step 1: Initialize accumulation state with KPIs
accumulationState = JsonAccumulationState(
    accumulatedJsonString="",
    isAccumulationMode=True,
    lastParsedResult=None,
    allSections=[],
    kpis=[{**kpi, "currentValue": 0} for kpi in kpiDefinitions]
)

print(f"\nStep 1: Initialized KPIs")
for kpi in accumulationState.kpis:
    print(f"   KPI {kpi['id']}: currentValue={kpi.get('currentValue', 'N/A')}, targetValue={kpi.get('targetValue', 'N/A')}")

# Step 2: Extract KPI values from parsed JSON
print(f"\nStep 2: Extracting KPI values from JSON...")
updatedKpis = JsonResponseHandler.extractKpiValuesFromJson(
    parsedJson,
    accumulationState.kpis
)

print(f"   Extracted {len(updatedKpis)} KPIs")
for kpi in updatedKpis:
    print(f"   KPI {kpi['id']}: currentValue={kpi.get('currentValue', 'N/A')}, targetValue={kpi.get('targetValue', 'N/A')}")

# Step 3: Validate progression
print(f"\nStep 3: Validating KPI progression...")
shouldProceed, reason = JsonResponseHandler.validateKpiProgression(
    accumulationState,
    updatedKpis
)

print(f"   Result: shouldProceed={shouldProceed}, reason={reason}")

# Step 4: Check what's in accumulationState.kpis vs updatedKpis
print(f"\nStep 4: Comparing state...")
print(f"   accumulationState.kpis[0].currentValue = {accumulationState.kpis[0].get('currentValue', 'N/A')}")
print(f"   updatedKpis[0].currentValue = {updatedKpis[0].get('currentValue', 'N/A')}")

# Step 5: Check if we need to update accumulationState.kpis
print(f"\nStep 5: Updating accumulationState.kpis...")
accumulationState.kpis = updatedKpis
print(f"   Updated accumulationState.kpis[0].currentValue = {accumulationState.kpis[0].get('currentValue', 'N/A')}")

# Step 6: Validate again (should show progress)
print(f"\nStep 6: Validating again after update...")
shouldProceed2, reason2 = JsonResponseHandler.validateKpiProgression(
    accumulationState,
    updatedKpis
)
print(f"   Result: shouldProceed={shouldProceed2}, reason={reason2}")

