.PHONY: run build test lint clean docs migrate-up migrate-down migrate-version migrate-create

# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
BINARY_NAME=bin/junhong-cmp
MAIN_PATH=./cmd/api
WORKER_PATH=./cmd/worker
WORKER_BINARY=bin/junhong-worker

# Database migration parameters
MIGRATE=migrate
MIGRATIONS_PATH=migrations
DB_URL=postgresql://erp_pgsql:erp_2025@cxd.whcxd.cn:16159/junhong_cmp_test?sslmode=disable

all: test build

build:
	$(GOBUILD) -o $(BINARY_NAME) -v $(MAIN_PATH)
	$(GOBUILD) -o $(WORKER_BINARY) -v $(WORKER_PATH)

test:
	$(GOTEST) -v ./...

clean:
	$(GOCLEAN)
	rm -f $(BINARY_NAME)
	rm -f $(WORKER_BINARY)

run:
	$(GOBUILD) -o $(BINARY_NAME) -v $(MAIN_PATH)
	./$(BINARY_NAME)

run-worker:
	$(GOBUILD) -o $(WORKER_BINARY) -v $(WORKER_PATH)
	./$(WORKER_BINARY)

# Generate OpenAPI documentation
docs:
	$(GOCMD) run cmd/gendocs/main.go

# Database migration commands
migrate-up:
	$(MIGRATE) -path $(MIGRATIONS_PATH) -database "$(DB_URL)" up

migrate-down:
	$(MIGRATE) -path $(MIGRATIONS_PATH) -database "$(DB_URL)" down

migrate-version:
	$(MIGRATE) -path $(MIGRATIONS_PATH) -database "$(DB_URL)" version

migrate-create:
	@read -p "Enter migration name: " name; \
	$(MIGRATE) create -ext sql -dir $(MIGRATIONS_PATH) -seq $$name
