"""
Test GCRI with a web search task to verify web_search tool works correctly.
"""
import sys
sys.path.insert(0, '.')

from dotenv import load_dotenv
load_dotenv()

from gcri.config import scope as gcri_scope
from gcri.graphs.gcri_unit import GCRI


@gcri_scope.observe('web_search_test', priority=0, default=True, chain_with='no_reasoning')
def web_search_test(config):
    """Test web search functionality."""
    config.protocols.max_iterations = 2
    config.branches_generator_type = 'adaptive'
    config.num_branches = 1
    # Note: use_web_search=True is already set by default in config.py
    return config


@gcri_scope
def main(config):
    # Task requiring web search
    search_task = """
Task: Research and summarize Python 3.14 new features

Search the web to find information about Python 3.14's new features and provide:
1. A brief summary of the most important new features
2. The release date or schedule
3. Any notable breaking changes

Write your findings to a file called 'python314_summary.md'.
"""

    print('=' * 60)
    print('GCRI Web Search Test')
    print('=' * 60)
    print(f'Generator Type: {config.branches_generator_type}')
    print('=' * 60)

    gcri = GCRI(config)
    result = gcri(search_task)

    print('\n' + '=' * 60)
    print('Final Result:')
    print('=' * 60)
    print(f"Decision: {result.get('decision')}")
    print(f"Best Branch: {result.get('best_branch')}")
    
    output = result.get('output', '')
    print(f"\nOutput Preview: {output[:1000]}...")

    print('\n✅ Web search test completed.')


if __name__ == '__main__':
    main()
