GIT_COMMIT := $(shell git rev-parse HEAD 2>/dev/null || echo "unknown")
GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
BUILD_TIME := $(shell date -u '+%Y-%m-%d %H:%M:%S UTC')
VERSION    := 1.0.0

LDFLAGS := -w -s \
    -X 'github.com/tidbcloud/inference-service/internal/version.GitCommit=$(GIT_COMMIT)' \
    -X 'github.com/tidbcloud/inference-service/internal/version.GitBranch=$(GIT_BRANCH)' \
    -X 'github.com/tidbcloud/inference-service/internal/version.BuildTime=$(BUILD_TIME)' \
    -X 'github.com/tidbcloud/inference-service/internal/version.Version=$(VERSION)'

default: build

.PHONY: generate
generate:
	go tool swag init -g cmd/server/app/server.go

# Build the binary
.PHONY: build
build: generate
	@mkdir -p bin
	$(eval GIT_COMMIT := $(shell git rev-parse HEAD 2>/dev/null || echo "unknown"))
	$(eval GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown"))
	$(eval BUILD_TIME := $(shell date -u '+%Y-%m-%d %H:%M:%S UTC'))
	CGO_ENABLED=0 go build -ldflags="$(LDFLAGS)" -o bin/inference-service cmd/server/main.go

# Clean build artifacts
.PHONY: clean
clean:
	rm -rf bin/

# Run tests
.PHONY: test
test:
	go test -race ./...

# Format code
.PHONY: fmt
fmt:
	go fmt ./...

# Run the application locally
.PHONY: run
run:
	go run cmd/server/main.go serve

# Generate Grafana dashboard
.PHONY: dashboard
dashboard:
	cd tools && go run generate_grafana_dashboard.go
