from pathlib import Path

import click

from vantage6.common.globals import InstanceType

from vantage6.cli.common.decorator import click_insert_context
from vantage6.cli.common.start import execute_cli_start
from vantage6.cli.context.hq import HQContext
from vantage6.cli.globals import CLICommandName
from vantage6.cli.k8s_config import select_k8s_config


@click.command()
@click.option("--context", default=None, help="Kubernetes context to use")
@click.option("--namespace", default=None, help="Kubernetes namespace to use")
@click.option(
    "--local-chart-dir",
    type=click.Path(exists=True),
    default=None,
    help="Local chart repository to use.",
)
@click_insert_context(type_=InstanceType.HQ)
def cli_hub_start(
    ctx: HQContext,
    context: str | None,
    namespace: str | None,
    local_chart_dir: Path | None,
) -> None:
    """
    Start a hub environment.
    """
    k8s_config = select_k8s_config(context=context, namespace=namespace)

    # First we need to start the keycloak service
    execute_cli_start(
        CLICommandName.AUTH,
        f"{ctx.name}-auth",
        k8s_config,
        local_chart_dir,
        system_folders=False,
        extra_args=["--wait-ready"],
    )

    # run the store. The store is started before the HQ so that the HQ can
    # couple to the store on startup.
    execute_cli_start(
        CLICommandName.ALGORITHM_STORE,
        f"{ctx.name}-store",
        k8s_config,
        local_chart_dir,
        system_folders=False,
    )

    # Then we need to start the HQ
    execute_cli_start(
        CLICommandName.HQ,
        ctx.name,
        k8s_config,
        local_chart_dir,
        system_folders=False,
    )
