#!/usr/bin/env python3
"""
Migration script to add the missing 'color' column to product_variants table
Run this from your local machine to fix the database schema
"""

import psycopg2
import os
import sys
from config import DATABASE_URL

def migrate():
    """Add color column to product_variants table"""
    try:
        # Connect to database
        if DATABASE_URL:
            conn = psycopg2.connect(DATABASE_URL)
        else:
            print("ERROR: DATABASE_URL environment variable not set!")
            sys.exit(1)
        
        cursor = conn.cursor()
        
        # Check if column already exists
        cursor.execute("""
            SELECT column_name 
            FROM information_schema.columns 
            WHERE table_name='product_variants' 
            AND column_name='color'
        """)
        
        if cursor.fetchone():
            print("✓ Column 'color' already exists in product_variants table")
            cursor.close()
            conn.close()
            return True
        
        # Add the column
        print("Adding 'color' column to product_variants table...")
        cursor.execute("""
            ALTER TABLE product_variants 
            ADD COLUMN color VARCHAR(100)
        """)
        
        conn.commit()
        print("✓ Successfully added 'color' column!")
        
        cursor.close()
        conn.close()
        return True
        
    except Exception as e:
        print(f"✗ Migration failed: {e}")
        return False

if __name__ == '__main__':
    success = migrate()
    sys.exit(0 if success else 1)
