"""Basic chat usage examples

Demonstrates the core chat features of the Octen SDK.
"""

from octen import Octen
from octen.models import ChatMessage, WebSearchOptions
from octen.models.chat import ChatFullContentOptions


def main():
    with Octen(api_key="octen-your-key") as client:
        print("=" * 80)
        print("Octen SDK -- Chat Basic Usage Examples")
        print("=" * 80)

        # -- 1. Simple non-streaming chat --
        print("\n1. Simple Non-Streaming Chat")
        print("-" * 80)

        response = client.chat.create(
            model="openai/gpt-5.4",
            messages=[ChatMessage(role="user", content="Explain Python in one sentence")],
            web_search="off",
            max_tokens=100,
        )

        print(f"Model:  {response.model}")
        print(f"Answer: {response.text}")
        if response.usage:
            print(f"Tokens: prompt={response.usage.prompt_tokens}, "
                  f"completion={response.usage.completion_tokens}, "
                  f"total={response.usage.total_tokens}")

        # -- 2. Chat with web search --
        print("\n2. Chat with Web Search")
        print("-" * 80)

        response = client.chat.create(
            model="openai/gpt-5.4",
            messages=[ChatMessage(role="user", content="What are today's top tech news?")],
            web_search="on",
            web_search_options=WebSearchOptions(safesearch="off"),
        )

        print(f"Answer: {response.text[:200]}...")
        if response.search_results:
            print(f"\nSearch results ({len(response.search_results)} query groups):")
            for group in response.search_results:
                print(f"  Query: {group.query}")
                for item in group.results[:2]:
                    print(f"    - {item.title}")
                    print(f"      {item.url}")

        # -- 3. System message --
        print("\n3. System Message + User Message")
        print("-" * 80)

        response = client.chat.create(
            model="openai/gpt-5.4",
            messages=[
                ChatMessage(role="system", content="You are a concise assistant. Answer in two sentences max."),
                ChatMessage(role="user", content="What is machine learning?"),
            ],
            web_search="off",
        )
        print(f"Answer: {response.text}")

        # -- 4. Multi-turn conversation --
        print("\n4. Multi-Turn Conversation")
        print("-" * 80)

        messages = [
            ChatMessage(role="user", content="Recommend a Python beginner book"),
        ]
        resp1 = client.chat.create(model="openai/gpt-5.4", messages=messages, web_search="off")
        print(f"Turn 1: {resp1.text}")

        messages.append(ChatMessage(role="assistant", content=resp1.text))
        messages.append(ChatMessage(role="user", content="Is this book suitable for absolute beginners?"))

        resp2 = client.chat.create(model="openai/gpt-5.4", messages=messages, web_search="off")
        print(f"Turn 2: {resp2.text}")

        # -- 5. Full page content --
        print("\n5. Web Search with Full Page Content")
        print("-" * 80)

        response = client.chat.create(
            model="openai/gpt-5.4",
            messages=[ChatMessage(role="user", content="What are the new features in Python 3.13?")],
            web_search="on",
            web_search_options=WebSearchOptions(
                safesearch="off",
                full_content=ChatFullContentOptions(enable=True, max_tokens=500),
            ),
        )
        print(f"Answer: {response.text[:300]}...")
        if response.usage:
            print(f"Full content tokens: {response.usage.full_content_tokens}")


if __name__ == "__main__":
    main()
