# migrate_database.py
import sqlite3
import os

OLD_DB = "spanish_trainer_legacy.db"  # Your existing database renamed
NEW_DB = "spanish_trainer.db"         # The fresh, simplified target database

def migrate():
    if not os.path.exists(OLD_DB):
        print(f"❌ Error: Could not find legacy database file named '{OLD_DB}'")
        print("Please rename your active database file to match before running this script.")
        return

    print("🚀 Initializing schema transformation...")
    
    # Connect to both databases
    conn_old = sqlite3.connect(OLD_DB)
    conn_old.row_factory = sqlite3.Row
    cursor_old = conn_old.cursor()
    
    conn_new = sqlite3.connect(NEW_DB)
    cursor_new = conn_new.cursor()
    
    # 1. Provision the clean, simplified new tables
    cursor_new.execute("""
        CREATE TABLE IF NOT EXISTS translations (
            translation_id INTEGER PRIMARY KEY AUTOINCREMENT,
            es_text TEXT NOT NULL,
            en_text TEXT NOT NULL,
            source_context TEXT,
            tags TEXT,
            notes TEXT
        );
    """)
    
    cursor_new.execute("""
        CREATE TABLE IF NOT EXISTS settings (
            key TEXT PRIMARY KEY,
            value TEXT
        );
    """)
    
    # 2. Extract and pair data using explicit, table-qualified SQL queries
    print("📦 Extracting and consolidating relational text rows...")
    migration_query = """
        SELECT 
            t.translation_id,
            p1.text            AS spanish_phrase,
            p2.text            AS english_translation,
            p1.source_context  AS textbook_unit,
            t.tags             AS metadata_tags,
            t.notes            AS historical_notes
        FROM translations t
        JOIN phrases p1 ON t.source_phrase_id = p1.id
        JOIN phrases p2 ON t.target_phrase_id = p2.id
        WHERE p1.language = 'es' 
          AND p2.language = 'en'
        ORDER BY t.translation_id ASC;
    """
    
    try:
        cursor_old.execute(migration_query)
        legacy_records = cursor_old.fetchall()
    except sqlite3.OperationalError as e:
        print(f"❌ Legacy structure lookup failed: {e}")
        print("Verify your old table structures match the schema before running.")
        conn_old.close()
        conn_new.close()
        return

    # 3. Insert records into the new simplified table structure
    inserted_count = 0
    for row in legacy_records:
        cursor_new.execute("""
            INSERT INTO translations (
                translation_id,
                es_text,
                en_text,
                source_context,
                tags,
                notes
            ) VALUES (?, ?, ?, ?, ?, ?);
        """, (
            row["translation_id"],
            row["spanish_phrase"],
            row["english_translation"],
            row["textbook_unit"],
            row["metadata_tags"],
            row["historical_notes"]
        ))
        inserted_count += 1

    # 4. Copy existing system configuration keys over safely
    try:
        cursor_old.execute("SELECT key, value FROM settings;")
        settings_records = cursor_old.fetchall()
        for setting in settings_records:
            cursor_new.execute("""
                INSERT OR REPLACE INTO settings (key, value)
                VALUES (?, ?);
            """, (setting["key"], setting["value"]))
    except sqlite3.OperationalError:
        print("⚠️ Warning: No legacy settings table found or could not read it. Skipping settings copy.")

    # Commit changes and clean up connections
    conn_new.commit()
    conn_old.close()
    conn_new.close()
    
    print(f"✨ Migration complete! Successfully converted {inserted_count} text rows.")
    print(f"💾 Fresh database engine ready at: {NEW_DB}")

if __name__ == "__main__":
    migrate()