import os
from pathlib import Path

import typer as t

from remotivelabs.cli.topology.context import ContainerEngine, TopologyContext
from remotivelabs.cli.topology.typer import (
    ContainerEngineOption,
    TopologyCommandOption,
    TopologyImageOption,
)
from remotivelabs.cli.topology.workspace import find_topology_workspace
from remotivelabs.cli.topology.wrapper import app
from remotivelabs.cli.utils.analytics import require_consent


@app.callback()
def service_callback(
    ctx: t.Context,
    topology_cmd: str = TopologyCommandOption,
    topology_image: str = TopologyImageOption,
    container_engine: ContainerEngine = ContainerEngineOption,
) -> None:
    require_consent()
    ctx.obj = ctx.obj or {}
    working_directory = Path(os.curdir).absolute().resolve()
    ctx.obj["topology"] = TopologyContext(
        container_engine=container_engine,
        cmd=topology_cmd,
        workspace=find_topology_workspace(working_directory),
        working_directory=working_directory,
        image=topology_image,
    )


__all__ = ["app"]
