# Makefile for cleaning Python cache files and empty directories
# Author: AI Assistant
# Usage: make clean or make clean-all

.PHONY: clean clean-all clean-pyc clean-pytest clean-mypy clean-empty-dirs help

# Colors for pretty output
GREEN  := \033[0;32m
YELLOW := \033[0;33m
RED    := \033[0;31m
NC     := \033[0m # No Color

# Default target
help:
	@echo "$(GREEN)Available targets:$(NC)"
	@echo "  $(YELLOW)make clean$(NC)        - Remove Python cache files, pytest cache, mypy cache, and empty directories"
	@echo "  $(YELLOW)make clean-pyc$(NC)    - Remove Python cache files (__pycache__, .pyc, .pyo)"
	@echo "  $(YELLOW)make clean-pytest$(NC) - Remove pytest cache (.pytest_cache)"
	@echo "  $(YELLOW)make clean-mypy$(NC)   - Remove mypy cache (.mypy_cache)"
	@echo "  $(YELLOW)make clean-empty-dirs$(NC) - Remove empty directories"
	@echo "  $(YELLOW)make clean-all$(NC)    - Remove everything (cache, logs, coverage, build, dist, etc.)"

# Main clean target
clean: clean-pyc clean-pytest clean-mypy clean-empty-dirs
	@echo "$(GREEN)✓ Cleanup completed successfully!$(NC)"

# Clean Python cache files
clean-pyc:
	@echo "$(YELLOW)Removing Python cache files...$(NC)"
	@find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
	@find . -type f -name "*.pyc" -delete 2>/dev/null || true
	@find . -type f -name "*.pyo" -delete 2>/dev/null || true
	@find . -type f -name "*.pyd" -delete 2>/dev/null || true
	@echo "$(GREEN)✓ Python cache files removed$(NC)"

# Clean pytest cache
clean-pytest:
	@echo "$(YELLOW)Removing pytest cache...$(NC)"
	@rm -rf .pytest_cache 2>/dev/null || true
	@find . -type d -name ".pytest_cache" -exec rm -rf {} + 2>/dev/null || true
	@echo "$(GREEN)✓ Pytest cache removed$(NC)"

# Clean mypy cache
clean-mypy:
	@echo "$(YELLOW)Removing mypy cache...$(NC)"
	@rm -rf .mypy_cache 2>/dev/null || true
	@find . -type d -name ".mypy_cache" -exec rm -rf {} + 2>/dev/null || true
	@echo "$(GREEN)✓ Mypy cache removed$(NC)"

# Remove empty directories
clean-empty-dirs:
	@echo "$(YELLOW)Removing empty directories...$(NC)"
	@# Remove empty directories (including hidden ones), excluding .git and .venv
	@find . -type d -empty -not -path "./.git/*" -not -path "./.venv/*" -not -path "./venv/*" -not -path "./env/*" -not -path "./.env/*" -delete 2>/dev/null || true
	@# Second pass to remove newly created empty parent directories
	@find . -type d -empty -not -path "./.git/*" -not -path "./.venv/*" -not -path "./venv/*" -not -path "./env/*" -not -path "./.env/*" -delete 2>/dev/null || true
	@echo "$(GREEN)✓ Empty directories removed$(NC)"

# Clean everything (extended cleanup)
clean-all: clean
	@echo "$(YELLOW)Removing additional files...$(NC)"
	@# Coverage reports
	@rm -rf htmlcov .coverage coverage.xml .coverage.* 2>/dev/null || true
	@# Build artifacts
	@rm -rf build/ dist/ *.egg-info .eggs/ 2>/dev/null || true
	@# Log files
	@find . -type f -name "*.log" -delete 2>/dev/null || true
	@# Jupyter notebooks checkpoint
	@find . -type d -name ".ipynb_checkpoints" -exec rm -rf {} + 2>/dev/null || true
	@# IDE files (optional, uncomment if needed)
	# @rm -rf .vscode/ .idea/ 2>/dev/null || true
	@echo "$(GREEN)✓ Extended cleanup completed!$(NC)"

# Dry run - show what would be deleted (for safety)
clean-dry-run:
	@echo "$(YELLOW)Dry run - files that would be deleted:$(NC)"
	@echo "\nPython cache files:"
	@find . -type d -name "__pycache__" 2>/dev/null || echo "None"
	@find . -type f -name "*.pyc" 2>/dev/null || echo "None"
	@find . -type f -name "*.pyo" 2>/dev/null || echo "None"
	@echo "\nPytest cache:"
	@find . -type d -name ".pytest_cache" 2>/dev/null || echo "None"
	@echo "\nMypy cache:"
	@find . -type d -name ".mypy_cache" 2>/dev/null || echo "None"
	@echo "\nEmpty directories:"
	@find . -type d -empty -not -path "./.git/*" -not -path "./.venv/*" 2>/dev/null || echo "None"
	@echo "$(YELLOW)\nRun 'make clean' to actually delete these files$(NC)"