import os
import sys
from datetime import datetime

# Import the app and models
from app import create_app, db
from models import User

def create_admin_user(username='admin2', email='admin2@example.com', password='admin123456'):
    """Create a new admin user"""
    app = create_app()
    
    with app.app_context():
        # Check if user already exists
        existing_user = User.query.filter_by(username=username).first()
        if existing_user:
            print(f"L'utilisateur {username} existe déjà.")
            return False
        
        # Create new admin user
        new_admin = User(
            username=username,
            email=email,
            role='admin',
            created_at=datetime.utcnow()
        )
        new_admin.set_password(password)
        
        db.session.add(new_admin)
        db.session.commit()
        
        print(f"Nouvel administrateur créé avec succès: {username}")
        return True

if __name__ == "__main__":
    username = 'admin2'
    email = 'admin2@example.com'
    password = 'admin123456'
    
    success = create_admin_user(username, email, password)
    
    if success:
        print(f"Vous pouvez maintenant vous connecter avec:")
        print(f"Nom d'utilisateur: {username}")
        print(f"Mot de passe: {password}")