"""
Test: Verify Langfuse feedback scores (simplified)
"""
import os, time, traceback

os.environ["LANGFUSE_SECRET_KEY"] = "sk-lf-bd0c7202-a7f4-4399-a36e-65ebe2e35104"
os.environ["LANGFUSE_PUBLIC_KEY"] = "pk-lf-2bb249b1-77e3-4309-8954-312b8fb2fff9"
os.environ["LANGFUSE_HOST"] = "http://172.16.2.207:3009"

from langfuse import Langfuse

try:
    print("=" * 50)
    print("TEST 1: Ket noi Langfuse")
    langfuse = Langfuse()
    auth_ok = langfuse.auth_check()
    print(f"Auth check: {auth_ok}")

    if not auth_ok:
        print("FAILED - Auth failed!")
        exit(1)

    print("\nTEST 2: Tao trace + push feedback scores")
    trace_id = Langfuse.create_trace_id()
    print(f"Trace ID: {trace_id}")

    # Create trace using create_trace method
    langfuse.trace(
        id=trace_id,
        name="feedback-test",
        user_id="test-feedback-user",
        tags=["feedback-test"],
        input={"query": "Tim ao polo nam"},
        output={"ai_response": "Da, em tim thay 3 san pham..."},
    )
    print("Trace created OK!")

    print("\nTEST 3: Push LIKE score")
    langfuse.score(
        trace_id=trace_id,
        name="user-feedback",
        value=1,
        data_type="NUMERIC",
        comment="San pham dung y toi!",
    )
    print("LIKE score pushed!")

    print("\nTEST 4: Push DISLIKE + complaint")
    trace_id_2 = Langfuse.create_trace_id()
    langfuse.trace(
        id=trace_id_2,
        name="feedback-test-dislike",
        user_id="test-feedback-user",
        tags=["feedback-test"],
        input={"query": "Ao khoac nu size L"},
        output={"ai_response": "Da em tim thay..."},
    )
    langfuse.score(
        trace_id=trace_id_2,
        name="user-feedback",
        value=0,
        data_type="NUMERIC",
        comment="San pham sai gia!",
    )
    print("DISLIKE score pushed!")

    print("\nTEST 5: Push AI classification")
    langfuse.score(
        trace_id=trace_id_2,
        name="feedback-category",
        value="du_lieu_sai",
        data_type="CATEGORICAL",
        comment="AI classified: gia khong khop",
    )
    langfuse.score(
        trace_id=trace_id_2,
        name="feedback-processed",
        value=0,
        data_type="BOOLEAN",
        comment="Chua xu ly",
    )
    print("Classification scores pushed!")

    print("\nTEST 6: Flush")
    langfuse.flush()
    print("Flushed OK!")

    time.sleep(3)

    print("\nTEST 7: Fetch scores back")
    try:
        scores = langfuse.api.score.list()
        found = [s for s in scores.data if s.trace_id in (trace_id, trace_id_2)]
        print(f"Found {len(found)} scores for test traces:")
        for s in found:
            print(f"  {s.name}: {s.value} ({s.data_type}) | {s.comment}")
    except Exception as e2:
        print(f"Fetch error (check Langfuse UI instead): {e2}")

    print("\n" + "=" * 50)
    print("RESULT: ALL TESTS PASSED!")
    print(f"Trace 1 (LIKE):    {trace_id}")
    print(f"Trace 2 (DISLIKE): {trace_id_2}")
    print(f"Check: http://172.16.2.207:3009")

except Exception as e:
    print(f"\nERROR: {e}")
    traceback.print_exc()
