#!/usr/bin/env python
"""
Migration test script for Local Deep Research.
This script checks the contents of both the legacy and new databases to diagnose migration issues.
"""

import os
import sqlite3
import sys
import time


def check_db_content(db_path, description):
    """Check what tables and how many rows are in a database."""
    if not os.path.exists(db_path):
        print(f"❌ {description} database not found at: {db_path}")
        return False

    print(f"📊 Examining {description} database at: {db_path}")
    try:
        conn = sqlite3.connect(db_path)
        cursor = conn.cursor()

        # Get list of tables
        cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
        tables = [
            row[0] for row in cursor.fetchall() if not row[0].startswith("sqlite_")
        ]

        if not tables:
            print("   ℹ️ No user tables found in database")
            conn.close()
            return False

        print(f"   📋 Tables found: {', '.join(tables)}")

        # For each table, count rows
        for table in tables:
            cursor.execute(f"SELECT COUNT(*) FROM {table}")
            count = cursor.fetchone()[0]
            print(f"   📝 Table '{table}' has {count} rows")

            # If table has rows, show sample
            if count > 0:
                cursor.execute(f"SELECT * FROM {table} LIMIT 1")
                columns = [description[0] for description in cursor.description]
                print(f"      Columns: {', '.join(columns)}")

                # For specific tables, get key columns
                if table in [
                    "research_history",
                    "research_logs",
                    "research",
                    "settings",
                ]:
                    key_cols = (
                        "id, query, status"
                        if table == "research_history"
                        else "id, key, value" if table == "settings" else "id, message"
                    )
                    cursor.execute(f"SELECT {key_cols} FROM {table} LIMIT 3")
                    sample = cursor.fetchall()
                    for row in sample:
                        print(f"      Sample data: {row}")

        conn.close()
        return True
    except Exception as e:
        print(f"❌ Error examining database: {e}")
        return False


def main():
    """Main function to test the migration."""
    # Import necessary constants
    try:
        # Set up paths
        current_dir = os.path.dirname(os.path.abspath(__file__))
        project_root = os.path.abspath(os.path.join(current_dir, "..", ".."))

        # Determine paths
        data_dir = os.path.join(project_root, "data")
        new_db_path = os.path.join(data_dir, "ldr.db")

        legacy_research_history_db = os.path.join(
            project_root, "src", "local_deep_research", "research_history.db"
        )
        legacy_deep_research_db = os.path.join(data_dir, "deep_research.db")

        # Print paths for verification
        print("=" * 60)
        print("DATABASE PATHS")
        print("=" * 60)
        print(f"New database path: {new_db_path}")
        print(f"Legacy research history DB: {legacy_research_history_db}")
        print(f"Legacy deep research DB: {legacy_deep_research_db}")
        print("=" * 60)

        # Check all databases
        check_db_content(legacy_research_history_db, "Legacy research_history")
        check_db_content(legacy_deep_research_db, "Legacy deep_research")

        # Now check for the new database or create it if needed
        if os.path.exists(new_db_path):
            check_db_content(new_db_path, "New ldr")
        else:
            print(f"ℹ️ New database doesn't exist yet at: {new_db_path}")
            print("Would you like to run a test migration? (y/n)")
            choice = input("> ").lower()
            if choice == "y":
                # Run the migration script directly
                try:
                    from src.local_deep_research.setup_data_dir import setup_data_dir
                except ImportError:
                    # If that fails, try with the direct import
                    sys.path.append(
                        os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
                    )
                    from local_deep_research.setup_data_dir import setup_data_dir

                setup_data_dir()

                # Import migration function
                try:
                    from src.local_deep_research.web.database.migrate_to_ldr_db import (
                        migrate_to_ldr_db,
                    )
                except ImportError:
                    # If that fails, try with the direct import
                    from local_deep_research.web.database.migrate_to_ldr_db import (
                        migrate_to_ldr_db,
                    )

                print("Running migration...")
                success = migrate_to_ldr_db()

                # Wait briefly to ensure file system has time to update
                time.sleep(1)

                if success:
                    print("\n✅ Migration completed. Checking new database:")
                    check_db_content(new_db_path, "New ldr")
                else:
                    print("❌ Migration failed")

        # Get the paths from the migration script to verify
        try:
            try:
                from src.local_deep_research.web.models.database import (
                    DB_PATH,
                    LEGACY_DEEP_RESEARCH_DB,
                    LEGACY_RESEARCH_HISTORY_DB,
                )
            except ImportError:
                from local_deep_research.web.models.database import (
                    DB_PATH,
                    LEGACY_DEEP_RESEARCH_DB,
                    LEGACY_RESEARCH_HISTORY_DB,
                )

            print("\n" + "=" * 60)
            print("PATHS FROM DATABASE MODULE")
            print("=" * 60)
            print(f"DB_PATH: {DB_PATH}")
            print(f"LEGACY_RESEARCH_HISTORY_DB: {LEGACY_RESEARCH_HISTORY_DB}")
            print(f"LEGACY_DEEP_RESEARCH_DB: {LEGACY_DEEP_RESEARCH_DB}")
        except ImportError as e:
            print(f"Could not import paths from database module: {e}")

    except Exception as e:
        print(f"Error in test script: {e}")
        return 1

    return 0


if __name__ == "__main__":
    sys.exit(main())
