import logging
import sys
from typing import Dict

from . import get_advanced_search_system, get_report_generator
from .config.config_files import settings
from .utilities.db_utils import get_db_setting


def print_report(report: Dict):
    """Print and save the report in a readable format"""

    # Print to console in readable format
    print("\n=== GENERATED REPORT ===\n")

    # Print content
    print(report["content"])

    # Save to file in markdown format
    with open("report.md", "w", encoding="utf-8") as markdown_file:
        # Write content
        markdown_file.write(report["content"])

        # Write metadata at the end of the file
        markdown_file.write("\n\n---\n\n")
        markdown_file.write("## Report Metadata\n")

        markdown_file.write(f"- Query: {report['metadata']['query']}\n")

    print("\nReport has been saved to report.md")


# Create the report generator lazily to avoid circular imports
def get_report_generator_instance():
    return get_report_generator()


# report_generator = IntegratedReportGenerator()
report_generator = None  # Will be initialized when needed


def main():
    import logging

    from .utilities.setup_utils import setup_user_directories

    # Configure logging
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger(__name__)
    search_iterations = get_db_setting("search.iterations", settings.search.iterations)
    questions_per_iteration = get_db_setting(
        "search.questions_per_iteration", settings.search.questions_per_iteration
    )
    logger.info(
        f"Starting with settings: iterations={search_iterations}, "
        f"questions_per_iteration={questions_per_iteration}"
    )

    # Explicitly run setup
    logger.info("Initializing configuration...")
    setup_user_directories()

    system = get_advanced_search_system()

    print("Welcome to the Advanced Research System")
    print("Type 'quit' to exit")

    while True:
        print("\nSelect output type:")
        print("1) Quick Summary (Generated in a few minutes)")
        print(
            "2) Detailed Research Report (Recommended for deeper analysis - may take several hours)"
        )
        choice = input("Enter number (1 or 2): ").strip()

        while choice not in ["1", "2"]:
            print("\nInvalid input. Please enter 1 or 2:")
            print("1) Quick Summary (Generated in a few minutes)")
            print(
                "2) Detailed Research Report (Recommended for deeper analysis - may take several hours)"
            )
            choice = input("Enter number (1 or 2): ").strip()

        query = input("\nEnter your research query: ").strip()

        if query.lower() == "quit":
            break

        # System will automatically use updated configuration
        # through the automatic reloading in get_llm() and get_search()

        if choice == "1":
            print("\nResearching... This may take a few minutes.\n")
        else:
            print(
                "\nGenerating detailed report... This may take several hours. Please be patient as this enables deeper analysis.\n"
            )

        results = system.analyze_topic(query)
        if results:
            if choice == "1":
                # Quick Summary
                print("\n=== QUICK SUMMARY ===")
                if results["findings"] and len(results["findings"]) > 0:
                    initial_analysis = [
                        finding["content"] for finding in results["findings"]
                    ]
                    print(initial_analysis)

            else:
                # Full Report
                # Initialize report_generator if not already done
                global report_generator
                if report_generator is None:
                    report_generator = get_report_generator()

                final_report = report_generator.generate_report(results, query)
                print("\n=== RESEARCH REPORT ===")
                print_report(final_report)

                print("\n=== RESEARCH METRICS ===")
                print(f"Search Iterations: {results['iterations']}")

        else:
            print("Research failed. Please try again.")


# Add command for database migration
if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(description="Local Deep Research")
    parser.add_argument("--web", action="store_true", help="Start the web server")
    parser.add_argument(
        "--migrate-db", action="store_true", help="Migrate legacy databases to ldr.db"
    )
    parser.add_argument("--debug", action="store_true", help="Enable debug mode")
    parser.add_argument(
        "--test-migration",
        action="store_true",
        help="Test migration by checking database contents",
    )
    parser.add_argument(
        "--schema-upgrade",
        action="store_true",
        help="Run schema upgrades on the database (e.g., remove redundant tables)",
    )

    args = parser.parse_args()

    if args.debug:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.INFO)

    if args.migrate_db:
        try:
            # First ensure data directory exists
            from src.local_deep_research.setup_data_dir import setup_data_dir

            setup_data_dir()

            # Then run the migration
            from src.local_deep_research.web.database.migrate_to_ldr_db import (
                migrate_to_ldr_db,
            )

            print("Starting database migration...")
            success = migrate_to_ldr_db()
            if success:
                print("Database migration completed successfully")
                sys.exit(0)
            else:
                print("Database migration failed")
                sys.exit(1)
        except Exception as e:
            print(f"Error running database migration: {e}")
            sys.exit(1)

    if args.test_migration:
        try:
            from src.local_deep_research.test_migration import main as test_main

            sys.exit(test_main())
        except Exception as e:
            print(f"Error running migration test: {e}")
            sys.exit(1)

    if args.schema_upgrade:
        try:
            from src.local_deep_research.web.database.schema_upgrade import (
                run_schema_upgrades,
            )

            print("Running database schema upgrades...")
            success = run_schema_upgrades()
            if success:
                print("Schema upgrades completed successfully")
                sys.exit(0)
            else:
                print("Schema upgrades failed")
                sys.exit(1)
        except Exception as e:
            print(f"Error running schema upgrades: {e}")
            sys.exit(1)

    if args.web:
        from src.local_deep_research.web.app import main as web_main

        web_main()
    else:
        # Default to web if no command specified
        from src.local_deep_research.web.app import main as web_main

        web_main()
