"""
Script to reset admin password.
Run this if you've lost access to your admin account.
"""

import mysql.connector
from werkzeug.security import generate_password_hash

# Database configuration
db_config = {
    "host": "127.0.0.1",
    "user": "root",
    "password": "",
    "database": "event_ai"
}

# New password - CHANGE THIS!
NEW_PASSWORD = "123456"  # Change to your desired password
USERNAME = "admin"

def reset_admin_password():
    try:
        conn = mysql.connector.connect(**db_config)
        cursor = conn.cursor(dictionary=True)
        
        # Check if admin user exists
        cursor.execute("SELECT * FROM admin_users WHERE username = %s", (USERNAME,))
        existing_user = cursor.fetchone()
        
        if not existing_user:
            print(f"✗ Admin user '{USERNAME}' does not exist!")
            print("Run setup_admin.py first to create the user.")
            cursor.close()
            conn.close()
            return
        
        # Generate new hash
        new_hash = generate_password_hash(NEW_PASSWORD)
        
        # Update password
        cursor.execute(
            "UPDATE admin_users SET password = %s WHERE username = %s",
            (new_hash, USERNAME)
        )
        conn.commit()
        
        print(f"✓ Password reset successful for '{USERNAME}'!")
        print(f"  New password: {NEW_PASSWORD}")
        print(f"\n⚠ Please change this to a secure password after logging in!")
        
        cursor.close()
        conn.close()
        
    except mysql.connector.Error as err:
        print(f"✗ Database error: {err}")
    except Exception as e:
        print(f"✗ Error: {e}")

if __name__ == "__main__":
    print("=" * 50)
    print("Admin Password Reset Script")
    print("=" * 50)
    reset_admin_password()
    print("=" * 50)
