"""Root ``acdc`` Typer application.

Assembles every command group and the top-level ``version`` / ``docs`` helpers.
The console-script entry point in pyproject.toml points at :func:`main`.
"""
from __future__ import annotations

import typer

from acdc_aws_etl_pipeline.cli import (
    config_cmds,
    delete_cmds,
    dict_cmds,
    ec2_cmds,
    indexd_cmds,
    jobs,
    k8s,
    metadata,
    synth,
)

app = typer.Typer(
    no_args_is_help=True,
    rich_markup_mode="rich",
    help="ACDC ETL pipeline operations. Run [bold]acdc docs[/bold] for an overview.",
)

app.add_typer(dict_cmds.app, name="dict")
app.add_typer(synth.app, name="synth")
app.add_typer(metadata.app, name="metadata")
app.add_typer(delete_cmds.app, name="delete")
app.add_typer(k8s.app, name="k8s")
app.add_typer(indexd_cmds.app, name="indexd")
app.add_typer(ec2_cmds.app, name="ec2")
app.add_typer(jobs.app, name="jobs")
app.add_typer(config_cmds.app, name="config")


_DOCS = """\
ACDC ETL pipeline — operations overview
=======================================

Mental model: two execution planes
  - Control plane (LOCAL): dict deploy, k8s restarts. These use the interactive
    `argocd login --sso` browser flow and AWS named profiles, so they run on
    your laptop only.
  - Data plane (LONG jobs): metadata upload/delete, indexd register. Add
    `--on ec2` to run them on the EC2 box via SSM (disconnect-safe); watch with
    `acdc jobs status|logs <run-id> --follow`.

Discover everything
  acdc --help                      list all command groups
  acdc <group> --help              commands + options for a group
  acdc config envs                 valid --env values
  acdc config studies              valid --study values
  acdc config show --env test      resolved settings (safe, read-only)

Typical release runbook (staging shown; repeat for prod with care)
  1. acdc dict deploy   --env staging
  2. acdc metadata upload --study <study> --env staging --on ec2
  3. acdc jobs logs <run-id> --follow
  4. acdc k8s restart-etl --env staging

Synthetic data (test only, all local)
  acdc synth deploy --env test

EC2 / SSM prerequisites
  - Instance role: AmazonSSMManagedInstanceCore (+ existing Athena/S3/Secrets).
  - Local profile: ssm:SendCommand / ssm:GetCommandInvocation, s3:GetObject on
    the log prefix, ec2:Start/Stop/DescribeInstances.
  - Set `ec2_instance_id` on the *_ec2 env blocks in config/deploy_config.yaml.

NOT run by this CLI (see docs/cli.md): the Glue jobs (ingestion, validation,
release-JSON) and the CodeBuild release writer. Those are deployed separately.
"""


@app.command()
def docs() -> None:
    """Print the operations overview (mental model, runbook, prerequisites)."""
    typer.echo(_DOCS)


@app.command()
def version() -> None:
    """Print the installed acdc_aws_etl_pipeline version."""
    try:
        from importlib.metadata import version as _v

        typer.echo(_v("acdc_aws_etl_pipeline"))
    except Exception:  # pragma: no cover - fallback when not installed
        typer.echo("unknown")


def main() -> None:
    """Console-script entry point."""
    app()


if __name__ == "__main__":
    main()
