"""
Script to create an admin user in the database.
Run this once to set up your admin credentials.
"""

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"
}

# Admin credentials - CHANGE THESE!
ADMIN_USERNAME = "photographer"
ADMIN_PASSWORD = "09876"  # Change this to a secure password!

def create_admin_user():
    try:
        # Connect to database
        conn = mysql.connector.connect(**db_config)
        cursor = conn.cursor(dictionary=True)
        
        # Check if admin_users table exists
        cursor.execute("""
            CREATE TABLE IF NOT EXISTS admin_users (
                id INT AUTO_INCREMENT PRIMARY KEY,
                username VARCHAR(255) NOT NULL UNIQUE,
                password VARCHAR(255) NOT NULL,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            )
        """)
        
        # Check if admin user already exists
        cursor.execute("SELECT * FROM admin_users WHERE username = %s", (ADMIN_USERNAME,))
        existing_user = cursor.fetchone()
        
        if existing_user:
            print(f"Admin user '{ADMIN_USERNAME}' already exists!")
            print(f"User ID: {existing_user['id']}")
            print(f"Created: {existing_user['created_at']}")
        else:
            # Hash the password
            hashed_password = generate_password_hash(ADMIN_PASSWORD)
            
            # Insert admin user
            cursor.execute(
                "INSERT INTO admin_users (username, password) VALUES (%s, %s)",
                (ADMIN_USERNAME, hashed_password)
            )
            conn.commit()
            print(f"✓ Admin user '{ADMIN_USERNAME}' created successfully!")
            print(f"  Username: {ADMIN_USERNAME}")
            print(f"  Password: {ADMIN_PASSWORD}")
            print(f"\n⚠ Please change the password to something more secure!")
        
        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 User Setup Script")
    print("=" * 50)
    create_admin_user()
    print("=" * 50)
