#!/usr/bin/env python
"""
Database migration script for Local Deep Research.
Migrates data from legacy databases (deep_research.db and research_history.db) to the new unified database (ldr.db).
"""

import argparse
import logging
import os
import sys

try:
    from local_deep_research.web.database.migrate_to_ldr_db import (
        migrate_to_ldr_db,
    )
    from local_deep_research.web.models.database import (
        DB_PATH,
        LEGACY_DEEP_RESEARCH_DB,
        LEGACY_RESEARCH_HISTORY_DB,
    )
except ImportError:
    # If that fails, try with the relative path.
    from .web.database.migrate_to_ldr_db import migrate_to_ldr_db
    from .web.models.database import (
        DB_PATH,
        LEGACY_DEEP_RESEARCH_DB,
        LEGACY_RESEARCH_HISTORY_DB,
    )

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
    handlers=[logging.StreamHandler()],
)
logger = logging.getLogger("migrate_db")

# Add proper paths for import
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.dirname(os.path.dirname(current_dir)))


def main():
    """Main migration function that parses arguments and runs the migration"""
    parser = argparse.ArgumentParser(
        description="Local Deep Research Database Migration"
    )
    parser.add_argument(
        "--backup",
        action="store_true",
        help="Create backup of existing databases before migration",
    )
    parser.add_argument(
        "--force",
        action="store_true",
        help="Force migration even if target database exists",
    )
    parser.add_argument(
        "--dry-run",
        action="store_true",
        help="Only check what would be migrated, don't perform actual migration",
    )
    parser.add_argument("--verbose", action="store_true", help="Enable verbose logging")

    args = parser.parse_args()

    if args.verbose:
        logging.getLogger().setLevel(logging.DEBUG)

    print("=" * 80)
    print("LOCAL DEEP RESEARCH DATABASE MIGRATION")
    print("=" * 80)

    try:
        # First try the normal import
        print(f"Target database will be created at: {DB_PATH}")

        # Check if migration is needed
        if os.path.exists(DB_PATH) and not args.force:
            print(f"Target database already exists at: {DB_PATH}")
            if (
                input(
                    "Do you want to continue anyway? This may overwrite data. (y/n): "
                ).lower()
                != "y"
            ):
                print("Migration aborted.")
                return 1

        # Check if source databases exist
        deep_research_exists = os.path.exists(LEGACY_DEEP_RESEARCH_DB)
        research_history_exists = os.path.exists(LEGACY_RESEARCH_HISTORY_DB)

        if not deep_research_exists and not research_history_exists:
            print("No legacy databases found. Nothing to migrate.")
            return 0

        print("Found legacy databases:")
        if deep_research_exists:
            print(f"  - {LEGACY_DEEP_RESEARCH_DB}")
        if research_history_exists:
            print(f"  - {LEGACY_RESEARCH_HISTORY_DB}")

        # Create backups if requested
        if args.backup:
            if deep_research_exists:
                backup_path = f"{LEGACY_DEEP_RESEARCH_DB}.bak"
                import shutil

                shutil.copy2(LEGACY_DEEP_RESEARCH_DB, backup_path)
                print(f"Created backup: {backup_path}")

            if research_history_exists:
                backup_path = f"{LEGACY_RESEARCH_HISTORY_DB}.bak"
                import shutil

                shutil.copy2(LEGACY_RESEARCH_HISTORY_DB, backup_path)
                print(f"Created backup: {backup_path}")

        # Run migration or dry run
        if args.dry_run:
            print("\nDRY RUN - No changes will be made.")
            print(f"Would migrate data to: {DB_PATH}")
            return 0
        else:
            print(f"\nStarting migration to: {DB_PATH}")

            success = migrate_to_ldr_db()

            if success:
                print("\nMigration completed successfully.")
                print(
                    "You can now start the application with the new unified database."
                )
                return 0
            else:
                print("\nMigration failed. Check the logs for details.")
                return 1

    except Exception as e:
        logger.error(f"Migration error: {e}", exc_info=True)
        print(f"Error during migration: {e}")
        return 1


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