#!/usr/bin/env python3
"""
Apply migration 011: Add fabricant and produit fields
"""
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent))

from app.db.session import get_peripherals_db


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

    try:
        print("\ud83d\udd27 Applying migration 011: Add fabricant and produit")
        print("=" * 60)

        migration_file = Path(__file__).parent / "migrations" / "011_add_fabricant_produit.sql"
        with open(migration_file, "r") as f:
            sql_commands = f.read()

        for command in sql_commands.split(';'):
            command = command.strip()
            if command and not command.startswith('--'):
                db.execute(command)

        db.commit()
        print("\n\u2705 Migration 011 applied successfully!")
        print("=" * 60)
        print("Added columns:")
        print("  - fabricant (TEXT)")
        print("  - produit (TEXT)")

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


if __name__ == "__main__":
    apply_migration()
