#!/usr/bin/env python3
"""
Test script to verify webhook routing order and ensure webhooks are caught correctly.
"""

import sys
import os

# Add the gateway directory to the path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from fastapi.testclient import TestClient
from app.main import app

def test_webhook_routing():
    """Test that webhook routes are properly handled."""
    
    client = TestClient(app)
    
    print("Testing webhook routing...")
    
    # Test Stripe webhook route
    response = client.post("/api/v1/webhooks/stripe", json={"test": "data"})
    print(f"Stripe webhook route: {response.status_code}")
    
    # Test generic webhook route
    response = client.post("/api/v1/webhooks/generic", json={"test": "data"})
    print(f"Generic webhook route: {response.status_code}")
    
    # Test that other routes still work
    response = client.get("/health")
    print(f"Health check route: {response.status_code}")
    
    # Test that tenant routes still work
    response = client.get("/api/v1/tenants/test")
    print(f"Tenant route: {response.status_code}")
    
    print("\n✅ Webhook routing test completed!")
    return True

if __name__ == "__main__":
    try:
        success = test_webhook_routing()
        sys.exit(0 if success else 1)
    except Exception as e:
        print(f"❌ Error during webhook routing test: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)
