GO_CTL_NAME=goctl
GO_ZERO_STYLE=go_zero
GO ?= go
GOFMT ?= gofmt "-s"
GOFILES := $(shell find . -name "*.go")
LDFLAGS := -s -w
VERSION="v1.0.0"
DOCKER_REPO="refactor-service"

.PHONY: test
test: # 進行測試
	go test -v --cover ./...


.PHONY: gen-api
gen-api: # 產生 api
	goctl api go -api ./generate/api/gateway.api -dir . -style go_zero

.PHONY: gen-doc
gen-doc: # 生成 Swagger 文檔
#	go-doc openapi --api ./generate/api/gateway.api --filename gateway.json --host dev-api.truheart.com.tw --basepath /api/v1
	 go-doc -a generate/api/gateway.api -d ./ -f gateway -s openapi3.0

.PHONY: mock-gen
mock-gen: # 建立 mock 資料
	mockgen -source=./pkg/member/domain/repository/account.go -destination=./pkg/member/mock/repository/account.go -package=mock
	mockgen -source=./pkg/member/domain/repository/account_uid.go -destination=./pkg/member/mock/repository/account_uid.go -package=mock
	mockgen -source=./pkg/member/domain/repository/auto_id.go -destination=./pkg/member/mock/repository/auto_id.go -package=mock
	mockgen -source=./pkg/member/domain/repository/user.go -destination=./pkg/member/mock/repository/user.go -package=mock
	mockgen -source=./pkg/member/domain/repository/verify_code.go -destination=./pkg/member/mock/repository/verify_code.go -package=mock
	mockgen -source=./pkg/member/domain/usecase/generate_uid.go -destination=./pkg/member/mock/usecase/generate_uid.go -package=mock

	@echo "Generate mock files successfully"

.PHONY: fmt
fmt: # 格式優化
	$(GOFMT) -w $(GOFILES)
	goimports -w ./

.PHONY: build
build: # 編譯專案
	go build -ldflags "$(LDFLAGS)" -o bin/gateway cmd/gateway/main.go

.PHONY: run
run: # 運行專案
	go run cmd/gateway/main.go

.PHONY: clean
clean: # 清理編譯文件
	rm -rf bin/

.PHONY: docker-build
docker-build: # 構建 Docker 映像
	docker build -t $(DOCKER_REPO):$(VERSION) .

.PHONY: docker-run
docker-run: # 運行 Docker 容器
	docker run -p 8888:8888 $(DOCKER_REPO):$(VERSION)

.PHONY: install
install: # 安裝依賴
	go mod tidy
	go mod download

.PHONY: help
help: # 顯示幫助信息
	@echo "Available commands:"
	@echo "  test             - 運行測試"
	@echo "  gen-api          - 產生 api"
	@echo "  gen-doc          - 生成 Swagger 文檔"
	@echo "  mock-gen         - 建立 mock 資料"
	@echo "  fmt              - 格式化代碼"
	@echo "  build            - 編譯專案"
	@echo "  run              - 運行專案"
	@echo "  clean            - 清理編譯文件"
	@echo "  docker-build     - 構建 Docker 映像"
	@echo "  docker-run       - 運行 Docker 容器"
	@echo "  install          - 安裝依賴"
	@echo "  help             - 顯示幫助信息"

