# World OS AutoCode - Makefile
# Makefile for common development tasks

.PHONY: help install install-dev test test-cov lint format type-check check docs clean build publish docker-build docker-run

# Default target
.DEFAULT_GOAL := help

# Python interpreter
PYTHON := python3
PIP := pip

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

help: ## Show this help message
	@echo "$(BLUE)World OS AutoCode - Available Commands:$(NC)"
	@echo ""
	@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "  $(GREEN)%-20s$(NC) %s\n", $$1, $$2}' $(MAKEFILE_LIST)
	@echo ""
	@echo "$(YELLOW)For more information, see README.md$(NC)"

# Installation targets
install: ## Install the package
	@echo "$(BLUE)Installing World OS AutoCode...$(NC)"
	$(PIP) install -e .
	@echo "$(GREEN)✓ Installation complete$(NC)"

install-dev: ## Install with development dependencies
	@echo "$(BLUE)Installing with development dependencies...$(NC)"
	$(PIP) install -e ".[dev]"
	@echo "$(GREEN)✓ Development installation complete$(NC)"

install-docs: ## Install with documentation dependencies
	@echo "$(BLUE)Installing with documentation dependencies...$(NC)"
	$(PIP) install -e ".[docs]"
	@echo "$(GREEN)✓ Documentation installation complete$(NC)"

install-all: ## Install with all optional dependencies
	@echo "$(BLUE)Installing with all dependencies...$(NC)"
	$(PIP) install -e ".[dev,docs]"
	@echo "$(GREEN)✓ Full installation complete$(NC)"

# Testing targets
test: ## Run tests
	@echo "$(BLUE)Running tests...$(NC)"
	$(PYTHON) -m pytest tests/ -v
	@echo "$(GREEN)✓ Tests passed$(NC)"

test-cov: ## Run tests with coverage
	@echo "$(BLUE)Running tests with coverage...$(NC)"
	$(PYTHON) -m pytest tests/ --cov=worldos_autocode --cov-report=html --cov-report=term
	@echo "$(GREEN)✓ Coverage report generated in htmlcov/$(NC)"

test-unit: ## Run unit tests only
	@echo "$(BLUE)Running unit tests...$(NC)"
	$(PYTHON) -m pytest tests/unit/ -v

test-integration: ## Run integration tests only
	@echo "$(BLUE)Running integration tests...$(NC)"
	$(PYTHON) -m pytest tests/integration/ -v

test-fast: ## Run tests in parallel (fast)
	@echo "$(BLUE)Running tests in parallel...$(NC)"
	$(PYTHON) -m pytest tests/ -n auto

# Code quality targets
lint: ## Run linters
	@echo "$(BLUE)Running linters...$(NC)"
	@echo "$(YELLOW)Running flake8...$(NC)"
	flake8 src/worldos_autocode tests
	@echo "$(YELLOW)Running pylint...$(NC)"
	pylint src/worldos_autocode
	@echo "$(GREEN)✓ Linting passed$(NC)"

format: ## Format code
	@echo "$(BLUE)Formatting code...$(NC)"
	@echo "$(YELLOW)Running black...$(NC)"
	black src/worldos_autocode tests
	@echo "$(YELLOW)Running isort...$(NC)"
	isort src/worldos_autocode tests
	@echo "$(GREEN)✓ Formatting complete$(NC)"

type-check: ## Run type checker
	@echo "$(BLUE)Running type checker...$(NC)"
	mypy src/worldos_autocode
	@echo "$(GREEN)✓ Type checking passed$(NC)"

bandit: ## Run security check
	@echo "$(BLUE)Running security check...$(NC)"
	bandit -r src/worldos_autocode -f json -o bandit-report.json || true
	@echo "$(GREEN)✓ Security check complete (see bandit-report.json)$(NC)"

check: ## Run all checks (lint, type-check, test)
	@echo "$(BLUE)Running all checks...$(NC)"
	$(MAKE) lint
	$(MAKE) type-check
	$(MAKE) test
	@echo "$(GREEN)✓ All checks passed$(NC)"

# Documentation targets
docs: ## Build documentation
	@echo "$(BLUE)Building documentation...$(NC)"
	cd docs && mkdocs build
	@echo "$(GREEN)✓ Documentation built in docs/site/$(NC)"

docs-serve: ## Serve documentation locally
	@echo "$(BLUE)Starting documentation server...$(NC)"
	cd docs && mkdocs serve

docs-deploy: ## Deploy documentation
	@echo "$(BLUE)Deploying documentation...$(NC)"
	cd docs && mike deploy --push --update-aliases latest
	@echo "$(GREEN)✓ Documentation deployed$(NC)"

# Build and distribution targets
build: ## Build package
	@echo "$(BLUE)Building package...$(NC)"
	$(PYTHON) -m build
	@echo "$(GREEN)✓ Package built in dist/$(NC)"

check-dist: ## Check distribution
	@echo "$(BLUE)Checking distribution...$(NC)"
	twine check dist/*
	@echo "$(GREEN)✓ Distribution check passed$(NC)"

publish-test: ## Publish to TestPyPI
	@echo "$(BLUE)Publishing to TestPyPI...$(NC)"
	twine upload --repository testpypi dist/*
	@echo "$(GREEN)✓ Published to TestPyPI$(NC)"

publish: ## Publish to PyPI
	@echo "$(BLUE)Publishing to PyPI...$(NC)"
	twine upload dist/*
	@echo "$(GREEN)✓ Published to PyPI$(NC)"

# Docker targets
docker-build: ## Build Docker image
	@echo "$(BLUE)Building Docker image...$(NC)"
	docker build -t worldos-autocode:latest .
	@echo "$(GREEN)✓ Docker image built$(NC)"

docker-run: ## Run Docker container
	@echo "$(BLUE)Running Docker container...$(NC)"
	docker run -it --rm worldos-autocode:latest

docker-push: ## Push Docker image
	@echo "$(BLUE)Pushing Docker image...$(NC)"
	docker push worldos-autocode:latest
	@echo "$(GREEN)✓ Docker image pushed$(NC)"

# Cleanup targets
clean: ## Clean build artifacts
	@echo "$(BLUE)Cleaning build artifacts...$(NC)"
	rm -rf build/ dist/ *.egg-info/ .eggs/
	rm -rf .pytest_cache/ .coverage htmlcov/
	rm -rf .mypy_cache/ .ruff_cache/
	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
	@echo "$(GREEN)✓ Cleanup complete$(NC)"

clean-all: clean ## Clean all artifacts including virtual environment
	@echo "$(BLUE)Cleaning all artifacts...$(NC)"
	rm -rf venv/ .venv/
	@echo "$(GREEN)✓ Full cleanup complete$(NC)"

# Utility targets
update-deps: ## Update dependencies
	@echo "$(BLUE)Updating dependencies...$(NC)"
	pip install --upgrade pip
	pip-compile --upgrade pyproject.toml
	@echo "$(GREEN)✓ Dependencies updated$(NC)"

info: ## Show project info
	@echo "$(BLUE)World OS AutoCode - Project Info$(NC)"
	@echo "================================"
	@echo "Version: $$(grep -oP '(?<=version = \")[^\"]+' pyproject.toml)"
	@echo "Python:  $$(python3 --version)"
	@echo "Location: $$(pwd)"
	@echo ""
	@echo "$(GREEN)Available commands:$(NC)"
	@$(MAKE) help | tail -n +3

.DEFAULT:
	@echo "$(RED)Unknown target: $@$(NC)"
	@echo "Run 'make help' for available commands."
	@exit 1
