.PHONY: help run test lint format docker-run docker-stop

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}'

SRC_DIRS = web models services settings
SRC_FILES = $(SRC_DIRS) manage.py

# Development
run: ## Start the server
	uv run python manage.py start-web

# Testing
test: ## Run all tests
	uv run pytest

test-cov: ## Run tests with coverage
	uv run pytest --cov=. --cov-report=html --cov-report=term-missing

# Code quality
lint: ## Run linter
	uv run ruff check $(SRC_FILES)

lint-fix: ## Fix linting issues
	uv run ruff check $(SRC_FILES) --fix

format: ## Format code
	uv run ruff format $(SRC_FILES)

type-check: ## Run type checking
	uv run mypy $(SRC_DIRS)

check: lint format type-check ## Run all checks

# Database
db-migrate: ## Create new migration
	uv run alembic revision --autogenerate -m "$(message)"

db-upgrade: ## Apply migrations
	uv run alembic upgrade head

db-downgrade: ## Rollback last migration
	uv run alembic downgrade -1

db-history: ## Show migration history
	uv run alembic history

# Docker
docker-run: ## Run with Docker Compose
	docker-compose up -d --force-recreate

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

docker-rebuild: ## Rebuild and run
	docker-compose up --build -d --force-recreate

docker-infra: ## Start only postgres + minio
	docker-compose up -d postgres minio minio_init

# Utilities
clean: ## Clean up temporary files
	find . -type f -name "*.pyc" -delete
	find . -type d -name "__pycache__" -delete
	rm -rf .pytest_cache .mypy_cache htmlcov .coverage dist build

init: ## Full project init
	uv sync
	make docker-infra
	@sleep 5
	make db-upgrade
	@echo "Done! Run: make run"
