# One API Makefile
# 快速构建和部署

VERSION := $(shell cat VERSION 2>/dev/null || echo "v0.0.0")
ifeq ($(VERSION),)
	VERSION := "v0.0.0"
endif

.PHONY: all build frontend backend docker docker-full clean

all: build

# 完整构建（前端 + 后端）
build:
	@echo "=== Building One API $(VERSION) ==="
	@echo ""
	@echo "--- Building frontend ---"
	cd web/default && npm install --legacy-peer-deps 2>/dev/null || npm install
	cd web/default && DISABLE_ESLINT_PLUGIN='true' REACT_APP_VERSION="$(VERSION)" npm run build
	@echo ""
	@echo "--- Copying frontend build ---"
	mkdir -p web/build
	cp -r web/default/build/* web/build/ 2>/dev/null || true
	@echo ""
	@echo "--- Building Go backend ---"
	go mod download
	go build -trimpath -ldflags "-s -w -X 'github.com/songquanpeng/one-api/common.Version=$(VERSION)'" -o one-api
	@echo ""
	@echo "=== Build complete! ==="
	@echo "Binary: ./one-api"
	@echo "Frontend: web/build/"

# 仅构建前端
frontend:
	cd web/default && npm install --legacy-peer-deps 2>/dev/null || npm install
	cd web/default && DISABLE_ESLINT_PLUGIN='true' REACT_APP_VERSION="$(VERSION)" npm run build
	mkdir -p web/build
	cp -r web/default/build/* web/build/ 2>/dev/null || true

# 仅构建后端
backend:
	go mod download
	go build -trimpath -ldflags "-s -w -X 'github.com/songquanpeng/one-api/common.Version=$(VERSION)'" -o one-api

# 快速 Docker 构建（使用预编译文件）
docker:
	docker build -t redline-api .

# 完整 Docker 构建（从源码编译）
docker-full:
	docker build -f Dockerfile.full -t redline-api .

# 运行
run:
	./one-api --port 3000 --log-dir ./logs

# 清理构建产物
clean:
	rm -f one-api
	rm -rf web/build
	rm -rf web/default/build
	rm -rf web/air/build
	rm -rf web/berry/build