.PHONY: help build run smoke load stress nightly clean test

# 默認目標
.DEFAULT_GOAL := help

# 顏色定義
GREEN  := \033[0;32m
YELLOW := \033[0;33m
NC     := \033[0m # No Color

# 環境變數
BASE_URL ?= http://localhost:8888
TEST_LOGIN_ID ?= 
TEST_PASSWORD ?= 
K6_IMAGE ?= k6-test:latest

help: ## 顯示幫助信息
	@echo "$(GREEN)可用命令:$(NC)"
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "  $(YELLOW)%-20s$(NC) %s\n", $$1, $$2}'


smoke-auth: ## 運行認證功能冒煙測試
	@echo "$(GREEN)運行認證功能冒煙測試...$(NC)"
	@docker run --rm -i \
		--network host \
		-v $(PWD)/scenarios:/app/scenarios \
		-v $(PWD)/tests:/app/tests \
		-e BASE_URL=$(BASE_URL) \
		grafana/k6:latest run /app/tests/smoke/smoke-auth-test.js

load-auth: ## 運行認證功能負載測試
	@echo "$(GREEN)運行認證功能負載測試...$(NC)"
	@docker run --rm -i \
		--network host \
		-v $(PWD)/scenarios:/app/scenarios \
		-v $(PWD)/tests:/app/tests \
		-e BASE_URL=$(BASE_URL) \
		grafana/k6:latest run /app/tests/pre/load-auth-test.js


# build: ## 構建 Docker 映像
# 	@echo "$(GREEN)構建 k6 測試映像...$(NC)"
# 	docker-compose build

# # ==================== 冒煙測試 ====================
# smoke: smoke-all ## 運行所有冒煙測試

# smoke-all: ## 運行所有冒煙測試
# 	@echo "$(GREEN)運行所有冒煙測試...$(NC)"
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-e BASE_URL=$(BASE_URL) \
# 		grafana/k6:latest run /app/tests/smoke/smoke-health-test.js
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-e BASE_URL=$(BASE_URL) \
# 		grafana/k6:latest run /app/tests/smoke/smoke-auth-test.js
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-e BASE_URL=$(BASE_URL) \
# 		grafana/k6:latest run /app/tests/smoke/smoke-user-test.js
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-e BASE_URL=$(BASE_URL) \
# 		grafana/k6:latest run /app/tests/smoke/smoke-user-profile-flow-test.js

# smoke-health: ## 運行健康檢查冒煙測試
# 	@echo "$(GREEN)運行健康檢查冒煙測試...$(NC)"
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-e BASE_URL=$(BASE_URL) \
# 		grafana/k6:latest run /app/tests/smoke/smoke-health-test.js

# smoke-auth: ## 運行認證功能冒煙測試
# 	@echo "$(GREEN)運行認證功能冒煙測試...$(NC)"
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-e BASE_URL=$(BASE_URL) \
# 		grafana/k6:latest run /app/tests/smoke/smoke-auth-test.js

# smoke-user: ## 運行使用者功能冒煙測試
# 	@echo "$(GREEN)運行使用者功能冒煙測試...$(NC)"
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-e BASE_URL=$(BASE_URL) \
# 		grafana/k6:latest run /app/tests/smoke/smoke-user-test.js

# smoke-user-profile-flow: ## 運行使用者資料流程冒煙測試
# 	@echo "$(GREEN)運行使用者資料流程冒煙測試...$(NC)"
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-e BASE_URL=$(BASE_URL) \
# 		grafana/k6:latest run /app/scenarios/e2e/user-profile-flow.js

# # ==================== 負載測試 ====================
# load: load-all ## 運行所有負載測試

# load-all: ## 運行所有負載測試
# 	@echo "$(GREEN)運行所有負載測試...$(NC)"
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-e BASE_URL=$(BASE_URL) \
# 		grafana/k6:latest run /app/tests/pre/load-auth-test.js
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-e BASE_URL=$(BASE_URL) \
# 		grafana/k6:latest run /app/tests/pre/load-user-test.js
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-e BASE_URL=$(BASE_URL) \
# 		grafana/k6:latest run /app/tests/pre/load-user-profile-flow-test.js



# load-user: ## 運行使用者功能負載測試
# 	@echo "$(GREEN)運行使用者功能負載測試...$(NC)"
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-e BASE_URL=$(BASE_URL) \
# 		grafana/k6:latest run /app/tests/pre/load-user-test.js

# load-user-profile-flow: ## 運行使用者資料流程負載測試
# 	@echo "$(GREEN)運行使用者資料流程負載測試...$(NC)"
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-e BASE_URL=$(BASE_URL) \
# 		grafana/k6:latest run /app/tests/pre/load-user-profile-flow-test.js

# # ==================== 壓力測試 ====================
# stress: stress-all ## 運行所有壓力測試

# stress-all: ## 運行所有壓力測試
# 	@echo "$(GREEN)運行所有壓力測試...$(NC)"
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-e BASE_URL=$(BASE_URL) \
# 		grafana/k6:latest run /app/tests/pre/stress-auth-test.js
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-e BASE_URL=$(BASE_URL) \
# 		grafana/k6:latest run /app/tests/pre/stress-user-test.js

# stress-auth: ## 運行認證功能壓力測試
# 	@echo "$(GREEN)運行認證功能壓力測試...$(NC)"
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-e BASE_URL=$(BASE_URL) \
# 		grafana/k6:latest run /app/tests/pre/stress-auth-test.js

# stress-user: ## 運行使用者功能壓力測試
# 	@echo "$(GREEN)運行使用者功能壓力測試...$(NC)"
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-e BASE_URL=$(BASE_URL) \
# 		grafana/k6:latest run /app/tests/pre/stress-user-test.js

# # ==================== 生產環境測試 ====================
# nightly: nightly-all ## 運行所有夜間測試

# nightly-all: ## 運行所有夜間測試
# 	@echo "$(GREEN)運行所有夜間測試...$(NC)"
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-e BASE_URL=$(BASE_URL) \
# 		-e TEST_LOGIN_ID=$(TEST_LOGIN_ID) \
# 		-e TEST_PASSWORD=$(TEST_PASSWORD) \
# 		grafana/k6:latest run /app/tests/prod/nightly-health-test.js
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-e BASE_URL=$(BASE_URL) \
# 		-e TEST_LOGIN_ID=$(TEST_LOGIN_ID) \
# 		-e TEST_PASSWORD=$(TEST_PASSWORD) \
# 		grafana/k6:latest run /app/tests/prod/nightly-auth-test.js
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-e BASE_URL=$(BASE_URL) \
# 		-e TEST_LOGIN_ID=$(TEST_LOGIN_ID) \
# 		-e TEST_PASSWORD=$(TEST_PASSWORD) \
# 		grafana/k6:latest run /app/tests/prod/nightly-user-test.js

# nightly-health: ## 運行健康檢查夜間測試
# 	@echo "$(GREEN)運行健康檢查夜間測試...$(NC)"
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-e BASE_URL=$(BASE_URL) \
# 		grafana/k6:latest run /app/tests/prod/nightly-health-test.js

# nightly-auth: ## 運行認證功能夜間測試
# 	@echo "$(GREEN)運行認證功能夜間測試...$(NC)"
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-e BASE_URL=$(BASE_URL) \
# 		-e TEST_LOGIN_ID=$(TEST_LOGIN_ID) \
# 		-e TEST_PASSWORD=$(TEST_PASSWORD) \
# 		grafana/k6:latest run /app/tests/prod/nightly-auth-test.js

# nightly-user: ## 運行使用者功能夜間測試
# 	@echo "$(GREEN)運行使用者功能夜間測試...$(NC)"
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-e BASE_URL=$(BASE_URL) \
# 		-e TEST_LOGIN_ID=$(TEST_LOGIN_ID) \
# 		-e TEST_PASSWORD=$(TEST_PASSWORD) \
# 		grafana/k6:latest run /app/tests/prod/nightly-user-test.js

# # ==================== 通用命令 ====================
# run: ## 運行指定的測試文件 (使用: make run TEST=tests/smoke/smoke-auth-test.js)
# 	@if [ -z "$(TEST)" ]; then \
# 		echo "$(YELLOW)錯誤: 請指定測試文件$(NC)"; \
# 		echo "用法: make run TEST=tests/smoke/smoke-auth-test.js"; \
# 		exit 1; \
# 	fi
# 	@echo "$(GREEN)運行測試: $(TEST)$(NC)"
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-e BASE_URL=$(BASE_URL) \
# 		-e TEST_LOGIN_ID=$(TEST_LOGIN_ID) \
# 		-e TEST_PASSWORD=$(TEST_PASSWORD) \
# 		grafana/k6:latest run /app/$(TEST)

# run-local: ## 本地運行測試（不使用 Docker，需要本地安裝 k6）
# 	@if [ -z "$(TEST)" ]; then \
# 		echo "$(YELLOW)錯誤: 請指定測試文件$(NC)"; \
# 		echo "用法: make run-local TEST=tests/smoke/smoke-auth-test.js"; \
# 		exit 1; \
# 	fi
# 	@echo "$(GREEN)本地運行測試: $(TEST)$(NC)"
# 	@BASE_URL=$(BASE_URL) \
# 	TEST_LOGIN_ID=$(TEST_LOGIN_ID) \
# 	TEST_PASSWORD=$(TEST_PASSWORD) \
# 	k6 run $(TEST)

# clean: ## 清理 Docker 資源
# 	@echo "$(GREEN)清理 Docker 資源...$(NC)"
# 	@docker-compose down -v 2>/dev/null || true
# 	@docker rmi $(K6_IMAGE) 2>/dev/null || true
# 	@echo "$(GREEN)清理完成$(NC)"

# # ==================== 測試結果輸出 ====================
# run-with-output: ## 運行測試並輸出結果到文件 (使用: make run-with-output TEST=tests/smoke/smoke-auth-test.js OUTPUT=results.json)
# 	@if [ -z "$(TEST)" ]; then \
# 		echo "$(YELLOW)錯誤: 請指定測試文件$(NC)"; \
# 		echo "用法: make run-with-output TEST=tests/smoke/smoke-auth-test.js OUTPUT=results.json"; \
# 		exit 1; \
# 	fi
# 	@mkdir -p results
# 	@echo "$(GREEN)運行測試並輸出結果: $(TEST) -> results/$(OUTPUT)$(NC)"
# 	@docker run --rm -i \
# 		--network host \
# 		-v $(PWD)/scenarios:/app/scenarios \
# 		-v $(PWD)/tests:/app/tests \
# 		-v $(PWD)/results:/app/results \
# 		-e BASE_URL=$(BASE_URL) \
# 		-e TEST_LOGIN_ID=$(TEST_LOGIN_ID) \
# 		-e TEST_PASSWORD=$(TEST_PASSWORD) \
# 		grafana/k6:latest run --out json=/app/results/$(OUTPUT) /app/$(TEST)

# # ==================== 環境設置 ====================
# env-dev: ## 設置開發環境變數
# 	@echo "$(GREEN)設置開發環境...$(NC)"
# 	@export BASE_URL=https://dev-api.example.com
# 	@echo "BASE_URL=$(BASE_URL)"

# env-pre: ## 設置預發布環境變數
# 	@echo "$(GREEN)設置預發布環境...$(NC)"
# 	@export BASE_URL=https://pre-api.example.com
# 	@echo "BASE_URL=$(BASE_URL)"

# env-prod: ## 設置生產環境變數
# 	@echo "$(GREEN)設置生產環境...$(NC)"
# 	@export BASE_URL=https://api.example.com
# 	@echo "BASE_URL=$(BASE_URL)"
# 	@echo "$(YELLOW)注意: 生產環境測試需要設置 TEST_LOGIN_ID 和 TEST_PASSWORD$(NC)"

