#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.11"
# dependencies = [
#     "typer",
#     "httpx",
# ]
# ///
"""
Check the status of an agent orchestrator session.

Returns: running, finished, or not_existent
"""

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent / "lib"))

import typer

app = typer.Typer(add_completion=False)


@app.command()
def main(
    session_name: str = typer.Argument(..., help="Name of the session"),
):
    """
    Check the status of a session.

    Outputs one of: running, finished, not_existent

    Examples:
        ao-status mysession
    """
    from config import get_api_url
    from session_client import SessionClient, SessionClientError

    try:
        # Get API URL
        api_url = get_api_url()

        # Get session status via API
        try:
            client = SessionClient(api_url)
            session = client.get_session_by_name(session_name)
            if session:
                status = client.get_status(session['session_id'])
                print(status)
            else:
                print("not_existent")
        except SessionClientError as e:
            print(f"Error: Failed to connect to session manager: {e}", file=sys.stderr)
            raise typer.Exit(1)

    except Exception as e:
        # Unexpected errors
        print(f"Error: {e}", file=sys.stderr)
        raise typer.Exit(1)


if __name__ == "__main__":
    app()
