# Nucleic build channels.
#
# Each target sets NUCLEIC_CHANNEL, which Package.swift reads to pick the app
# executable's product (== process) name and the compile-time channel define that
# drives BuildChannel / the warning banner:
#
#   channel  process name     banner                  built from   how
#   -------  ---------------  ----------------------  -----------  ----------------
#   dev      nucleic-local    red "Local Build"       dev          make dev / run
#   beta     nucleic-beta     blue "Beta"             staging      make beta
#   rc       nucleic-rc       gold "Release Candidate"  rc         make rc
#   stable   nucleic          none                    main         make stable
#
# `--build-system native` is required in this repo (iCloud + SwiftBuild codesign
# workaround). dev is a debug build; beta/rc/stable are release-optimized.

BUILDSYS := --build-system native

.PHONY: dev run beta rc stable test clean app-dev app-beta app-rc app-stable apps \
        dmg-beta dmg-rc dmg-stable release-beta release-rc release-stable ios-release \
        edge-deps edge-build edge-test edge-deploy

## dev: build the dev channel (process: nucleic-local)
dev:
	NUCLEIC_CHANNEL=dev swift build $(BUILDSYS) --product nucleic-local

## run: build + launch the dev channel
run:
	NUCLEIC_CHANNEL=dev swift run $(BUILDSYS) nucleic-local

## beta: build the beta channel, release-optimized (process: nucleic-beta)
beta:
	NUCLEIC_CHANNEL=beta swift build -c release $(BUILDSYS) --product nucleic-beta

## rc: build the release-candidate channel, release-optimized (process: nucleic-rc)
rc:
	NUCLEIC_CHANNEL=rc swift build -c release $(BUILDSYS) --product nucleic-rc

## stable: build the stable/prod channel, release-optimized (process: nucleic)
stable:
	NUCLEIC_CHANNEL=stable swift build -c release $(BUILDSYS) --product nucleic

## app-dev: package the dev channel as dist/Nucleic Dev.app
app-dev:
	./scripts/package-app.sh dev

## app-beta: package the beta channel as dist/Nucleic Beta.app
app-beta:
	./scripts/package-app.sh beta

## app-rc: package the release-candidate channel as dist/Nucleic RC.app
app-rc:
	./scripts/package-app.sh rc

## app-stable: package the stable channel as dist/Nucleic.app
app-stable:
	./scripts/package-app.sh stable

## apps: package all channels
apps: app-dev app-beta app-rc app-stable

## dmg-beta: build a signed DMG from dist/Nucleic Beta.app (run app-beta first)
dmg-beta:
	./scripts/make-dmg.sh beta

## dmg-rc: build a signed DMG from dist/Nucleic RC.app (run app-rc first)
dmg-rc:
	./scripts/make-dmg.sh rc

## dmg-stable: build a signed DMG from dist/Nucleic.app (run app-stable first)
dmg-stable:
	./scripts/make-dmg.sh stable

# Releases bump ./VERSION: build number always +1, plus a semver bump via BUMP (default minor).
#   make release-beta                 # 0.1.0 (827) -> 0.2.0 (828): minor (the default)
#   make release-beta BUMP=major      # -> 1.0.0 (828)
#   make release-beta BUMP=patch      # -> 0.1.1 (828)
#   make release-beta BUMP=build      # -> 0.1.0 (828): build number only

## release-beta: signed + notarized + stapled DMG release (beta channel). BUMP=major|minor|patch
release-beta:
	NUCLEIC_BUMP=$(BUMP) ./scripts/release-macos.sh beta

## release-rc: signed + notarized + stapled DMG release (rc channel). BUMP=major|minor|patch
release-rc:
	NUCLEIC_BUMP=$(BUMP) ./scripts/release-macos.sh rc

## release-stable: signed + notarized + stapled DMG release (stable channel). BUMP=major|minor|patch
release-stable:
	NUCLEIC_BUMP=$(BUMP) ./scripts/release-macos.sh stable

## ios-release: archive + upload the iPhone app to App Store Connect (TestFlight)
ios-release:
	./scripts/ios-release.sh

## test: run the test suite
test:
	swift test $(BUILDSYS)

## clean: remove build products
clean:
	swift package clean

# --- nucleic-edge cloud service (Cloudflare Worker; cloud/nucleic-edge) -------
# Build = typecheck + bundle via wrangler's dry-run (no upload, no auth).
# Deploy = `wrangler deploy` (needs Cloudflare auth; see cloud/nucleic-edge/README.md).
# The Worker's npm deps install on demand whenever package*.json changes.

EDGE_DIR := cloud/nucleic-edge

$(EDGE_DIR)/node_modules: $(EDGE_DIR)/package.json $(EDGE_DIR)/package-lock.json
	cd $(EDGE_DIR) && npm install
	@touch $@

## edge-deps: install the nucleic-edge Worker's npm dependencies
edge-deps: $(EDGE_DIR)/node_modules

## edge-build: typecheck + bundle the Worker without uploading (wrangler dry-run)
edge-build: $(EDGE_DIR)/node_modules
	cd $(EDGE_DIR) && npm run typecheck && npm run check

## edge-test: run the nucleic-edge Worker unit tests
edge-test: $(EDGE_DIR)/node_modules
	cd $(EDGE_DIR) && npm test

## edge-deploy: build, then deploy the Worker to Cloudflare (wrangler deploy)
edge-deploy: edge-build
	cd $(EDGE_DIR) && npm run deploy
