#!/usr/bin/env python3
"""Test auth functionality."""

import asyncio
from motor.motor_asyncio import AsyncIOMotorClient
from passlib.context import CryptContext
from app.core.config import settings
from app.models.user import User
from app.core.security import verify_password, create_access_token

pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")

async def test_login():
    print("Testing login logic...")
    
    client = AsyncIOMotorClient(settings.mongodb_uri)
    db = client[settings.mongodb_db]
    
    try:
        # Find user
        user_doc = await db.users.find_one({"email": "admin@example.com"})
        print("Found user document:", bool(user_doc))
        
        if user_doc:
            print("User email:", user_doc.get('email'))
            print("User role:", user_doc.get('role'))
            print("User active:", user_doc.get('is_active'))
            
            # Create User object
            user = User(**user_doc)
            print("✅ User object created successfully")
            
            # Test password verification
            password_ok = verify_password("admin", user.hashed_password)
            print(f"Password verification: {password_ok}")
            
            if password_ok:
                # Create token
                token = create_access_token(subject=str(user.id))
                print(f"✅ Token created: {token[:20]}...")
            else:
                print("❌ Password verification failed")
                
    except Exception as e:
        print(f"❌ Error: {e}")
        import traceback
        traceback.print_exc()
    
    client.close()

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