// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/ubuntu
{
	"name": "physical-ai-toolchain-devcontainer",
	// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
	"build": {
		"dockerfile": "Dockerfile",
		"args": {
			"NPM_CONFIG_REGISTRY": "${localEnv:NPM_CONFIG_REGISTRY}",
			"PIP_INDEX_URL": "${localEnv:PIP_INDEX_URL}",
			"UV_DEFAULT_INDEX": "${localEnv:UV_DEFAULT_INDEX}"
		}
	},
	// Features to add to the dev container. More info: https://containers.dev/features.
	"features": {
		"ghcr.io/devcontainers/features/common-utils:2": {},
		"ghcr.io/devcontainers/features/docker-in-docker:2": {},
		"ghcr.io/devcontainers/features/git:1": {},
		"ghcr.io/devcontainers/features/azure-cli:1": {
			"extensions": "connectedk8s,k8s-extension,azure-iot-ops,amg,eventgrid,ml"
		},
		"ghcr.io/devcontainers/features/github-cli:1": {},
		"ghcr.io/devcontainers/features/node:1": {},
		"ghcr.io/devcontainers/features/go:1": {},
		"ghcr.io/devcontainers/features/terraform:1": {
			"installTerraformDocs": true
		},
		"ghcr.io/devcontainers/features/kubectl-helm-minikube:1": {
 			// "version": "latest" uses https://dl.k8s.io/release/stable.txt which experiences outages during heavy load.
			// Update this back to "latest" or continue to upgrade the version periodically.
			"version": "1.33.2",
			"helm": "latest",
			"minikube": "none"
		},
		"ghcr.io/rio/features/k9s:1": {},
		"ghcr.io/devcontainers/features/powershell:1": {}
	},
	"customizations": {
		"vscode": {
			"settings": {
				"terminal.integrated.profiles.linux": {
					"bash": {
						"path": "/bin/bash"
					}
				},
				"[python]": {
					"editor.defaultFormatter": "charliermarsh.ruff",
					"editor.codeActionsOnSave": {
						"source.fixAll": "explicit",
						"source.organizeImports": "explicit"
					}
				}
			},
			"extensions": [
				"charliermarsh.ruff",
				"darkriszty.markdown-table-prettify",
				"DavidAnson.vscode-markdownlint",
				"eamodio.gitlens",
				"EditorConfig.EditorConfig",
				"foxundermoon.shell-format",
				"GitHub.vscode-pull-request-github",
				"hashicorp.terraform",
				"ms-azuretools.vscode-azureterraform",
				"ms-azuretools.vscode-docker",
				"ms-python.debugpy",
				"ms-python.python",
				"ms-python.vscode-pylance",
				"ms-vscode-remote.vscode-remote-extensionpack",
				"ms-vscode.azurecli",
				"streetsidesoftware.code-spell-checker",
				"streetsidesoftware.code-spell-checker-cspell-bundled-dictionaries",
				"timonwong.shellcheck",
				"yzhang.markdown-all-in-one",
				"ms-azuretools.vscode-azure-github-copilot",
				"ms-kubernetes-tools.vscode-kubernetes-tools",
				"github.vscode-github-actions",
				"bierner.markdown-mermaid",
				"ms-azuretools.vscode-azurefunctions",
				"ms-azuretools.vscode-azureresourcegroups",
				"ms-azuretools.vscode-containers",
				"tamasfe.even-better-toml",
				"redhat.vscode-yaml",
				"ms-toolsai.vscode-ai",
				"GitHub.copilot@prerelease",
				"GitHub.copilot-chat@prerelease",
				"ise-hve-essentials.hve-core-all"
			]
		}
	},

	// Use 'forwardPorts' to make a list of ports inside the container available locally.
	"forwardPorts": [8080],

	// GPU device nodes are group-restricted: /dev/nvmap is root:video and
	// /dev/dri/renderD* is root:render (GID 993 here). CUDA initialization fails
	// with error 801 unless the container user holds both; adjust 993 if the host
	// render group differs. "gpu": "optional" attaches devices when present and
	// still builds on GPU-less hosts.
	"runArgs": [
		"--group-add",
		"video",
		"--group-add",
		"993"
	],
	"hostRequirements": {
		"gpu": "optional"
	},

	// Container environment variables
	"containerEnv": {
		"BROWSER": "code --new-window",
		"UV_LINK_MODE": "copy",
		"NVIDIA_VISIBLE_DEVICES": "all",
		"NVIDIA_DRIVER_CAPABILITIES": "all"
	},

	"onCreateCommand": {
		"setup-dev": "./setup-dev.sh --disable-venv",
		"npm-install": "npm install",
		"apt": "sudo apt update && sudo apt install -y shellcheck jq unzip ffmpeg postgresql-client redis-tools",
		"tflint": "TFLINT_VERSION=v0.64.0 && case \"$(uname -m)\" in x86_64) TFLINT_ARCH=amd64; TFLINT_SHA256=cca9d13e2e1d7a2c627af60ff899a3c9b74212899416aeb96ec764d2ef954537 ;; aarch64|arm64) TFLINT_ARCH=arm64; TFLINT_SHA256=560da89aacf59389d4eb029730dd5b109b7288096c32f2726a0d9e783a5ea8eb ;; *) echo \"Unsupported arch: $(uname -m)\" >&2; exit 1 ;; esac && curl -fsSL \"https://github.com/terraform-linters/tflint/releases/download/${TFLINT_VERSION}/tflint_linux_${TFLINT_ARCH}.zip\" -o /tmp/tflint.zip && echo \"${TFLINT_SHA256}  /tmp/tflint.zip\" | sha256sum -c --quiet - && sudo unzip -o /tmp/tflint.zip -d /usr/local/bin && rm /tmp/tflint.zip && tflint --init",
		"actionlint": "./scripts/setup/install-actionlint.sh",
		"golangci-lint": "GOLANGCI_LINT_VERSION=2.13.2 && case \"$(uname -m)\" in x86_64) GOLANGCI_LINT_ARCH=amd64; GOLANGCI_LINT_SHA256=2277d43b98ec0054280f2ac26b53268bae97682444678a59a657dd565da021d6 ;; aarch64|arm64) GOLANGCI_LINT_ARCH=arm64; GOLANGCI_LINT_SHA256=a2a4e0065aa41be71f7c5ac90f271b61751331e5d04314e62afe4027855f0893 ;; *) echo \"Unsupported arch: $(uname -m)\" >&2; exit 1 ;; esac && curl -fsSL \"https://github.com/golangci/golangci-lint/releases/download/v${GOLANGCI_LINT_VERSION}/golangci-lint-${GOLANGCI_LINT_VERSION}-linux-${GOLANGCI_LINT_ARCH}.tar.gz\" -o /tmp/golangci-lint.tar.gz && echo \"${GOLANGCI_LINT_SHA256}  /tmp/golangci-lint.tar.gz\" | sha256sum -c --quiet - && sudo tar -xzf /tmp/golangci-lint.tar.gz -C /usr/local/bin --strip-components=1 \"golangci-lint-${GOLANGCI_LINT_VERSION}-linux-${GOLANGCI_LINT_ARCH}/golangci-lint\" && rm /tmp/golangci-lint.tar.gz",
		"update-bashrc": "echo 'export PATH=\"${containerWorkspaceFolder}/scripts:${containerWorkspaceFolder}/node_modules/.bin:$PATH\"' | sudo tee -a  ~/.bashrc",
		"osmo-cli": "OSMO_VERSION=6.3.0 && case \"$(uname -m)\" in x86_64) OSMO_ARCH=x86_64; OSMO_INSTALLER_SHA256=e2e70b4868fe2ee22c7952f2d53d1e013070168b77e89f51e8c52a0eaaf18928 ;; aarch64|arm64) OSMO_ARCH=arm64; OSMO_INSTALLER_SHA256=03d559d8a999b4d6f2ac6aac989d17792a47aaee70b5c7fc5787655f123923ee ;; *) echo \"Unsupported arch: $(uname -m)\" >&2; exit 1 ;; esac && curl -fsSL \"https://github.com/NVIDIA/OSMO/releases/download/${OSMO_VERSION}/osmo-client-installer-${OSMO_VERSION}-linux-${OSMO_ARCH}.sh\" -o /tmp/osmo_install.sh && echo \"${OSMO_INSTALLER_SHA256}  /tmp/osmo_install.sh\" | sha256sum -c --quiet - && sudo bash /tmp/osmo_install.sh && rm /tmp/osmo_install.sh",
		"ngc-cli": "if [ \"$(uname -m)\" != \"x86_64\" ]; then echo \"Skipping ngc-cli: no $(uname -m) build available (x86_64 only)\"; exit 0; fi && NGC_CLI_VERSION=3.41.4 && NGC_CLI_SHA256=2c86681048ab8e2980bdd6aa6c17f086eff988276ad90a28f1307b69fdb50252 && curl -fsSL \"https://api.ngc.nvidia.com/v2/resources/nvidia/ngc-apps/ngc_cli/versions/${NGC_CLI_VERSION}/files/ngccli_linux.zip\" -o /tmp/ngccli.zip && echo \"${NGC_CLI_SHA256}  /tmp/ngccli.zip\" | sha256sum -c --quiet - && sudo unzip -o /tmp/ngccli.zip -d /usr/local && rm /tmp/ngccli.zip && sudo chmod u+x /usr/local/ngc-cli/ngc && sudo ln -sf /usr/local/ngc-cli/ngc /usr/local/bin/ngc",
		"az-ml": "az extension add -n ml --yes || true"
	},

	// DONT USE 'postCreateCommand', it ruins your global .gitconfig https://github.com/devcontainers/cli/issues/787

	// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
	"remoteUser": "vscode"
}
