"""Root Typer app + callback + global options for session-hub."""

import typer

from cli_framework import ForceColorOption, NoColorOption, create_app, get_console, setup_logging
from session_hub_cli import __version__
from session_hub_cli.context import SessionHubContext

app = create_app(
    name="session-hub",
    help="Sync and query the central AI session database.",
)

# Register commands by importing their modules (each decorates @app.command)
from session_hub_cli.commands import delete_host as _delete_host  # noqa: E402,F401
from session_hub_cli.commands import hosts as _hosts  # noqa: E402,F401
from session_hub_cli.commands import query as _query  # noqa: E402,F401
from session_hub_cli.commands import stats as _stats  # noqa: E402,F401
from session_hub_cli.commands import sync as _sync  # noqa: E402,F401


@app.callback()
def main(
    ctx: typer.Context,
    verbose: bool = typer.Option(False, "--verbose", "-v", help="Enable verbose logging"),
    no_color: NoColorOption = False,
    color: ForceColorOption = False,
    version: bool = typer.Option(False, "--version", "-V", help="Show version and exit"),
) -> None:
    """Sync and query the central AI session database."""
    console = get_console()
    if version:
        console.print(f"session-hub {__version__}")
        raise typer.Exit()
    setup_logging(verbose)
    ctx.obj = SessionHubContext(console=console, verbose=verbose)


def run() -> None:
    """Run the CLI (for script entry point)."""
    app()


if __name__ == "__main__":
    run()
