"""`acdc k8s` — restart Gen3 microservices / ETL via ArgoCD (LOCAL only).

These use ``argocd login --sso`` (a browser flow), so they cannot run headless
on EC2.
"""
from __future__ import annotations

import typer

from acdc_aws_etl_pipeline.cli._internal import runner
from acdc_aws_etl_pipeline.cli._internal.resolve import env_of, load_cfg

app = typer.Typer(no_args_is_help=True, help="ArgoCD / Kubernetes restarts (local).")

_SCHEMA = "services/k8s_ops/argocd_restart_schema.sh"
_ETL = "services/k8s_ops/argocd_restart_etl.sh"
_ETL_AND_MS = "services/k8s_ops/restart_etl_and_ms.sh"


@app.command(name="restart-schema")
def restart_schema(
    env: str = typer.Option(..., "--env", "-e", help="Environment profile."),
    sync: bool = typer.Option(False, "--sync", "-s", help="argocd app sync first."),
) -> None:
    """Restart sheepdog/peregrine/guppy/portal (schema microservices)."""
    cfg = load_cfg()
    e = env_of(cfg, env)
    args = ["-d", e.domain, "-a", e.app_name, "-n", e.namespace]
    if sync:
        args.append("-s")
    runner.run(runner.bash_script(_SCHEMA, *args))


@app.command(name="restart-etl")
def restart_etl(
    env: str = typer.Option(..., "--env", "-e", help="Environment profile."),
    sync: bool = typer.Option(False, "--sync", "-s", help="argocd app sync first."),
) -> None:
    """Create + run the ETL cronjob and wait for completion."""
    cfg = load_cfg()
    env_of(cfg, env)
    args = ["-e", env]
    if sync:
        args.append("-s")
    runner.run(runner.bash_script(_ETL, *args))


@app.command(name="restart-ms")
def restart_ms(
    env: str = typer.Option(..., "--env", "-e", help="Environment profile."),
) -> None:
    """Restart both ETL and schema microservices (wraps restart_etl_and_ms.sh)."""
    cfg = load_cfg()
    env_of(cfg, env)
    runner.run(runner.bash_script(_ETL_AND_MS, env))
