# CodeCat 项目 Makefile
.PHONY: help install test test-unit test-integration test-cov clean lint check-i18n format docker-build docker-cli http-up http-up-d http-submit http-status http-result http-export http-list http-logs http-down ttyd-up telegram-up

help:  ## 显示帮助信息
	@echo "CodeCat 项目命令:"
	@echo ""
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'

install:  ## 安装项目和依赖
	pip install -e .

install-dev:  ## 安装开发依赖
	pip install -e .[dev]

install-test:  ## 安装测试依赖
	pip install -e .[test]

test:  ## 运行所有测试
	pytest

test-unit:  ## 仅运行单元测试
	pytest -m unit

test-integration:  ## 仅运行集成测试
	pytest -m integration

test-cov:  ## 运行测试并生成覆盖率报告
	pytest --cov=codecat --cov-report=html --cov-report=term

test-fast:  ## 运行快速测试（排除慢速测试）
	pytest -m "not slow"

test-parallel:  ## 并行运行测试
	pytest -n auto

test-verbose:  ## 详细模式运行测试
	pytest -vvs

test-watch:  ## 监视模式运行测试（需要 pytest-watch）
	@command -v ptw >/dev/null 2>&1 || { echo "请先安装 pytest-watch: pip install pytest-watch"; exit 1; }
	ptw -- -x

lint:  ## 运行代码检查
	ruff check codecat tests

check-i18n:  ## 检查 i18n key 完整性（源码 vs locales.csv）
	python3 dev/check_i18n_keys.py

format:  ## 格式化代码
	ruff format codecat tests

clean:  ## 清理临时文件
	find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true
	find . -type f -name "*.pyc" -delete
	find . -type f -name "*.pyo" -delete
	find . -type f -name "*.coverage" -delete
	find . -type d -name "*.egg-info" -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name ".pytest_cache" -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name ".ruff_cache" -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name "htmlcov" -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name ".coverage" -exec rm -rf {} + 2>/dev/null || true

build:  ## 构建项目
	python -m build

docs:  ## 生成文档（如果有 sphinx）
	@echo "文档生成功能待实现"

check-all: lint test  ## 运行所有检查（lint + test）

pre-commit: format lint test-fast  ## 提交前检查

ci: clean install-test lint test-cov  ## CI 流程

docker-build:  ## 构建 CodeCat Docker 镜像
	./docker/codecat.sh build

docker-cli:  ## 启动容器内 CodeCat CLI
	./docker/codecat.sh cli

http-up:  ## 启动 HTTP 服务
	./docker/codecat.sh http

http-up-d:  ## 后台启动 HTTP 服务
	./docker/codecat.sh http -d

http-submit:  ## 提交 HTTP 任务，使用 Q="..."
	@test -n "$(Q)" || { echo 'Usage: make http-submit Q="你是谁?"'; exit 2; }
	./docker/codecat.sh submit "$(Q)"

http-status:  ## 查询 HTTP 任务状态，使用 ID=...
	@test -n "$(ID)" || { echo 'Usage: make http-status ID=<task_id>'; exit 2; }
	./docker/codecat.sh status "$(ID)"

http-result:  ## 查询 HTTP 任务结果，使用 ID=...
	@test -n "$(ID)" || { echo 'Usage: make http-result ID=<task_id>'; exit 2; }
	./docker/codecat.sh result "$(ID)"

http-export:  ## 导出 HTTP 任务数据，使用 ID=...
	@test -n "$(ID)" || { echo 'Usage: make http-export ID=<task_id>'; exit 2; }
	./docker/codecat.sh export "$(ID)"

http-list:  ## 列出 HTTP 任务
	./docker/codecat.sh list

http-logs:  ## 查看 HTTP 服务日志
	./docker/codecat.sh logs http

http-down:  ## 停止 Docker compose 服务
	./docker/codecat.sh down

ttyd-up:  ## 启动 ttyd Web 终端
	./docker/codecat.sh ttyd

telegram-up:  ## 启动 Telegram bot
	./docker/codecat.sh telegram