"""
Script to create a photographer user in the database.
"""

import mysql.connector
from werkzeug.security import generate_password_hash

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

def create_photographer():
    try:
        conn = mysql.connector.connect(**db_config)
        cursor = conn.cursor(dictionary=True)
        
        # Check if users_auth table exists
        cursor.execute("""
            SELECT TABLE_NAME 
            FROM information_schema.TABLES 
            WHERE TABLE_SCHEMA = 'event_ai' AND TABLE_NAME = 'users_auth'
        """)
        table_exists = cursor.fetchone()
        
        if not table_exists:
            print("✗ users_auth table does NOT exist!")
            print("\nCreating table...")
            cursor.execute("""
                CREATE TABLE users_auth (
                    id INT AUTO_INCREMENT PRIMARY KEY,
                    username VARCHAR(255) NOT NULL UNIQUE,
                    password VARCHAR(255) NOT NULL,
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                )
            """)
            conn.commit()
            print("✓ Table created successfully")
        else:
            print("✓ users_auth table exists")
        
        # Check if photographer already exists
        cursor.execute("SELECT * FROM users_auth WHERE username = %s", ("photographer",))
        existing_user = cursor.fetchone()
        
        if existing_user:
            print(f"\n✗ Photographer user already exists!")
            print(f"  Username: {existing_user['username']}")
            print(f"  ID: {existing_user['id']}")
        else:
            # Create photographer with plain text password
            cursor.execute(
                "INSERT INTO users_auth (username, password) VALUES (%s, %s)",
                ("photographer", "photo123")
            )
            conn.commit()
            print(f"\n✓ Photographer user created successfully!")
            print(f"  Username: photographer")
            print(f"  Password: photo123")
        
        # List all users
        cursor.execute("SELECT id, username, created_at FROM users_auth")
        users = cursor.fetchall()
        print(f"\n{'='*60}")
        print(f"{'ID':<5} {'Username':<20} {'Created'}")
        print(f"{'='*60}")
        for user in users:
            print(f"{user['id']:<5} {user['username']:<20} {user['created_at']}")
        print(f"{'='*60}")
        
        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("="*60)
    print("Photographer User Setup")
    print("="*60)
    create_photographer()
    print("="*60)
