#!/usr/bin/env python3
"""
Migration 016: Ajout du champ ram_max_capacity_mb
"""
import sqlite3
import sys
from pathlib import Path

# Configuration
DB_PATH = Path(__file__).parent / "data" / "data.db"
MIGRATION_FILE = Path(__file__).parent / "migrations" / "016_add_ram_max_capacity.sql"

def main():
    if not DB_PATH.exists():
        print(f"❌ Base de données non trouvée: {DB_PATH}")
        sys.exit(1)

    # Lire le fichier SQL
    with open(MIGRATION_FILE, 'r') as f:
        sql = f.read()

    # Connexion à la BDD
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()

    try:
        # Vérifier si la colonne existe déjà
        cursor.execute("PRAGMA table_info(hardware_snapshots)")
        columns = [col[1] for col in cursor.fetchall()]

        if 'ram_max_capacity_mb' in columns:
            print("✅ La colonne ram_max_capacity_mb existe déjà")
            return

        # Appliquer la migration
        print("🔧 Application de la migration 016...")
        cursor.executescript(sql)
        conn.commit()
        print("✅ Migration 016 appliquée avec succès")

        # Vérifier
        cursor.execute("PRAGMA table_info(hardware_snapshots)")
        columns_after = [col[1] for col in cursor.fetchall()]

        if 'ram_max_capacity_mb' in columns_after:
            print("✅ Colonne ram_max_capacity_mb ajoutée")
        else:
            print("❌ Erreur: colonne non ajoutée")
            sys.exit(1)

    except Exception as e:
        print(f"❌ Erreur lors de la migration: {e}")
        conn.rollback()
        sys.exit(1)
    finally:
        conn.close()

if __name__ == "__main__":
    main()
