import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from app import create_app, db
from app.models.user import User

def create_admin_user(email='admin@example.com', password='admin123', username='Arif jawaid'):
    """Create or update admin user with the specified credentials"""
    app = create_app()
    
    with app.app_context():
        # Check if admin exists
        admin = User.query.filter_by(email=email).first()
        
        if admin:
            print(f"Admin user {email} already exists. Updating password...")
            admin.set_password(password)
        else:
            print(f"Creating new admin user {email}...")
            admin = User(
                email=email,
                username=username,
                role='admin',
                is_approved=True
            )
            admin.set_password(password)
            db.session.add(admin)
        
        db.session.commit()
        print(f"Admin user {email} set up successfully with the provided password.")
        print("You can now log in with these credentials.")

if __name__ == "__main__":
    # Get credentials from command line arguments or use defaults
    import argparse
    parser = argparse.ArgumentParser(description='Create or update admin user')
    parser.add_argument('--email', default='admin@example.com', help='Admin email address')
    parser.add_argument('--password', default='admin123', help='Admin password')
    parser.add_argument('--username', default='Arif jawaid', help='Admin username')
    
    args = parser.parse_args()
    create_admin_user(args.email, args.password, args.username)
