"""albus CLI - Pathway-first command line interface.

Usage:
    albus                              # Launch studio (default)
    albus init                         # Initialize workspace
    albus run pathway.yaml             # Run a pathway directly
    albus chat "message"               # One-shot chat with Host
    albus server                       # Start the server
    albus pathway list                 # List pathways
    albus tool web.search --query X    # Call a tool directly
    albus --help                       # Show all commands

The CLI is organized around pathways as the universal composition language.
"""

from __future__ import annotations

import os
import sys
from pathlib import Path


def main() -> int:
    """Main CLI entry point."""
    from dotenv import load_dotenv

    project_root = Path(__file__).parent.parent.parent.parent.parent
    for env_file in [project_root / ".env", project_root / ".env.local"]:
        if env_file.exists():
            load_dotenv(env_file, override=True)

    # No args -> launch studio
    if len(sys.argv) == 1:
        from server.transport.studio import run_studio

        host = os.getenv("ALBUS_HOST", "127.0.0.1")
        port = int(os.getenv("ALBUS_PORT", "8080"))
        return run_studio(host=host, port=port, thread_id="default")

    from server.transport.cli._parser import build_parser, dispatch

    parser = build_parser()
    args = parser.parse_args()
    return dispatch(args)


__all__ = ["main"]


if __name__ == "__main__":
    sys.exit(main())
