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

def seed_sample_users():
    """Create sample users for testing login with correct roles"""
    app = create_app()
    
    with app.app_context():
        try:
            # Sample users with correct role names (must match database roles exactly)
            sample_users = [
                {
                    'username': 'Super Admin',
                    'email': 'superadmin@vending.com',
                    'password': 'admin123',
                    'role': 'SuperAdmin',  # MUST MATCH DATABASE EXACTLY
                    'contact': '+1234567890'
                },
                {
                    'username': 'Management User',
                    'email': 'management@vending.com',
                    'password': 'management123',
                    'role': 'Management',
                    'contact': '+1234567891'
                },
                {
                    'username': 'Admin User',
                    'email': 'admin@vending.com',
                    'password': 'admin123',
                    'role': 'Admin',
                    'contact': '+1234567892'
                },
                {
                    'username': 'Client User',
                    'email': 'client@vending.com',
                    'password': 'client123',
                    'role': 'Client',
                    'contact': '+1234567893'
                },
                {
                    'username': 'Refiller User',
                    'email': 'refiller@vending.com',
                    'password': 'refiller123',
                    'role': 'Refiller',
                    'contact': '+1234567894'
                },
                {
                    'username': 'Servicer User',
                    'email': 'servicer@vending.com',
                    'password': 'servicer123',
                    'role': 'Servicer',
                    'contact': '+1234567895'
                }
            ]
            
            print("=" * 70)
            print("SEEDING SAMPLE USERS")
            print("=" * 70)
            
            created_count = 0
            
            for user_data in sample_users:
                # Check if user already exists
                existing_user = User.query.filter_by(email=user_data['email']).first()
                if existing_user:
                    print(f"⚠️  User {user_data['email']} already exists - skipping")
                    continue
                
                # Create new user
                new_user = User(
                    user_id=User.generate_user_id(user_data['username'], user_data['email']),
                    username=user_data['username'],
                    email=user_data['email'],
                    contact=user_data['contact'],
                    roles=user_data['role'],  # CORRECT ROLE NAME
                    user_status='active'
                )
                
                # Set password
                new_user.set_password(user_data['password'])
                
                # Add to database
                db.session.add(new_user)
                created_count += 1
                
                print(f"✓ User: {user_data['username']} ({user_data['role']})")
            
            # Commit all changes
            db.session.commit()
            
            print("\n" + "=" * 70)
            print(f"✓ Successfully created {created_count} sample user(s)")
            print("=" * 70)
            
            # Display all created users
            print("\nCREATED USERS - USE THESE TO LOGIN:")
            print("=" * 70)
            all_users = User.query.all()
            for user in all_users:
                print(f"\nUsername: {user.username}")
                print(f"Email: {user.email}")
                print(f"Role: {user.roles}")
                print(f"Status: {user.user_status}")
                print("-" * 70)
            
        except Exception as e:
            db.session.rollback()
            print(f"\n❌ Error creating sample users: {str(e)}")
            import traceback
            traceback.print_exc()
            sys.exit(1)

if __name__ == '__main__':
    seed_sample_users()     