#!/usr/bin/env python
import sqlite3
import sys

db_path = r'c:\Users\DN761673\Downloads\TWM_New\truewave-platform\backend\db.sqlite3'

try:
    conn = sqlite3.connect(db_path)
    cursor = conn.cursor()
    
    # Check if the columns already exist
    cursor.execute("PRAGMA table_info(products_product)")
    columns = [col[1] for col in cursor.fetchall()]
    
    missing_columns = []
    
    if 'allow_part_payment' not in columns:
        missing_columns.append('allow_part_payment')
    if 'part_payment_percentage' not in columns:
        missing_columns.append('part_payment_percentage')
    if 'part_payment_cooling_days' not in columns:
        missing_columns.append('part_payment_cooling_days')
    if 'part_payment_conversion_product_id' not in columns:
        missing_columns.append('part_payment_conversion_product_id')
    
    if missing_columns:
        print(f"Adding missing columns: {', '.join(missing_columns)}")
        
        if 'allow_part_payment' not in columns:
            cursor.execute("ALTER TABLE products_product ADD COLUMN allow_part_payment BOOLEAN DEFAULT 0")
        if 'part_payment_percentage' not in columns:
            cursor.execute("ALTER TABLE products_product ADD COLUMN part_payment_percentage INTEGER DEFAULT 50")
        if 'part_payment_cooling_days' not in columns:
            cursor.execute("ALTER TABLE products_product ADD COLUMN part_payment_cooling_days INTEGER DEFAULT 180")
        if 'part_payment_conversion_product_id' not in columns:
            cursor.execute("ALTER TABLE products_product ADD COLUMN part_payment_conversion_product_id INTEGER")
        
        conn.commit()
        print("✓ Successfully added missing columns!")
    else:
        print("✓ All columns already exist")
        
    conn.close()
    sys.exit(0)
except Exception as e:
    print(f"✗ Error: {e}", file=sys.stderr)
    sys.exit(1)
