#!/usr/bin/env python3
"""Test WhatsApp endpoints are properly registered"""

import requests
import json

API_URL = "http://localhost:8000"

def test_api():
    print("🧪 Testing WhatsApp Integration...")
    
    headers = {
        "X-User-ID": "admin-user",
        "Content-Type": "application/json"
    }
    
    # Test root endpoint
    print("\n1. Testing root endpoint...")
    resp = requests.get(f"{API_URL}/")
    print(f"   ✅ {resp.status_code}: {resp.json()}")
    
    # Test if backend understands the new endpoint routes (just check they exist)
    print("\n2. Checking if WhatsApp endpoints are registered...")
    print("   ℹ️  Endpoints will return 404 without valid event_id, but shouldn't 500")
    
    # Try a test event creation first
    print("\n3. Creating test event...")
    event_data = {
        "name": "Test Wedding",
        "partner1_name": "David",
        "partner2_name": "Sarah",
        "venue": "Grand Hall",
        "event_time": "19:00"
    }
    
    resp = requests.post(
        f"{API_URL}/events",
        json=event_data,
        headers=headers
    )
    
    if resp.status_code == 201 or resp.status_code == 200:
        event = resp.json()
        event_id = event.get('id')
        print(f"   ✅ Event created: {event_id}")
        
        # Now test WhatsApp endpoints exist
        print(f"\n4. Testing WhatsApp endpoints with event {event_id}...")
        
        # Test single guest endpoint (should 404 for non-existent guest)
        resp = requests.post(
            f"{API_URL}/events/{event_id}/guests/00000000-0000-0000-0000-000000000000/whatsapp/invite",
            json={},
            headers=headers
        )
        if resp.status_code == 404:
            print(f"   ✅ Single-guest endpoint registered (404 for missing guest is expected)")
        else:
            print(f"   ❓ Status {resp.status_code}: {resp.json()}")
        
        # Test bulk endpoint (should work with empty list)
        resp = requests.post(
            f"{API_URL}/events/{event_id}/whatsapp/invite",
            json={"guest_ids": []},
            headers=headers
        )
        if resp.status_code >= 200 and resp.status_code < 500:
            print(f"   ✅ Bulk-send endpoint registered (status {resp.status_code})")
        else:
            print(f"   ❌ Endpoint error: {resp.status_code}")
            
    else:
        print(f"   ❌ Failed to create event: {resp.status_code}")
        print(f"      {resp.json()}")
    
    print("\n✅ API test complete!")

if __name__ == "__main__":
    test_api()
