from app import create_app, db
from app.models import User
from flask_bcrypt import Bcrypt

app = create_app()
bcrypt = Bcrypt(app)  # Initialize bcrypt with app

with app.app_context():
    try:
        # Test bcrypt directly
        password = 'admin123'
        password_hash = bcrypt.generate_password_hash(password).decode('utf-8')
        print(f"Generated hash: {password_hash}")
        
        # Check password
        check_result = bcrypt.check_password_hash(password_hash, password)
        print(f"Password check result: {check_result}")
        
        # Test with actual user from DB
        user = User.query.filter_by(username='admin').first()
        if user:
            print(f"Found user: {user.username}")
            print(f"Stored hash: {user.password_hash}")
            
            # Test the user's check_password method
            user_check = user.check_password(password)
            print(f"User check_password result: {user_check}")
            
            # Test bcrypt directly with stored hash
            bcrypt_check = bcrypt.check_password_hash(user.password_hash, password)
            print(f"Bcrypt direct check result: {bcrypt_check}")
        else:
            print("No user found")
            
    except Exception as e:
        print(f"Error during bcrypt test: {e}")
        import traceback
        traceback.print_exc()