import mysql.connector
from db import DB_CONFIG

def fix_users_table():
    try:
        conn = mysql.connector.connect(**DB_CONFIG)
        cursor = conn.cursor()
        
        columns_to_add = [
            ("first_name", "VARCHAR(100)"),
            ("last_name", "VARCHAR(100)"),
            ("phone", "VARCHAR(20)"),
            ("shipping_address", "TEXT")
        ]
        
        for name, type in columns_to_add:
            try:
                cursor.execute(f"ALTER TABLE users ADD COLUMN {name} {type};")
                print(f"Added column {name}")
            except mysql.connector.Error as e:
                if e.errno == 1060: # Column already exists
                    print(f"Column {name} already exists")
                else:
                    print(f"Error adding {name}: {e}")
                    
        conn.commit()
    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__":
    fix_users_table()
