.PHONY: build webhook test test-db lint generate generate-check dev migrate seed export clean tidy ui desktop desktop-package install

BINARY := bin/oikos
GO ?= go

build:
	mkdir -p bin
	$(GO) build -o $(BINARY) -tags timetzdata ./cmd/oikos

webhook:
	$(GO) build -o webhook -tags timetzdata ./cmd/webhook

test:
	$(GO) test -race -cover ./...

# Integration tests against the compose Postgres (starts it if needed)
test-db:
	docker compose up -d postgres
	@sleep 3
	OIKOS_TEST_DATABASE_URL="postgres://oikos:$${OIKOS_DB_PASSWORD:-oikos_dev}@localhost:5432/oikos?sslmode=disable" \
		$(GO) test -race -count=1 ./internal/db/ ./internal/httpapi/ ./internal/mcp/

lint: vet golangci govulncheck

vet:
	$(GO) vet ./...

golangci:
	@command -v golangci-lint >/dev/null 2>&1 && golangci-lint run --config .golangci.yml || echo "golangci-lint not installed — see https://golangci-lint.run/usage/install/"

govulncheck:
	@command -v govulncheck >/dev/null 2>&1 && govulncheck ./... || echo "govulncheck not installed — run: go install golang.org/x/vuln/cmd/govulncheck@latest"

.PHONY: lint vet golangci govulncheck

generate:
	$(GO) run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@v2.4.1 \
		-config api/codegen.yaml api/openapi.yaml
	$(GO) run github.com/sqlc-dev/sqlc/cmd/sqlc@v1.29.0 generate

# CI drift guard: regenerate and fail if the committed output changed.
generate-check: generate
	@git diff --exit-code -- internal/httpapi/gen internal/db/sqlcgen \
		|| (echo "generated code is stale — run 'make generate' and commit" && exit 1)

migrate:
	$(GO) run ./cmd/oikos migrate

seed:
	$(GO) run ./cmd/oikos seed

export:
	$(GO) run ./cmd/oikos export

dev:
	docker compose --profile dev up -d

# Local sanity-check build of the SPA. Not embedded in the oikos binary
# (plans/2026-07-12-wails-desktop-app.md 0.1) — deploys as its own
# container (compose/web/Dockerfile) via `docker compose --profile full
# up -d web`, same push-to-main pipeline as everything else.
ui:
	cd web && npm run build

desktop: ui   ## Build the Wails desktop app for the current platform
	rm -rf cmd/desktop/frontend/dist
	mkdir -p cmd/desktop/frontend/dist
	cp -r web/dist/* cmd/desktop/frontend/dist/
	cd cmd/desktop && CGO_ENABLED=1 go build -ldflags "-X main.version=$$(cat ../VERSION)" -o build/bin/Oikos .

desktop-package: desktop   ## Build + package the desktop app (zip on macOS, tar.gz on Linux)
	@case $$(uname -s) in \
		Darwin) \
			APP="cmd/desktop/build/bin/Oikos.app"; \
			rm -rf "$$APP"; \
			mkdir -p "$$APP/Contents/MacOS"; \
			mkdir -p "$$APP/Contents/Resources"; \
			cp cmd/desktop/build/bin/Oikos "$$APP/Contents/MacOS/Oikos"; \
			cp cmd/desktop/icon.icns "$$APP/Contents/Resources/icon.icns"; \
			sed "s/\$$(VERSION)/$$(cat VERSION)/" cmd/desktop/Info.plist.template > "$$APP/Contents/Info.plist"; \
			cd cmd/desktop/build/bin && zip -r oikos-desktop-darwin-$$(uname -m).zip Oikos.app ;; \
		Linux) \
			cd cmd/desktop/build/bin && tar czf oikos-desktop-linux-$$(uname -m).tar.gz Oikos ;; \
	esac
	@echo "Package: cmd/desktop/build/bin/"

install: desktop-package   ## Install to /Applications
	rm -rf /Applications/Oikos.app
	cp -r cmd/desktop/build/bin/Oikos.app /Applications/
	@echo "Installed to /Applications/Oikos.app"

clean:
	rm -f $(BINARY)
	rm -rf bin
	rm -rf cmd/desktop/build
	rm -rf cmd/desktop/frontend/dist
	$(GO) clean -testcache

tidy:
	$(GO) mod tidy
