"""Add venue_id and tour_id columns to rating table"""
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from database import DATABASE_URL
import psycopg2

def run():
    conn = psycopg2.connect(DATABASE_URL)
    cur = conn.cursor()
    
    # Add venue_id column
    try:
        cur.execute("ALTER TABLE rating ADD COLUMN venue_id INTEGER REFERENCES venue(id)")
        print("✅ Added venue_id to rating")
    except Exception as e:
        if "already exists" in str(e):
            print("⚠️ venue_id column already exists")
        else:
            print(f"❌ Error adding venue_id: {e}")
        conn.rollback()
    else:
        conn.commit()
    
    # Add tour_id column
    try:
        cur.execute("ALTER TABLE rating ADD COLUMN tour_id INTEGER REFERENCES tour(id)")
        print("✅ Added tour_id to rating")
    except Exception as e:
        if "already exists" in str(e):
            print("⚠️ tour_id column already exists")
        else:
            print(f"❌ Error adding tour_id: {e}")
        conn.rollback()
    else:
        conn.commit()
    
    cur.close()
    conn.close()

if __name__ == "__main__":
    run()
