#!/usr/bin/env python3
"""Run database migrations to add WhatsApp columns"""

import psycopg2
from dotenv import load_dotenv
import os

load_dotenv()

# Get database credentials
db_url = os.getenv('DATABASE_URL', 'postgresql://wedding_admin:Aa123456@localhost:5432/wedding_guests')

try:
    # Connect to database
    conn = psycopg2.connect(db_url)
    cursor = conn.cursor()
    
    # Add the new columns
    alter_statements = [
        "ALTER TABLE events ADD COLUMN IF NOT EXISTS partner1_name TEXT;",
        "ALTER TABLE events ADD COLUMN IF NOT EXISTS partner2_name TEXT;",
        "ALTER TABLE events ADD COLUMN IF NOT EXISTS venue TEXT;",
        "ALTER TABLE events ADD COLUMN IF NOT EXISTS event_time TEXT;",
        "ALTER TABLE events ADD COLUMN IF NOT EXISTS guest_link TEXT;",
    ]
    
    for stmt in alter_statements:
        try:
            cursor.execute(stmt)
            print("✅ " + stmt)
        except Exception as e:
            print("⚠️ " + stmt + " - " + str(e)[:60])
    
    conn.commit()
    
    # Verify columns exist
    cursor.execute("SELECT column_name FROM information_schema.columns WHERE table_name = 'events' ORDER BY ordinal_position")
    columns = cursor.fetchall()
    print("\n📋 Events table columns:")
    for col in columns:
        print("  - " + col[0])
    
    cursor.close()
    conn.close()
    print("\n✅ Migration completed successfully!")
    
except Exception as e:
    print("❌ Error: " + str(e))
