"""
Test vTiger modules and queries
"""
import asyncio
import sys
sys.path.insert(0, '/app')

from app.services.vtiger_service import get_vtiger_service

async def test_vtiger():
    vtiger = get_vtiger_service()
    
    # Test connection
    print("🔑 Testing vTiger connection...")
    connected = await vtiger.test_connection()
    if not connected:
        print("❌ Connection failed!")
        return
    
    print("\n" + "="*60)
    print("Testing different module queries for account 3x760")
    print("="*60)
    
    # Test various queries
    queries = [
        # Try different module names
        ("Services", "SELECT * FROM Services WHERE account_id='3x760' LIMIT 5;"),
        ("Products", "SELECT * FROM Products WHERE account_id='3x760' LIMIT 5;"),
        ("SalesOrder", "SELECT * FROM SalesOrder WHERE account_id='3x760' LIMIT 5;"),
        ("Invoice", "SELECT * FROM Invoice WHERE account_id='3x760' LIMIT 5;"),
        ("Quotes", "SELECT * FROM Quotes WHERE account_id='3x760' LIMIT 5;"),
        ("Contacts", "SELECT * FROM Contacts WHERE account_id='3x760' LIMIT 5;"),
        
        # Try without account filter to see structure
        ("SalesOrder (all)", "SELECT * FROM SalesOrder LIMIT 2;"),
        ("Invoice (all)", "SELECT * FROM Invoice LIMIT 2;"),
    ]
    
    for name, query in queries:
        print(f"\n📋 Testing: {name}")
        print(f"Query: {query}")
        try:
            results = await vtiger.query(query)
            if results:
                print(f"✅ Found {len(results)} records")
                if len(results) > 0:
                    print(f"Sample keys: {list(results[0].keys())[:10]}")
                    # Show first record
                    print("\nFirst record:")
                    for key, value in list(results[0].items())[:15]:
                        print(f"  {key}: {value}")
            else:
                print("⚠️ No results")
        except Exception as e:
            print(f"❌ Error: {e}")

if __name__ == "__main__":
    asyncio.run(test_vtiger())
