"""Push test feedback scores to Langfuse using REST API + verify"""
import requests, uuid, time, json

BASE = "http://172.16.2.207:3009"
AUTH = ("pk-lf-2bb249b1-77e3-4309-8954-312b8fb2fff9", "sk-lf-bd0c7202-a7f4-4399-a36e-65ebe2e35104")
trace_id = str(uuid.uuid4())
ts = time.strftime("%Y-%m-%dT%H:%M:%S.000Z")

print("Pushing feedback scores to Langfuse...")
r = requests.post(f"{BASE}/api/public/ingestion", auth=AUTH, timeout=10, json={"batch": [
    {"id": str(uuid.uuid4()), "type": "trace-create", "timestamp": ts, "body": {
        "id": trace_id, "name": "feedback-test", "input": {"query": "ao polo nam"}, "output": {"resp": "test"}}},
    {"id": str(uuid.uuid4()), "type": "score-create", "timestamp": ts, "body": {
        "id": f"{trace_id}-uf", "traceId": trace_id, "name": "user-feedback", "value": 0, "dataType": "NUMERIC", "comment": "Dislike"}},
    {"id": str(uuid.uuid4()), "type": "score-create", "timestamp": ts, "body": {
        "id": f"{trace_id}-fc", "traceId": trace_id, "name": "feedback-category", "value": "du_lieu_sai", "dataType": "CATEGORICAL", "comment": "Du lieu sai"}},
    {"id": str(uuid.uuid4()), "type": "score-create", "timestamp": ts, "body": {
        "id": f"{trace_id}-fs", "traceId": trace_id, "name": "feedback-severity", "value": "high", "dataType": "CATEGORICAL", "comment": "High severity"}},
    {"id": str(uuid.uuid4()), "type": "score-create", "timestamp": ts, "body": {
        "id": f"{trace_id}-fp", "traceId": trace_id, "name": "feedback-processed", "value": 0, "dataType": "BOOLEAN", "comment": "Pending"}},
]})
print(f"Status: {r.status_code}")
resp = r.json()
print(f"Successes: {len(resp.get('successes',[]))}, Errors: {len(resp.get('errors',[]))}")
if resp.get('errors'):
    for e in resp['errors']:
        print(f"  ERR: {json.dumps(e)[:200]}")

time.sleep(2)
r2 = requests.get(f"{BASE}/api/public/traces/{trace_id}", auth=AUTH, timeout=10)
if r2.status_code == 200:
    t = r2.json()
    print(f"\nTrace: {t['name']} | Scores: {len(t.get('scores',[]))}")
    for s in t.get('scores', []):
        print(f"  {s['name']:25s} = {str(s.get('value','')):15s} ({s.get('dataType','?')})")
    print(f"\nURL: {BASE}/project/cmk3i5l310008r407rsiib2h4/traces/{trace_id}")
    if len(t.get('scores',[])) >= 4:
        print("\nALL 4 SCORES VERIFIED!")
else:
    print(f"Trace fetch error: {r2.status_code}")
