from .search_system import AdvancedSearchSystem
from typing import Dict
from .config import settings

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(f"\nReport has been saved to report.md")


from .report_generator import IntegratedReportGenerator

report_generator = IntegratedReportGenerator()



def main():
    import os
    import logging
    from .utilties.setup_utils import setup_user_directories
    
    # Configure logging
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger(__name__)
    logger.info(f"Starting with settings: iterations={settings.search.iterations}, "
                f"questions_per_iteration={settings.search.questions_per_iteration}")
        
    # Explicitly run setup
    logger.info("Initializing configuration...")
    setup_user_directories()
    
    system = AdvancedSearchSystem()

    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
                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.")

if __name__ == "__main__":
    main()