"""Basic usage example for the ketju library.

Keep this example intentionally minimal.
For tracing/instrumentation setup, see `ketju.examples.instrumentation_demo`.
"""

from __future__ import annotations

import argparse
import os
from pathlib import Path
import tempfile
from ketju.examples._cli import import_agent_module, missing_optional_deps_message, parse_args

def main(argv: list[str] | None = None) -> None:
    def _configure_parser(parser: argparse.ArgumentParser) -> None:
        parser.add_argument(
            "--log-level",
            default=os.getenv("KETJU_LOG_LEVEL", "WARNING"),
            help="Root log level (default: WARNING).",
        )
        parser.add_argument(
            "--no-quiet",
            action="store_true",
            help="Do not silence third-party logs (llama_index/chromadb/httpx/openai).",
        )

    args = parse_args(
        description="Run the ketju basic usage CLI example.",
        argv=argv,
        configure=_configure_parser,
    )

    try:
        from ketju.rag import SimplePdfRag
        agent_module = import_agent_module()
        run_cli_agent = agent_module.run_cli_agent
        configure_example_logging = agent_module.configure_example_logging
    except ModuleNotFoundError as e:
        raise SystemExit(
            missing_optional_deps_message(
                extras="--extra rag --extra agent",
                run_as_module="ketju.examples.basic_usage",
            )
        ) from e

    configure_example_logging(log_level=args.log_level, quiet_third_party=not args.no_quiet)

    with tempfile.TemporaryDirectory() as tmpdir:
        persist_dir = Path(tmpdir)
        srag = SimplePdfRag(name="simple_rag", persist_dir=persist_dir)

        print("Starting the agent...")
        print(
            "You can ask questions about contents in pdf files, "
            "just paste the path to the file in the chat."
        )

        run_cli_agent(srag)


if __name__ == "__main__":
    main()

