"""py-cml Quickstart — store and retrieve memories in 30 seconds.

Set CML_API_KEY and CML_BASE_URL in .env.
"""

import os
from pathlib import Path

try:
    from dotenv import load_dotenv

    load_dotenv(Path(__file__).resolve().parent.parent / ".env")
except ImportError:
    pass

from cml import CognitiveMemoryLayer


def main():
    base_url = (os.environ.get("CML_BASE_URL") or "").strip() or "http://localhost:8000"
    with CognitiveMemoryLayer(
        api_key=os.environ.get("CML_API_KEY"),
        base_url=base_url,
    ) as memory:
        memory.write("User prefers vegetarian food and lives in Paris.")
        memory.write("User works at a tech startup as a backend engineer.")
        memory.write("User has a meeting with the design team every Tuesday.")
        result = memory.read("What does the user do for work?")
        print(f"Found {result.total_count} relevant memories:")
        for item in result.memories:
            print(f"  [{item.type}] {item.text}")
            print(f"    Relevance: {item.relevance:.2f}, Confidence: {item.confidence:.2f}")

        print("\nStreaming memories (SSE):")
        for item in memory.read_stream("Tell me about the user"):
            print(f"  Streamed: {item.text}")

        context = memory.get_context("dietary restrictions")
        print(f"\nLLM Context:\n{context}")
        stats = memory.stats()
        print(f"\nMemory Stats: {stats.total_memories} memories stored")


if __name__ == "__main__":
    main()
