import mysql.connector
from db import DB_CONFIG

def update_schema():
    try:
        conn = mysql.connector.connect(**DB_CONFIG)
        cursor = conn.cursor()
        
        # Add columns if they don't exist
        columns_to_add = [
            ("first_name", "VARCHAR(100)"),
            ("last_name", "VARCHAR(100)"),
            ("phone", "VARCHAR(20)"),
            ("shipping_address", "TEXT")
        ]
        
        for col_name, col_type in columns_to_add:
            try:
                cursor.execute(f"ALTER TABLE users ADD COLUMN {col_name} {col_type};")
            except mysql.connector.Error as e:
                if e.errno == 1060: # Column already exists
                    print(f"Column {col_name} already exists")
                else:
                    print(f"Error adding {col_name}: {e}")
                    
        conn.commit()
        print("Schema updated successfully")
        
    except mysql.connector.Error as err:
        print(f"Error: {err}")
    finally:
        if 'conn' in locals() and conn.is_connected():
            cursor.close()
            conn.close()

if __name__ == "__main__":
    update_schema()
