# Copyright (c) 2026 Reactor Technologies, Inc. All rights reserved.

##@ Configuration

VERSION  := $(shell sed -n 's/^version = "\(.*\)"/\1/p' pyproject.toml)
GIT_SHA  := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
RELEASE  := v$(VERSION)-g$(GIT_SHA)
PYTHON   ?= python3
SRC_DIRS  = src/ tests/

##@ General

.PHONY: help
help: ## Display this help
	@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n  make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf "  \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

.PHONY: version
version: ## Display the current version
	@echo "$(VERSION)"

.PHONY: release
release: ## Display the release (version + git SHA)
	@echo "$(RELEASE)"

##@ Setup

.PHONY: install
install: ## Install package in editable mode with dev dependencies
	@echo "--- 📦 Installing reactor-sdk[dev] in editable mode"
	pip install -e ".[dev]"

##@ Build

.PHONY: build
build: ## Build source and wheel distributions
	@echo "--- 🛠️ Building reactor-sdk $(RELEASE)"
	pip install build
	$(PYTHON) -m build

##@ Linting

.PHONY: lint
lint: ## Run all linters (ruff check + mypy)
	@echo "--- 🔎 Running ruff check"
	ruff check $(SRC_DIRS)
	@echo "--- 🔎 Running mypy"
	mypy src/

.PHONY: lint-fix
lint-fix: ## Auto-fix lint issues (ruff check --fix + ruff format)
	@echo "--- 🔧 Running ruff check --fix"
	ruff check --fix $(SRC_DIRS)
	@echo "--- 🔧 Running ruff format"
	ruff format $(SRC_DIRS)

.PHONY: format
format: ## Format code with ruff
	@echo "--- 🔧 Running ruff format"
	ruff format $(SRC_DIRS)

.PHONY: format-check
format-check: ## Check formatting without modifying files
	@echo "--- 🔎 Checking formatting"
	ruff format --check $(SRC_DIRS)

##@ Testing

.PHONY: test
test: ## Run unit tests
	@echo "--- 🧪 Running unit tests"
	pytest tests/unit -v

.PHONY: test-integration
test-integration: ## Run integration tests (requires REACTOR_API_KEY)
	@test -n "$(REACTOR_API_KEY)" || (echo "ERROR: REACTOR_API_KEY is not set" && exit 1)
	@echo "--- 🧪 Running integration tests"
	pytest tests/integration -v

.PHONY: test-all
test-all: test test-integration ## Run all tests

.PHONY: typecheck
typecheck: ## Run mypy type checking
	@echo "--- 🔎 Running mypy"
	mypy src/

##@ CI

.PHONY: ci-lint
ci-lint: ## CI: install ruff and run lint check
	@echo "--- 🔎 Running CI lint"
	pip install ruff
	ruff check $(SRC_DIRS)

.PHONY: ci-build
ci-build: ## CI: build the package
	@echo "--- 🛠️ Running CI build"
	pip install build
	$(PYTHON) -m build

.PHONY: ci-test
ci-test: ## CI: install deps and run unit tests
	@echo "--- 🧪 Running CI unit tests"
	pip install -e ".[dev]"
	pytest tests/unit -v

.PHONY: ci-test-integration
ci-test-integration: ## CI: install deps and run integration tests
	@test -n "$(REACTOR_API_KEY)" || (echo "ERROR: REACTOR_API_KEY is not set" && exit 1)
	@echo "--- 🧪 Running CI integration tests"
	pip install -e ".[dev]"
	pytest tests/integration -v

##@ Cleanup

.PHONY: clean
clean: ## Remove build artifacts and caches
	@echo "--- 🧹 Cleaning build artifacts"
	@rm -rf dist/ build/ *.egg-info src/*.egg-info
	@rm -rf .pytest_cache .mypy_cache .ruff_cache
	@rm -rf __pycache__ src/**/__pycache__ tests/**/__pycache__
	@echo "--- ✅ Clean"
