.PHONY: all build clean run test docker-build docker-run install-dev help release

# 应用名称
APP_NAME := sms-receiver-v2
MAIN_FILE := main.go

# 版本信息
VERSION := v2.0.1
BUILD_TIME := $(shell date +%Y-%m-%d\ %H:%M:%S)
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
GO_VERSION := $(shell go version | awk '{print $$3}')
BUILD_ENV := prod

# Go 配置
GO := go
GOFMT := gofmt
GOVET := go vet
GOTEST := go test

# 构建配置
BUILD_DIR := ./build
LDFLAGS := -ldflags="-X 'main.Version=$(VERSION)' -X 'main.BuildTime=$(BUILD_TIME)' -X 'main.GitCommit=$(GIT_COMMIT)' -X 'main.GoVersion=$(GO_VERSION)' -X 'main.BuildEnv=$(BUILD_ENV)' -w -s"

# Docker 配置
DOCKER_IMAGE := sms-receiver-go
DOCKER_TAG := latest

# 默认目标
all: build

## build: 编译应用
build:
	@echo "编译应用..."
	$(GO) build $(LDFLAGS) -o $(APP_NAME) $(MAIN_FILE)
	@echo "编译完成: $(APP_NAME)"

## clean: 清理构建文件
clean:
	@echo "清理构建文件..."
	rm -f $(APP_NAME)
	rm -rf $(BUILD_DIR)
	@echo "清理完成"

## run: 运行应用
run:
	@echo "运行应用..."
	$(GO) run $(MAIN_FILE)

## test: 运行测试
test:
	@echo "运行测试..."
	$(GOTEST) -v ./...

## fmt: 格式化代码
fmt:
	@echo "格式化代码..."
	$(GOFMT) -s -w .

## vet: 代码质量检查
vet:
	@echo "代码质量检查..."
	$(GOVET) ./...

## install-dev: 安装开发依赖
install-dev:
	@echo "安装开发依赖..."
	$(GO) install -v github.com/golangci/golangci-lint/cmd/golangci-lint@latest

## lint: 代码检查
lint:
	@echo "代码检查..."
	golangci-lint run

## docker-build: 构建 Docker 镜像
docker-build:
	@echo "构建 Docker 镜像..."
	docker build -t $(DOCKER_IMAGE):$(DOCKER_TAG) .

## docker-run: 运行 Docker 容器
docker-run:
	@echo "运行 Docker 容器..."
	docker run -d \
		--name $(DOCKER_IMAGE) \
		-p 28001:28001 \
		-v $(PWD)/config.yaml:/app/config.yaml:ro \
		-v $(PWD)/data:/app/data \
		--restart unless-stopped \
		$(DOCKER_IMAGE):$(DOCKER_TAG)

## docker-stop: 停止 Docker 容器
docker-stop:
	@echo "停止 Docker 容器..."
	docker stop $(DOCKER_IMAGE) || true
	docker rm $(DOCKER_IMAGE) || true

## docker-logs: 查看 Docker 日志
docker-logs:
	docker logs -f $(DOCKER_IMAGE)

## password: 生成密码哈希
password:
	@echo "生成密码哈希..."
	@read -p "请输入密码: " password; \
	$(GO) run tools/password_hash.go $$password

## help: 显示帮助信息
help:
	@echo "可用命令:"
	@grep -E '^## ' $(MAKEFILE_LIST) | sed 's/## /  /'

## 初始化项目
init:
	@echo "初始化项目..."
	@if [ ! -f config.yaml ]; then \
		cp config.example.yaml config.yaml; \
		echo "已创建 config.yaml，请根据需要修改配置"; \
	else \
		echo "config.yaml 已存在"; \
	fi

## release: 发布版本（创建 tag、推送到远程、上传二进制文件）
release: clean build
	@echo "正在发布版本: $(VERSION)"
	@if [ -z "$(VERSION)" ]; then \
		echo "错误: 请指定 VERSION，例如: make release VERSION=v2.0.0"; \
		exit 1; \
	fi
	@git tag -a $(VERSION) -m "Release $(VERSION)" || echo "Tag 已存在，继续..."
	@git push origin $(VERSION) || echo "Tag 已推送"
	@echo "请在 Gitea 上手动上传二进制文件到 Release: $(VERSION)"
	@echo "或者使用: make release-upload VERSION=$(VERSION)"

## release-upload: 上传二进制文件到 Gitea Release
release-upload:
	@echo "上传二进制文件到 Gitea Release: $(VERSION)"
	@if [ ! -f $(APP_NAME) ]; then \
		echo "错误: 找不到编译文件 $(APP_NAME)，请先运行 make build"; \
		exit 1; \
	fi
	@if [ -z "$(GITEA_TOKEN)" ]; then \
		echo "错误: 请设置环境变量 GITEA_TOKEN"; \
		echo "用法: make release-upload VERSION=v2.0.0 GITEA_TOKEN=your_token"; \
		exit 1; \
	fi
	@echo "正在上传 $(APP_NAME) 到 Gitea..."
	@curl -X POST \
		-H "Authorization: token $(GITEA_TOKEN)" \
		-F "attachment=@$(APP_NAME)" \
		"https://gitea.king.nyc.mn/api/v1/repos/openclaw/SmsReceiver-go/releases/$(VERSION)/assets" \
		2>&1 | grep -E "(name|size|browser_download_url)" || echo "上传完成"
	@echo "✅ 上传完成！"

## release-full: 完整发布流程（编译、tag、推送、上传）
release-full: clean build
	@echo "完整发布流程: $(VERSION)"
	@if [ -z "$(VERSION)" ] || [ -z "$(GITEA_TOKEN)" ]; then \
		echo "用法: make release-full VERSION=v2.0.0 GITEA_TOKEN=your_token"; \
		exit 1; \
	fi
	@echo "1. 创建并推送 Git tag..."
	@git tag -a $(VERSION) -m "Release $(VERSION)"
	@git push origin $(VERSION)
	@echo "2. 上传二进制文件到 Gitea Release..."
	@curl -X POST \
		-H "Authorization: token $(GITEA_TOKEN)" \
		-F "attachment=@$(APP_NAME)" \
		"https://gitea.king.nyc.mn/api/v1/repos/openclaw/SmsReceiver-go/releases/$(VERSION)/assets"
	@echo "✅ 发布完成！访问: https://gitea.king.nyc.mn/openclaw/SmsReceiver-go/releases/tag/$(VERSION)"
