# edgecompiler - Makefile
# Top-level build orchestration for macOS ARM64

.PHONY: install test test-auto test-hardware test-coral test-metal test-sloth lint format clean docs help download-models benchmark-report

# ── Configuration ──────────────────────────────────────────────────────────────

PYTHON      ?= python3
PIP         ?= pip3
PYTEST      ?= pytest
RUFF        ?= ruff
MYPY        ?= mypy
BUILDDIR    := build
DISTDIR     := dist
EGGDIR      := *.egg-info
SRCDIR      := src
TESTDIR     := tests
DOCDIR      := docs
SLOTHDIR    := sloth-integration

# ── Default target ────────────────────────────────────────────────────────────

help: ## Show this help message
	@echo "edgecompiler - available targets:"
	@echo ""
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
		awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-18s\033[0m %s\n", $$1, $$2}'
	@echo ""

# ── Installation ──────────────────────────────────────────────────────────────

install: ## Install in development mode with all dev dependencies
	$(PIP) install -e ".[dev]"

install-all: ## Install with all optional dependencies + dev
	$(PIP) install -e ".[all,dev]"

install-pytorch: ## Install with PyTorch frontend support
	$(PIP) install -e ".[pytorch,dev]"

install-tensorflow: ## Install with TensorFlow frontend support
	$(PIP) install -e ".[tensorflow,dev]"

install-onnx: ## Install with ONNX frontend support
	$(PIP) install -e ".[onnx,dev]"

install-coral: ## Install with Coral backend support
	$(PIP) install -e ".[coral,dev]"

install-coreml: ## Install with Core ML / Metal backend support
	$(PIP) install -e ".[coreml,dev]"

# ── Testing ────────────────────────────────────────────────────────────────────

test: ## Run unit tests (no hardware required)
	$(PYTEST) $(TESTDIR) -v -m "not hardware and not slow"

test-auto: ## Auto-detect Coral and run simulation/hardware suites as appropriate
	$(PYTHON) -m edgecompiler.test_runner --mode auto

test-all: ## Run all tests including slow ones (no hardware)
	$(PYTEST) $(TESTDIR) -v -m "not hardware"
	$(PYTEST) $(SLOTHDIR)/tests -v

test-hardware: ## Run hardware-specific tests (requires device)
	$(PYTEST) $(TESTDIR) -v -m "hardware"

test-coral: ## Run Coral USB-specific tests
	$(PYTEST) $(TESTDIR) -v -m "coral"

test-metal: ## Run Apple Silicon Metal-specific tests
	$(PYTEST) $(TESTDIR) -v -m "metal"

test-pytorch: ## Run PyTorch frontend tests
	$(PYTEST) $(TESTDIR) -v -m "pytorch"

test-tensorflow: ## Run TensorFlow frontend tests
	$(PYTEST) $(TESTDIR) -v -m "tensorflow"

test-onnx: ## Run ONNX frontend tests
	$(PYTEST) $(TESTDIR) -v -m "onnx"

test-coverage: ## Run tests with coverage report
	$(PYTEST) $(TESTDIR) -v -m "not hardware" --cov=edgecompiler --cov-report=term-missing --cov-report=html

test-quick: ## Run fast tests only (skip slow + hardware)
	$(PYTEST) $(TESTDIR) -v -m "not hardware and not slow" -x --timeout=30

test-sloth: ## Run sloth integration test suite
	$(PYTEST) $(SLOTHDIR)/tests -v

# ── Code Quality ──────────────────────────────────────────────────────────────

lint: ## Run linter (ruff check)
	$(RUFF) check $(SRCDIR) $(TESTDIR) examples/ $(SLOTHDIR)/src $(SLOTHDIR)/tests $(SLOTHDIR)/examples

format: ## Auto-format code (ruff format)
	$(RUFF) format $(SRCDIR) $(TESTDIR) examples/ $(SLOTHDIR)/src $(SLOTHDIR)/tests $(SLOTHDIR)/examples

format-check: ## Check formatting without modifying files
	$(RUFF) format --check $(SRCDIR) $(TESTDIR) examples/ $(SLOTHDIR)/src $(SLOTHDIR)/tests $(SLOTHDIR)/examples

typecheck: ## Run type checking (mypy)
	$(MYPY) $(SRCDIR)/edgecompiler

check: lint format-check typecheck ## Run all code quality checks

# ── Documentation ─────────────────────────────────────────────────────────────

docs: ## Build MkDocs documentation site
	@pip show mkdocs-material > /dev/null 2>&1 || pip install mkdocs-material
	@cp README.md docs/index.md
	@sed -i '' -e 's|\./docs/examples.md|examples.md|g' docs/index.md
	@sed -i '' -e 's|docs/examples.md|examples.md|g' docs/index.md
	@sed -i '' -e 's|\./docs/architecture.md|architecture.md|g' docs/index.md
	@sed -i '' -e 's|docs/architecture.md|architecture.md|g' docs/index.md
	@sed -i '' -e 's|docs/coral_macos_setup.md|coral_macos_setup.md|g' docs/index.md
	@cp instructions.md docs/instructions.md
	@sed -i '' -e 's|docs/architecture.md|architecture.md|g' docs/instructions.md
	@sed -i '' -e 's|docs/coral_backend.md|coral_backend.md|g' docs/instructions.md
	@sed -i '' -e 's|docs/examples.md|examples.md|g' docs/instructions.md
	@sed -i '' -e 's|docs/coral_macos_setup.md|coral_macos_setup.md|g' docs/instructions.md
	@cp benchmarks.md docs/benchmarks.md
	@cp RESEARCH.md docs/research.md
	@cp $(SLOTHDIR)/docs/benchmarks_sloth.md docs/sloth_benchmarks.md
	@cp $(SLOTHDIR)/instructions_sloth_integration.md docs/sloth_integration.md
	mkdocs build
	@echo "✓ Documentation built in site/"

docs-serve: ## Serve MkDocs documentation locally (live-reload)
	@pip show mkdocs-material > /dev/null 2>&1 || pip install mkdocs-material
	@cp README.md docs/index.md
	@sed -i '' -e 's|\./docs/examples.md|examples.md|g' docs/index.md
	@sed -i '' -e 's|docs/examples.md|examples.md|g' docs/index.md
	@sed -i '' -e 's|\./docs/architecture.md|architecture.md|g' docs/index.md
	@sed -i '' -e 's|docs/architecture.md|architecture.md|g' docs/index.md
	@sed -i '' -e 's|docs/coral_macos_setup.md|coral_macos_setup.md|g' docs/index.md
	@cp instructions.md docs/instructions.md
	@sed -i '' -e 's|docs/architecture.md|architecture.md|g' docs/instructions.md
	@sed -i '' -e 's|docs/coral_backend.md|coral_backend.md|g' docs/instructions.md
	@sed -i '' -e 's|docs/examples.md|examples.md|g' docs/instructions.md
	@sed -i '' -e 's|docs/coral_macos_setup.md|coral_macos_setup.md|g' docs/instructions.md
	@cp benchmarks.md docs/benchmarks.md
	@cp RESEARCH.md docs/research.md
	@cp $(SLOTHDIR)/docs/benchmarks_sloth.md docs/sloth_benchmarks.md
	@cp $(SLOTHDIR)/instructions_sloth_integration.md docs/sloth_integration.md
	mkdocs serve

docs-deploy: ## Deploy MkDocs documentation to GitHub Pages
	@pip show mkdocs-material > /dev/null 2>&1 || pip install mkdocs-material
	@cp README.md docs/index.md
	@sed -i '' -e 's|\./docs/examples.md|examples.md|g' docs/index.md
	@sed -i '' -e 's|docs/examples.md|examples.md|g' docs/index.md
	@sed -i '' -e 's|\./docs/architecture.md|architecture.md|g' docs/index.md
	@sed -i '' -e 's|docs/architecture.md|architecture.md|g' docs/index.md
	@sed -i '' -e 's|docs/coral_macos_setup.md|coral_macos_setup.md|g' docs/index.md
	@cp instructions.md docs/instructions.md
	@sed -i '' -e 's|docs/architecture.md|architecture.md|g' docs/instructions.md
	@sed -i '' -e 's|docs/coral_backend.md|coral_backend.md|g' docs/instructions.md
	@sed -i '' -e 's|docs/examples.md|examples.md|g' docs/instructions.md
	@sed -i '' -e 's|docs/coral_macos_setup.md|coral_macos_setup.md|g' docs/instructions.md
	@cp benchmarks.md docs/benchmarks.md
	@cp RESEARCH.md docs/research.md
	@cp $(SLOTHDIR)/docs/benchmarks_sloth.md docs/sloth_benchmarks.md
	@cp $(SLOTHDIR)/instructions_sloth_integration.md docs/sloth_integration.md
	mkdocs gh-deploy --force

# ── Clean ─────────────────────────────────────────────────────────────────────

clean: ## Remove build artifacts
	rm -rf $(BUILDDIR)
	rm -rf $(DISTDIR)
	rm -rf $(EGGDIR)
	rm -rf .pytest_cache
	rm -rf .mypy_cache
	rm -rf .ruff_cache
	rm -rf htmlcov
	rm -rf .coverage
	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 "✓ Cleaned build artifacts"

distclean: clean ## Remove everything including compiled models
	rm -rf *.tflite *.mlpackage *.onnx *.pt *.npy
	rm -rf models/
	@echo "✓ Cleaned all artifacts"

# ── Coral Setup ───────────────────────────────────────────────────────────────

coral-setup: ## Install Coral USB runtime on macOS
	bash scripts/install_coral_runtime.sh

download-models: ## Download Coral hardware test models
	bash scripts/download_models.sh --output-dir tests/hardware/test_models

# ── Benchmarks ────────────────────────────────────────────────────────────────

benchmark: ## Run benchmark script
	$(PYTHON) scripts/benchmark.py --iterations 100

benchmark-quick: ## Quick benchmark (10 iterations)
	$(PYTHON) scripts/benchmark.py --iterations 10

benchmark-report: ## Show local benchmark report markdown
	@cat benchmarks.md
	@echo ""
	@echo "sloth integration benchmark report:"
	@cat $(SLOTHDIR)/docs/benchmarks_sloth.md

# ── Build / Distribution ─────────────────────────────────────────────────────

build: ## Build distribution packages
	$(PYTHON) -m build

publish-test: ## Publish to Test PyPI
	twine upload --repository testpypi $(DISTDIR)/*

publish: ## Publish to PyPI
	twine upload $(DISTDIR)/*

# ── Development Helpers ───────────────────────────────────────────────────────

version: ## Show current version
	@$(PYTHON) -c "import importlib.metadata; print(importlib.metadata.version('edgecompiler'))" 2>/dev/null || echo "Not installed"

deps: ## Show installed dependencies
	@$(PIP) show edgecompiler 2>/dev/null || echo "Not installed via pip"

tree: ## Show project structure
	@find . -not -path './.git/*' -not -path './.mypy_cache/*' -not -path './.ruff_cache/*' -not -path './__pycache__/*' -not -path '*/node_modules/*' -not -path './build/*' -not -path './dist/*' | head -80
