#!/usr/bin/env python3
"""
Apply migration 010: Add iManufacturer and iProduct fields
"""
import sys
from pathlib import Path

# Add app to path
sys.path.insert(0, str(Path(__file__).parent))

from app.db.session import get_peripherals_db

def apply_migration():
    """Apply migration 010"""
    db = next(get_peripherals_db())

    try:
        print("🔧 Applying migration 010: Add iManufacturer and iProduct")
        print("=" * 60)

        # Read migration SQL
        migration_file = Path(__file__).parent / "migrations" / "010_add_usb_manufacturer_product.sql"
        with open(migration_file, 'r') as f:
            sql_commands = f.read()

        # Split by semicolon and execute each command
        for command in sql_commands.split(';'):
            command = command.strip()
            if command and not command.startswith('--'):
                print(f"Executing: {command[:80]}...")
                db.execute(command)

        db.commit()
        print("\n✅ Migration 010 applied successfully!")
        print("=" * 60)
        print("Added columns:")
        print("  - iManufacturer (TEXT)")
        print("  - iProduct (TEXT)")

    except Exception as e:
        print(f"❌ Error applying migration: {e}")
        db.rollback()
        raise
    finally:
        db.close()

if __name__ == "__main__":
    apply_migration()
