"""CLI command for trajectory web viewer."""

from pathlib import Path
from typing import Annotated

from typer import Argument, Option

from harbor.viewer.server import start_server


def view_command(
    folder: Annotated[
        Path,
        Argument(
            help="Folder containing job/trial directories with trajectories",
        ),
    ],
    port: Annotated[
        int,
        Option(
            "--port",
            "-p",
            help="Port to run the server on",
        ),
    ] = 8080,
    host: Annotated[
        str,
        Option(
            "--host",
            help="Host to bind the server to",
        ),
    ] = "127.0.0.1",
    no_browser: Annotated[
        bool,
        Option(
            "--no-browser",
            help="Don't open browser automatically",
        ),
    ] = False,
) -> None:
    """Start a web server to browse and view trajectories.

    Scans the specified folder for jobs/trials containing trajectory files
    and serves an interactive web UI for browsing them.

    Example usage:
        harbor view ./jobs
        harbor view ./jobs/2025-11-14__15-57-06 --port 9000
        harbor view ./trials --no-browser
    """
    if not folder.exists():
        print(f"Error: Folder not found: {folder}")
        raise SystemExit(1)

    start_server(
        folder=folder.resolve(),
        host=host,
        port=port,
        open_browser=not no_browser,
    )
