set dotenv-load := true

# =============================================================================
# Release
# =============================================================================

[group('release')]
release *ARGS:
	cd scripts/release && pnpm exec tsx ./main.ts --phase setup-local {{ ARGS }}

# Build a single target via Docker
[group('release')]
release-build target="x86_64-unknown-linux-musl":
	./docker/release/build.sh {{target}}

# Build all release binaries
[group('release')]
release-build-all:
	./docker/release/build.sh x86_64-unknown-linux-musl
	./docker/release/build.sh aarch64-unknown-linux-musl
	./docker/release/build.sh x86_64-pc-windows-gnu
	./docker/release/build.sh x86_64-apple-darwin
	./docker/release/build.sh aarch64-apple-darwin

# =============================================================================
# Development
# =============================================================================

[group('dev')]
dev-daemon:
	SANDBOX_AGENT_SKIP_INSPECTOR=1 cargo run -p sandbox-agent -- daemon start --upgrade

[group('dev')]
dev: dev-daemon
	pnpm dev -F @sandbox-agent/inspector -- --host 0.0.0.0

[group('dev')]
build:
	cargo build -p sandbox-agent

[group('dev')]
test:
	cargo test --all-targets

[group('dev')]
check:
	cargo check --all-targets
	cargo fmt --all -- --check
	pnpm run typecheck

[group('dev')]
fmt:
	cargo fmt --all

[group('dev')]
install-fast-sa:
	SANDBOX_AGENT_SKIP_INSPECTOR=1 cargo build --release -p sandbox-agent
	rm -f ~/.cargo/bin/sandbox-agent
	cp target/release/sandbox-agent ~/.cargo/bin/sandbox-agent

[group('dev')]
install-gigacode:
	SANDBOX_AGENT_SKIP_INSPECTOR=1 cargo build --release -p gigacode
	rm -f ~/.cargo/bin/gigacode
	cp target/release/gigacode ~/.cargo/bin/gigacode

[group('dev')]
run-sa *ARGS:
	SANDBOX_AGENT_SKIP_INSPECTOR=1 cargo run -p sandbox-agent -- {{ ARGS }}

[group('dev')]
run-gigacode *ARGS:
	SANDBOX_AGENT_SKIP_INSPECTOR=1 cargo run -p gigacode -- {{ ARGS }}

[group('dev')]
dev-docs:
	cd docs && pnpm dlx mintlify dev --host 0.0.0.0

install:
    pnpm install
    pnpm build --filter @sandbox-agent/inspector...
    cargo install --path server/packages/sandbox-agent --debug
    cargo install --path gigacode --debug

install-fast:
    SANDBOX_AGENT_SKIP_INSPECTOR=1 cargo install --path server/packages/sandbox-agent --debug
    SANDBOX_AGENT_SKIP_INSPECTOR=1 cargo install --path gigacode --debug

install-release:
    pnpm install
    pnpm build --filter @sandbox-agent/inspector...
    cargo install --path server/packages/sandbox-agent
    cargo install --path gigacode

# =============================================================================
# Foundry
# =============================================================================

[group('foundry')]
foundry-deps:
	pnpm install

[group('foundry')]
foundry-install:
	pnpm install
	pnpm -w build

[group('foundry')]
foundry-typecheck:
	pnpm -w typecheck

[group('foundry')]
foundry-build:
	pnpm -w build

[group('foundry')]
foundry-test:
	pnpm -w test

[group('foundry')]
foundry-check:
	pnpm -w typecheck
	pnpm -w build
	pnpm -w test

[group('foundry')]
foundry-dev:
	pnpm install
	mkdir -p foundry/.foundry/logs
	HF_DOCKER_UID="$(id -u)" HF_DOCKER_GID="$(id -g)" docker compose --env-file .env -f foundry/compose.dev.yaml up --build --force-recreate -d

[group('foundry')]
foundry-preview:
	pnpm install
	mkdir -p foundry/.foundry/logs
	HF_DOCKER_UID="$(id -u)" HF_DOCKER_GID="$(id -g)" docker compose --env-file .env -f foundry/compose.preview.yaml up --build --force-recreate -d

[group('foundry')]
foundry-frontend-dev host='127.0.0.1' port='4173' backend='http://127.0.0.1:7741/api/rivet':
	pnpm install
	VITE_HF_BACKEND_ENDPOINT="{{backend}}" pnpm --filter @sandbox-agent/foundry-frontend dev -- --host {{host}} --port {{port}}

[group('foundry')]
foundry-dev-mock host='127.0.0.1' port='4174':
	pnpm install
	FOUNDRY_FRONTEND_CLIENT_MODE=mock pnpm --filter @sandbox-agent/foundry-frontend dev -- --host {{host}} --port {{port}}

[group('foundry')]
foundry-mock:
	pnpm install
	mkdir -p foundry/.foundry/logs
	docker compose -f foundry/compose.mock.yaml up --build --force-recreate -d

[group('foundry')]
foundry-mock-down:
	docker compose -f foundry/compose.mock.yaml down

[group('foundry')]
foundry-mock-logs:
	docker compose -f foundry/compose.mock.yaml logs -f --tail=200

[group('foundry')]
foundry-dev-turbo:
	pnpm exec turbo run dev --parallel --filter=@sandbox-agent/foundry-*

[group('foundry')]
foundry-dev-down:
	docker compose --env-file .env -f foundry/compose.dev.yaml down

[group('foundry')]
foundry-dev-logs:
	docker compose --env-file .env -f foundry/compose.dev.yaml logs -f --tail=200

[group('foundry')]
foundry-preview-down:
	docker compose --env-file .env -f foundry/compose.preview.yaml down

[group('foundry')]
foundry-preview-logs:
	docker compose --env-file .env -f foundry/compose.preview.yaml logs -f --tail=200

[group('foundry')]
foundry-format:
	prettier --write foundry

[group('foundry')]
foundry-docker-build tag='foundry:local':
	docker build -f foundry/docker/backend.Dockerfile -t {{tag}} .

