"""Top-level CLI entry point."""

import click

from bincio import __version__


@click.group()
@click.version_option(__version__)
def main() -> None:
    """BincioActivity — federated, open-source activity stats."""


from bincio.extract.cli import extract          # noqa: E402
from bincio.render.cli import render            # noqa: E402
from bincio.edit.cli import edit                # noqa: E402
from bincio.import_.cli import import_group     # noqa: E402
from bincio.serve.init_cmd import init          # noqa: E402
from bincio.serve.cli import serve              # noqa: E402
from bincio.dev import dev                      # noqa: E402
from bincio.reextract_cmd import reextract_originals  # noqa: E402
from bincio.sync_strava import sync_strava_cmd        # noqa: E402
from bincio.sync_garmin import sync_garmin_cmd        # noqa: E402
from bincio.segments.cli import segments_group        # noqa: E402


@main.command("bake-tracks")
@click.option("--data-dir", required=True, help="BAS data store directory.")
@click.option("--handle", default=None, help="Bake one user only (default: all).")
def bake_tracks_cmd(data_dir: str, handle: str | None) -> None:
    """Pre-bake GPS tracks.json for the Explore heatmap page."""
    from pathlib import Path
    from bincio.explore import bake_tracks
    from bincio.render.cli import _user_dirs
    from rich.console import Console
    console = Console()
    data = Path(data_dir).expanduser().resolve()
    targets = [data / handle] if handle else _user_dirs(data)
    for user_dir in targets:
        n = bake_tracks(user_dir.name, data)
        console.print(f"  [cyan]{user_dir.name}[/cyan]: {n} track(s) baked")


main.add_command(extract)
main.add_command(render)
main.add_command(edit)
main.add_command(import_group)
main.add_command(init)
main.add_command(serve)
main.add_command(dev)
main.add_command(reextract_originals)
main.add_command(sync_strava_cmd)
main.add_command(sync_garmin_cmd)
main.add_command(segments_group)
