SQLITE3_VER := $(shell grep 'mattn/go-sqlite3' go.mod | awk '{print $$2}')
SQLITE3_INC := $(shell go env GOMODCACHE)/github.com/mattn/go-sqlite3@$(SQLITE3_VER)
export CGO_CFLAGS := -I$(CURDIR)/internal/db/include -I$(SQLITE3_INC)

# Add `go install` bin dir to PATH so tools installed via `make tools-install`
# (golangci-lint, go-crap) are found by later make targets in CI environments
# where the default PATH does not include $(go env GOPATH)/bin.
export PATH := $(shell go env GOPATH)/bin:$(PATH)

.PHONY: tools-install
tools-install:
	go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@latest
	go install github.com/padiazg/go-crap@latest

.PHONY: lint
lint:
	go vet ./...
	go fix ./...
	test -z "$(shell gofmt -l ./...)"
	golangci-lint run ./... --fix

.PHONY: web-install
web-install:
	cd web && bun install

# Run the Go API (backend) and the SvelteKit dev server (frontend) together.
# The frontend proxies /api requests to the Go server (GLEAN_API_URL).
.PHONY: dev
dev:
	@if [ -f .env ]; then set -a; . ./.env; set +a; fi; \
	GLEAN_FRONTEND_URL="$${GLEAN_FRONTEND_URL:-http://localhost:3000}"; \
	echo "Starting Go API on :8080 and SvelteKit on :3000 (Ctrl-C stops both)..."; \
	GLEAN_API_URL=http://localhost:8080 GLEAN_FRONTEND_URL=$$GLEAN_FRONTEND_URL go run -tags fts5 . & \
	GO_PID=$$!; \
	trap 'kill $$GO_PID 2>/dev/null' INT TERM EXIT; \
	(cd web && GLEAN_API_URL=http://localhost:8080 bun run dev); \
	kill $$GO_PID 2>/dev/null || true

.PHONY: dev-api
dev-api:
	@if [ -f .env ]; then set -a; . ./.env; set +a; fi; \
	GLEAN_FRONTEND_URL="$${GLEAN_FRONTEND_URL:-http://localhost:3000}" go run -tags fts5 .

.PHONY: dev-web
dev-web:
	cd web && GLEAN_API_URL=http://localhost:8080 bun run dev

.PHONY: web-build
web-build:
	cd web && bun run build

.PHONY: build
build: web-build
	go build -tags fts5 -o glean .

.PHONY: icons
icons:
	magick web/static/favicon.svg -background none -density 1200 -resize 512x512 -depth 8 PNG32:web/static/favicon.png
	magick web/static/favicon.svg -background none -density 1200 -resize 180x180 -depth 8 PNG32:web/static/apple-touch-icon.png

.PHONY: lex-lint
lex-lint:
	goat lex lint

.PHONY: lex-parse
lex-parse:
	goat lex parse $(shell find lexicons -name '*.json' 2>/dev/null)

.PHONY: test
test:
	go test -tags fts5 ./...

# CRAP score analysis (cyclomatic complexity x coverage).
# The fts5 build tag is required for the test build, so it's exported via GOFLAGS
# for the underlying `go test -cover` run go-crap performs.
.PHONY: crap
crap:
	GOFLAGS=-tags=fts5 go-crap scan --fail-above --threshold 450

.PHONY: check
check:
	cd web && bun run check

.PHONY: clean
clean:
	rm -f glean glean.db $(IMAGE_TAR)
	rm -rf web/build web/.svelte-kit

# Container image: built by Nix (flake.nix `image` output, dockerTools
# streamLayeredImage). scripts/build-image.sh builds locally when `nix` is
# present and otherwise on the remote builder in $GLEAN_NIX_VM.
IMAGE ?= atcr.io/julien.rbrt.fr/glean:latest
IMAGE_TAR ?= glean-image.tar

.PHONY: image
image:
	scripts/build-image.sh $(IMAGE_TAR)

.PHONY: image-load
image-load: image
	docker load < $(IMAGE_TAR)
	docker tag glean:latest $(IMAGE)

.PHONY: image-push
image-push: image-load
	docker push $(IMAGE)
