.PHONY: help install dev test lint test-full ci ci-matrix type-check ci-check sync-version check-version docker clean

PYTHON ?= python3

help: ## Show this help
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'

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

install: ## Install project in development mode
	$(PYTHON) -m pip install -e ".[dev]"

test: ## Run all tests (quick)
	$(PYTHON) -m pytest tests/ -q

test-full: ## Run all tests with coverage
	$(PYTHON) -m pytest --cov=maysii --cov-report=term-missing tests/

lint: ## Run linters (ruff)
	$(PYTHON) -m ruff format --check src tests
	$(PYTHON) -m ruff check src tests

type-check: ## Run type checker
	$(PYTHON) -m mypy src/maysii

sync-version: ## Sync version from pyproject.toml to all packages
	npx tsx scripts/sync-version.ts

check-version: ## Check version consistency across all packages
	$(PYTHON) scripts/check-version-sync.py

# 与 GitHub Actions CI 完全一致（lint + typecheck + pytest 含 coverage），推送前建议执行
ci: ## Run same checks as CI (lint + typecheck + pytest with coverage)
	@bash scripts/ci-local.sh

# 在多个 Python 版本上跑 CI 校验（与 CI matrix 一致，需安装 python3.10 … 3.14）
ci-matrix: ## Run CI checks on all supported Python versions (3.10–3.14)
	@bash scripts/run-ci-matrix.sh

ci-check: ## Lint + type-check + secret scan (no pytest; use 'make ci' for full CI parity)
	$(PYTHON) -m ruff format --check src/ tests/
	$(PYTHON) -m ruff check src/ tests/
	$(PYTHON) -m mypy src/maysii
	@echo "Checking for potential secrets in code..."
	@SEC_FAIL=0; \
	if grep -rn 'sk-[a-zA-Z0-9]\{20,\}' src/ tests/ --include='*.py' 2>/dev/null; then \
		echo "::error::Potential API key found in source code"; SEC_FAIL=1; \
	fi; \
	if grep -rn 'PRIVATE KEY' src/ tests/ --include='*.py' 2>/dev/null; then \
		echo "::error::Private key found in source code"; SEC_FAIL=1; \
	fi; \
	if grep -rn 'AKIA[0-9A-Z]\{16\}' src/ tests/ --include='*.py' 2>/dev/null | grep -v 'test_' | grep -v '_test\.py'; then \
		echo "::error::Potential AWS access key found in source code"; SEC_FAIL=1; \
	fi; \
	if grep -rn 'gh[pous]_[a-zA-Z0-9]\{36,\}' src/ tests/ --include='*.py' 2>/dev/null | grep -v 'test_' | grep -v '_test\.py'; then \
		echo "::error::Potential GitHub token found in source code"; SEC_FAIL=1; \
	fi; \
	exit $$SEC_FAIL

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

docker: ## Build Docker image
	docker build -t openclaw:latest .

docker-up: ## Start gateway with docker-compose
	docker compose up -d maysii-gateway

docker-down: ## Stop docker-compose services
	docker compose down

# ─── Cleanup ─────────────────────────────────────────────────────

clean: ## Remove build artifacts
	rm -rf build/ dist/ *.egg-info
	find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true