"""
Quick connectivity test — run this before testing with Claude Desktop.
Usage: python pyapi/test_connection.py
"""

import sys
import os
sys.path.insert(0, os.path.dirname(__file__))

import httpx
from dotenv import load_dotenv

load_dotenv(dotenv_path=os.path.join(os.path.dirname(__file__), ".env"))

BASE_URL = os.getenv("LARAVEL_BASE_URL")
TOKEN    = os.getenv("LARAVEL_MCP_TOKEN")

ENDPOINTS = [
    "season-summary",
    "stock-alerts",
    "pending-approvals",
    "overdue-collections",
]

print(f"\nTarget: {BASE_URL}/api/v1/mcp/")
print(f"Token : {TOKEN[:8]}...{TOKEN[-4:]}\n")
print("-" * 55)

all_ok = True
for ep in ENDPOINTS:
    url = f"{BASE_URL}/api/v1/mcp/{ep}"
    try:
        r = httpx.get(
            url,
            headers={"Authorization": f"Bearer {TOKEN}", "Accept": "application/json"},
            timeout=10.0,
            verify=False,
        )
        status = "✓ OK" if r.status_code == 200 else f"✗ HTTP {r.status_code}"
        if r.status_code != 200:
            all_ok = False
        print(f"  [{r.status_code}] {status:12} /api/v1/mcp/{ep}")
    except Exception as e:
        print(f"  [ERR] ✗ FAIL       /api/v1/mcp/{ep}  →  {e}")
        all_ok = False

print("-" * 55)
print("\n✓ All endpoints reachable — ready for MCP Inspector\n" if all_ok else
      "\n✗ Fix the errors above before testing with Claude Desktop\n")
