.PHONY: gen-api fmt test run web-dev web-build

API_FILE := generate/api/gateway.api
GOCTL_HOME := generate/goctl
GOCTL ?= $(shell go env GOPATH)/bin/goctl

ENV_FILE ?= ../deploy/.env.dev

ifneq ($(wildcard $(ENV_FILE)),)
# 把 deploy/.env.dev 的值 export 出來（給 make run / native binary 用；dev-all.sh 自行 source 故不走這條）
include $(ENV_FILE)
export HAIXUN_MONGO_URI HAIXUN_MONGO_DB HAIXUN_REDIS_ADDR HAIXUN_REDIS_PASSWORD
export HAIXUN_JWT_ACCESS_SECRET HAIXUN_JWT_REFRESH_SECRET HAIXUN_WORKER_SECRET
export HAIXUN_SECRETS_KEY HAIXUN_BACKEND_URL
export HAIXUN_WORKER_POLL_MS HAIXUN_8D_TARGET_SAMPLES PLAYWRIGHT_HEADLESS
export INIT_TENANT_ID INIT_ADMIN_EMAIL INIT_ADMIN_PASSWORD INIT_ADMIN_DISPLAY_NAME
endif

export HAIXUN_JOB_WORKER_TYPE ?= go-demo
export HAIXUN_JOB_WORKER_ID ?= local-go-demo

# 修改 generate/api/*.api 後執行，重新產生 routes / types / handler（logic 已存在則保留實作）
gen-api:
	$(GOCTL) api go -api $(API_FILE) -dir . -style go_zero -home $(GOCTL_HOME)

fmt:
	go fmt ./...

test:
	go test ./...

run:
	mkdir -p bin
	go build -o bin/gateway gateway.go
	./bin/gateway -f etc/gateway.yaml

run-dev:
	go run gateway.go -f etc/gateway.yaml

init:
	go run ./cmd/tool/main.go init -f etc/gateway.yaml

web-dev:
	cd web && npm install && npm run dev

web-build:
	cd web && npm install && npm run build
