from app import create_app, db
from app.models import User
import sys

def seed_sample_user():
    """Create a sample user for testing login"""
    app = create_app()
    
    with app.app_context():
        try:
            # Check if user already exists
            existing_user = User.query.filter_by(email='admin@vending.com').first()
            if existing_user:
                print("Sample user already exists!")
                print(f"Email: admin@vending.com")
                print(f"Password: admin123")
                return
            
            # Create sample admin user
            sample_user = User(
                user_id=User.generate_user_id('admin', 'admin@vending.com'),
                username='Admin User',
                email='admin@vending.com',
                contact='+1234567890',
                roles='admin',
                user_status='active'
            )
            
            # Set password
            sample_user.set_password('admin123')
            
            # Add to database
            db.session.add(sample_user)
            db.session.commit()
            
            print("✅ Sample user created successfully!")
            print("=" * 50)
            print(f"User ID: {sample_user.user_id}")
            print(f"Username: {sample_user.username}")
            print(f"Email: admin@vending.com")
            print(f"Password: admin123")
            print(f"Role: {sample_user.roles}")
            print(f"Status: {sample_user.user_status}")
            print("=" * 50)
            
        except Exception as e:
            db.session.rollback()
            print(f"❌ Error creating sample user: {str(e)}")
            sys.exit(1)

if __name__ == '__main__':
    seed_sample_user()