import asyncio
import os
import sys

# Add the src directory to Python path for local testing
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../src')))

from toporag import TopoAnalyzer

async def main():
    if not os.getenv("OPENAI_API_KEY"):
        print("Please set the OPENAI_API_KEY environment variable.")
        return

    analyzer = TopoAnalyzer()
    
    # Text that is explicitly missing the "middle" of a development lifecycle story
    sample_text = """
    We started the project with great enthusiasm. The team was assembled, 
    requirements were gathered, and we had a solid plan for the architecture.
    
    Finally, we deployed the application to production and celebrated our success. 
    The customers loved the final result and our metrics improved dramatically.
    """
    
    print("Analyzing text for topological gaps...")
    try:
        gaps = await analyzer.analyze_text(sample_text)
        
        if not gaps:
            print("No significant gaps detected.")
            return
            
        print("\n--- Detected Gaps ---")
        for gap in gaps:
            print(f"Topic: {gap['topic_label']} (Persistence: {gap['persistence']:.3f})")
            print(f"Explanation: {gap['explanation']}")
            print(f"Suggestion: {gap.get('suggested_content', 'None')}")
            print("-" * 20)
            
    except Exception as e:
        print(f"Error during analysis: {e}")

if __name__ == "__main__":
    asyncio.run(main())
