import requests
import sys

BASE_URL = "http://127.0.0.1:5000"

def test_route(route, description):
    print(f"Testing {description} ({route})...", end=" ")
    try:
        response = requests.get(f"{BASE_URL}{route}")
        if response.status_code == 200:
            print("OK")
            return True
        else:
            print(f"FAILED (Status: {response.status_code})")
            # Print first 500 chars of response if error
            print(response.text[:500])
            return False
    except requests.exceptions.ConnectionError:
        print("FAILED (Connection Error - Is server running?)")
        return False
    except Exception as e:
        print(f"FAILED ({e})")
        return False

def main():
    print("--- Smoke Test: Team Routes ---")
    
    # 1. Clubhouse
    if not test_route("/teams/", "Clubhouse Page"):
        sys.exit(1)
        
    # 2. Roster API
    print("Testing Roster API...", end=" ")
    try:
        response = requests.get(f"{BASE_URL}/teams/api/roster")
        if response.status_code == 200:
            data = response.json()
            if data.get('status') == 'success':
                print(f"OK (Team: {data.get('team', {}).get('name')})")
                
                # Check if roster has stats
                roster = data.get('roster', [])
                if roster:
                    p = roster[0]
                    # Check for L3 keys
                    if 'stats' in p and 'core_avg_rating' in p['stats']:
                         print(f"  - Verified L3 Stats Key 'core_avg_rating' present: {p['stats']['core_avg_rating']}")
                    else:
                         print(f"  - WARNING: L3 Stats Key 'core_avg_rating' MISSING in {p.get('stats', {}).keys()}")
                else:
                    print("  - Roster is empty (Warning only)")
                    
                # Get Lineup ID for Detail Page Test
                lineup_id = data.get('team', {}).get('id')
                if lineup_id:
                     test_route(f"/teams/{lineup_id}", f"Team Detail Page (ID: {lineup_id})")
            else:
                print("FAILED (API returned error status)")
        else:
            print(f"FAILED (Status: {response.status_code})")
    except Exception as e:
        print(f"FAILED ({e})")
        sys.exit(1)

if __name__ == "__main__":
    main()
