.PHONY: dev build migrate test lint docker-up docker-down clean

BINARY := bin/forgebucket
GO_CMD := ./cmd/forgebucket
FRONTEND_DIR := frontend

# ─── Development ─────────────────────────────────────────────────────────────

dev:
	@echo "Starting backend (:8080) + Vite (:5173)..."
	@trap 'kill %1 %2 2>/dev/null; exit' INT; \
	  go run $(GO_CMD) & \
	  pnpm -C $(FRONTEND_DIR) dev & \
	  wait

# ─── Production build ─────────────────────────────────────────────────────────

build: build-frontend build-go

build-frontend:
	@echo "Building frontend..."
	pnpm -C $(FRONTEND_DIR) build
	@rm -rf web/dist
	@cp -r $(FRONTEND_DIR)/dist web/dist

build-go:
	@echo "Building Go binary..."
	@mkdir -p bin
	go build -ldflags="-s -w" -o $(BINARY) $(GO_CMD)

# ─── Database ─────────────────────────────────────────────────────────────────

migrate:
	@echo "Running database migrations..."
	go run ./cmd/migrate

# ─── Docker ───────────────────────────────────────────────────────────────────

docker-up:
	docker compose up -d

docker-down:
	docker compose down

# ─── Quality ──────────────────────────────────────────────────────────────────

test:
	go test ./... -race -count=1
	pnpm -C $(FRONTEND_DIR) test --run

lint:
	go vet ./...
	pnpm -C $(FRONTEND_DIR) lint

# ─── Housekeeping ─────────────────────────────────────────────────────────────

clean:
	rm -rf bin web/dist $(FRONTEND_DIR)/dist
