from __future__ import annotations

import argparse
import sys

from src.agent_core import run
from src.env_settings import load_env_candidates, missing_llm_env_keys, supported_env_locations, llm_key_status


def _print_llm_key_warning() -> None:
    load_env_candidates()
    status = llm_key_status()
    missing = missing_llm_env_keys()
    locations = supported_env_locations()
    if status.get("any"):
        if missing:
            print(
                "[langfuse-lens] info: 일부 LLM API 키가 비어있습니다 -> " + ", ".join(missing),
                file=sys.stderr,
            )
        return
    print(
        "[langfuse-lens] WARNING: LLM API 키가 설정되지 않았습니다. "
        f".env 위치: project={locations['project']}, home={locations['home']}",
        file=sys.stderr,
    )


def main() -> None:
    _print_llm_key_warning()
    parser = argparse.ArgumentParser(description="Langfuse Lens CLI")
    parser.add_argument(
        "--message",
        "-m",
        required=False,
        help="Lens query message. Example: 'mode=overview limit=20'",
    )
    args = parser.parse_args()
    message = args.message

    if not message and not sys.stdin.isatty():
        message = sys.stdin.read().strip()

    if not message:
        parser.error("--message/-m is required (or pass message via stdin)")

    print(run(message))


if __name__ == "__main__":
    main()
