#!/bin/bash
# Deploy recall endpoint fix to RMM server

echo "[1/3] Copying updated conversation_contexts.py to RMM server..."
scp /d/ClaudeTools/api/routers/conversation_contexts.py 172.16.3.30:/tmp/conversation_contexts.py

echo "[2/3] Moving file to production location..."
ssh 172.16.3.30 "sudo mv /tmp/conversation_contexts.py /opt/claudetools/api/routers/conversation_contexts.py && sudo chown claudetools:claudetools /opt/claudetools/api/routers/conversation_contexts.py"

echo "[3/3] Restarting API service..."
ssh 172.16.3.30 "sudo systemctl restart claudetools-api && sleep 2 && sudo systemctl status claudetools-api --no-pager | head -15"

echo ""
echo "[DONE] Deployment complete. Testing API..."

# Test the API
python - <<'PYTEST'
import requests
import json

jwt_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJpbXBvcnQtc2NyaXB0Iiwic2NvcGVzIjpbImFkbWluIiwiaW1wb3J0Il0sImV4cCI6MTc3MTI3NTEyOX0.-DJF50tq0MaNwVQBdO7cGYNuO5pQuXte-tTj5DpHi2U"

response = requests.get(
    "http://172.16.3.30:8001/api/conversation-contexts/recall",
    headers={"Authorization": f"Bearer {jwt_token}"},
    params={"search_term": "dataforth", "limit": 2}
)

print(f"\n[TEST] API Status: {response.status_code}")
data = response.json()

if "contexts" in data:
    print(f"[SUCCESS] Recall endpoint updated!")
    print(f"Total: {data['total']}, Returned: {len(data['contexts'])}")
    for ctx in data['contexts']:
        print(f"  - {ctx['title'][:50]}")
else:
    print(f"[WARNING] Still old format")
    print(json.dumps(data, indent=2)[:200])
PYTEST
