# Makefile for ljwx-fastapi
# 快速操作命令集

.PHONY: help install dev test clean db-init db-migrate db-upgrade db-downgrade db-reset docker-build docker-up docker-down k8s-deploy

# Default target
.DEFAULT_GOAL := help

# Colors
BLUE := \033[0;34m
GREEN := \033[0;32m
YELLOW := \033[0;33m
RED := \033[0;31m
NC := \033[0m

##@ General

help: ## Display this help
	@echo "$(BLUE)Available commands:$(NC)"
	@awk 'BEGIN {FS = ":.*##"; printf "\n"} /^[a-zA-Z_-]+:.*?##/ { printf "  $(GREEN)%-20s$(NC) %s\n", $$1, $$2 } /^##@/ { printf "\n$(YELLOW)%s$(NC)\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

##@ Development

install: ## Install dependencies
	@echo "$(BLUE)Installing dependencies...$(NC)"
	pip install -r requirements.txt
	@echo "$(GREEN)✓ Dependencies installed$(NC)"

dev: ## Run development server
	@echo "$(BLUE)Starting development server...$(NC)"
	python run.py

test: ## Run tests
	@echo "$(BLUE)Running tests...$(NC)"
	pytest tests/ -v --cov=app
	@echo "$(GREEN)✓ Tests completed$(NC)"

clean: ## Clean cache files
	@echo "$(BLUE)Cleaning up...$(NC)"
	find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
	find . -type f -name "*.pyc" -delete 2>/dev/null || true
	rm -rf .pytest_cache htmlcov .coverage
	@echo "$(GREEN)✓ Cleaned$(NC)"

##@ Database

db-init: ## Initialize database
	@echo "$(BLUE)Initializing database...$(NC)"
	bash scripts/init_database.sh
	python scripts/init_data.py
	@echo "$(GREEN)✓ Database initialized$(NC)"

db-migrate: ## Create new migration
	@read -p "Enter description: " desc; \
	alembic revision --autogenerate -m "$$desc"

db-upgrade: ## Upgrade database
	alembic upgrade head

db-current: ## Show current version
	alembic current

db-history: ## Show history
	alembic history

##@ Docker

docker-build: ## Build images
	docker build -t ljwx-fastapi:latest .
	docker build -f Dockerfile.migration -t ljwx-fastapi-migration:latest .

docker-up: ## Start services
	docker-compose up -d

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

docker-logs: ## Show logs
	docker-compose logs -f

##@ Quick Start

quickstart: install db-init dev ## Quick start
