#!/usr/bin/env python3
"""Debug login issues by testing components individually."""

import asyncio
from motor.motor_asyncio import AsyncIOMotorClient
from app.core.config import settings
from app.core.security import verify_password
from app.models.user import User

async def test_database_connection():
    """Test direct database connection."""
    print("1. Testing database connection...")
    client = AsyncIOMotorClient(settings.mongodb_uri)
    db = client[settings.mongodb_db]
    
    try:
        # Test connection
        await client.admin.command('ping')
        print("✅ Database connection successful")
        
        # Check if users collection exists
        collections = await db.list_collection_names()
        print(f"✅ Collections: {collections}")
        
        # Count users
        user_count = await db.users.count_documents({})
        print(f"✅ User count: {user_count}")
        
        # Find admin user
        user_doc = await db.users.find_one({"email": "admin@example.com"})
        if user_doc:
            print(f"✅ Found admin user: {user_doc['email']}")
            user = User(**user_doc)
            print(f"✅ User model validation successful")
            
            # Test password verification
            print("2. Testing password verification...")
            password_correct = verify_password("admin", user.hashed_password)
            print(f"✅ Password verification result: {password_correct}")
            
        else:
            print("❌ Admin user not found")
            
    except Exception as e:
        print(f"❌ Database error: {e}")
        import traceback
        traceback.print_exc()
    finally:
        client.close()

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