#!/usr/bin/env python3
"""
Semantic Agent Selection Example

Demonstrates intelligent agent selection using embeddings and semantic similarity.
This provides much better accuracy than keyword matching.
"""

import os
import sys
from pathlib import Path

# Add parent directory to path for development
sys.path.insert(0, str(Path(__file__).parent.parent.parent))

from claude_force import AgentOrchestrator


def main():
    """Demonstrate semantic agent selection"""

    # Check for API key (not needed for recommendations, only for running agents)
    api_key = os.getenv("ANTHROPIC_API_KEY")
    if not api_key:
        print("⚠️  Warning: ANTHROPIC_API_KEY not set")
        print("   Recommendations will work, but you won't be able to run agents")

    print("=" * 70)
    print("SEMANTIC AGENT SELECTION EXAMPLE")
    print("=" * 70 + "\n")

    # Initialize orchestrator
    try:
        orchestrator = AgentOrchestrator()
        print("✅ AgentOrchestrator initialized\n")
    except Exception as e:
        print(f"❌ Failed to initialize orchestrator: {e}")
        sys.exit(1)

    # Test cases with different types of tasks
    test_cases = [
        {
            "name": "Security Review",
            "task": "Review this authentication code for SQL injection vulnerabilities and OWASP Top 10 issues",
        },
        {
            "name": "Bug Investigation",
            "task": "Users are seeing 500 errors when uploading files. The logs show 'Connection timeout' after 30 seconds",
        },
        {
            "name": "Frontend Feature",
            "task": "Build a responsive navigation menu with dropdown submenus using React and Tailwind CSS",
        },
        {
            "name": "API Design",
            "task": "Design a RESTful API for user management with authentication, pagination, and filtering",
        },
        {
            "name": "Database Schema",
            "task": "Design database schema for an e-commerce platform with products, orders, and inventory tracking",
        },
    ]

    # Test each case
    for i, test_case in enumerate(test_cases, 1):
        print(f"\n{'=' * 70}")
        print(f"Test Case {i}: {test_case['name']}")
        print(f"{'=' * 70}\n")
        print(f"Task: {test_case['task'][:100]}...")

        try:
            # Get recommendations
            recommendations = orchestrator.recommend_agents(
                test_case["task"], top_k=3, min_confidence=0.2  # Lower threshold for demo
            )

            if not recommendations:
                print("❌ No agents matched this task")
                continue

            print(f"\n📊 Top {len(recommendations)} Recommendations:\n")

            for j, rec in enumerate(recommendations, 1):
                confidence_pct = rec["confidence"] * 100
                bar_length = int(confidence_pct / 5)
                bar = "█" * bar_length + "░" * (20 - bar_length)

                # Emoji based on confidence
                if confidence_pct >= 70:
                    emoji = "🟢"
                elif confidence_pct >= 50:
                    emoji = "🟡"
                else:
                    emoji = "🔴"

                print(f"{j}. {emoji} {rec['agent']}")
                print(f"   Confidence: {bar} {confidence_pct:.1f}%")
                print(f"   Reasoning: {rec['reasoning']}")
                print(f"   Domains: {', '.join(rec['domains'])}")
                print()

            # Get detailed explanation for top choice
            top_agent = recommendations[0]["agent"]
            explanation = orchestrator.explain_agent_selection(test_case["task"], top_agent)

            print(f"💡 Why '{top_agent}' was selected:")
            print(f"   Rank: #{explanation.get('rank', 'N/A')}")
            print(f"   Confidence: {explanation.get('confidence', 0):.3f}")
            print(f"   Reasoning: {explanation.get('reasoning', 'N/A')}")

        except ImportError:
            print("\n❌ Error: sentence-transformers not installed")
            print("   Install with: pip install sentence-transformers")
            sys.exit(1)
        except Exception as e:
            print(f"\n❌ Error: {e}")
            continue

    # Comparison: Explain why wrong agent wasn't selected
    print(f"\n{'=' * 70}")
    print("NEGATIVE EXAMPLE: Wrong Agent")
    print(f"{'=' * 70}\n")

    task = "Fix critical security vulnerability in authentication system"
    wrong_agent = "ui-components-expert"  # Not suitable for security

    explanation = orchestrator.explain_agent_selection(task, wrong_agent)

    print(f"Task: {task}")
    print(f"\nWhy '{wrong_agent}' WASN'T selected:")
    print(f"   Selected: {'Yes' if explanation['selected'] else 'No'}")
    print(f"   Rank: #{explanation.get('rank', 'N/A')}")
    print(f"   Confidence: {explanation.get('confidence', 0):.3f}")

    if "all_candidates" in explanation:
        print(f"\n   Better candidates:")
        for candidate in explanation["all_candidates"][:5]:
            print(f"      • {candidate['agent']}: {candidate['confidence']:.3f}")

    # Summary
    print(f"\n{'=' * 70}")
    print("BENEFITS OF SEMANTIC SELECTION")
    print(f"{'=' * 70}\n")

    print("✅ Understands context, not just keywords")
    print("✅ Provides confidence scores for transparency")
    print("✅ Explains why agents were selected")
    print("✅ Handles ambiguous or complex tasks better")
    print("✅ Improves accuracy from ~60% to ~90%")

    print(f"\n{'=' * 70}")
    print("USAGE IN YOUR CODE")
    print(f"{'=' * 70}\n")

    print(
        """
# Get recommendations
recommendations = orchestrator.recommend_agents(
    task="Your task description",
    top_k=3,
    min_confidence=0.3
)

# Use top recommendation
if recommendations:
    top_agent = recommendations[0]['agent']
    confidence = recommendations[0]['confidence']

    print(f"Using {top_agent} (confidence: {confidence:.2f})")

    # Run the agent
    result = orchestrator.run_agent(top_agent, task=task)
    """
    )

    print("\n" + "=" * 70)


if __name__ == "__main__":
    main()
