import os
import sys
from datetime import datetime
from werkzeug.security import generate_password_hash

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

def reset_admin_user(username='admin', email='admin@example.com', password='gmpl2023'):
    """Create a new admin user or reset existing admin"""
    app = create_app()
    
    with app.app_context():
        # Check if admin user already exists
        existing_admin = User.query.filter_by(username=username).first()
        
        if existing_admin:
            print(f"Resetting admin user: {existing_admin.username}")
            existing_admin.password_hash = generate_password_hash(password)
            existing_admin.email = email
            existing_admin.role = 'admin'
            db.session.commit()
        else:
            # Create admin user
            admin = User(
                username=username,
                email=email,
                password_hash=generate_password_hash(password),
                role='admin',
                created_at=datetime.utcnow()
            )
            
            db.session.add(admin)
            db.session.commit()
            print(f"Admin user created successfully: {username}")
        
        return True

if __name__ == "__main__":
    # Use fixed values for simplicity
    username = 'admin'
    email = 'admin@example.com'
    password = 'gmpl2023'
    
    reset_admin_user(username, email, password)
    print(f"You can now login at /admin/login with:")
    print(f"Username: {username}")
    print(f"Password: {password}")