"""
agentbudget — Anthropic Usage Example

This example demonstrates how to track the cost of Anthropic API calls
using agentbudget's CostSession context manager and the @track decorator.

Run:
    pip install agentbudget[anthropic]
    export ANTHROPIC_API_KEY=your-key
    python examples/anthropic_example.py
"""

from agentbudget import CostSession, track


def run_session_example() -> None:
    """Demonstrate cost tracking for a multi-step Anthropic pipeline."""

    # Simulated responses (replace with real Anthropic calls)
    class FakeUsage:
        def __init__(self, input_tokens: int, output_tokens: int) -> None:
            self.input_tokens = input_tokens
            self.output_tokens = output_tokens

    class FakeResponse:
        def __init__(self, model: str, input_tokens: int, output_tokens: int) -> None:
            self.model = model
            self.usage = FakeUsage(input_tokens, output_tokens)

    with CostSession(name="research-agent", budget=0.10) as session:
        # Step 1: Search and summarize
        # client = anthropic.Anthropic()
        # response = client.messages.create(
        #     model="claude-3-5-sonnet-20241022",
        #     max_tokens=1024,
        #     messages=[{"role": "user", "content": "Research quantum computing advances"}]
        # )
        response = FakeResponse("claude-3-5-sonnet-20241022", input_tokens=500, output_tokens=800)
        session.track(response, label="research")

        # Step 2: Write report
        response = FakeResponse("claude-3-5-sonnet-20241022", input_tokens=1200, output_tokens=2000)
        session.track(response, label="write")


def run_decorator_example() -> None:
    """Demonstrate cost tracking using the @track decorator."""

    class FakeUsage:
        def __init__(self, input_tokens: int, output_tokens: int) -> None:
            self.input_tokens = input_tokens
            self.output_tokens = output_tokens

    class FakeResponse:
        def __init__(self, model: str) -> None:
            self.model = model
            self.usage = FakeUsage(300, 500)

    @track(label="generate-summary", budget=0.02)
    def generate_summary(topic: str) -> FakeResponse:
        """Generate a summary for the given topic."""
        # return anthropic_client.messages.create(...)
        return FakeResponse("claude-3-5-haiku-20241022")

    result = generate_summary("climate change")
    print(f"\nReturned response model: {result.model}")


if __name__ == "__main__":
    print("=== Session Example ===")
    run_session_example()
    print("\n=== Decorator Example ===")
    run_decorator_example()
