IMAGE_NAME ?= mindreon/mai-cli
IMAGE_TAG ?= latest
IMAGE ?= $(IMAGE_NAME):$(IMAGE_TAG)
PLATFORMS ?= linux/amd64,linux/arm64
BUILDER ?= mai-cli-builder
TWINE ?= uv run --with twine twine

.PHONY: help install dev run test cov lint format typecheck build package-check release-check publish-pypi publish-testpypi publish-pip clean buildx-create image-build-local image-push image-build-push image-buildx image-buildx-push image-run-help

help: ## Show this help
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
		awk 'BEGIN {FS = ":.*?## "}; {printf "  %-12s %s\n", $$1, $$2}'

install: ## Create venv and install package + dev deps (editable)
	uv sync

dev: install ## Alias for install

run: ## Run the CLI, e.g. make run ARGS="status"
	uv run mai $(ARGS)

test: ## Run the test suite
	uv run pytest

cov: ## Run tests with coverage report
	uv run pytest --cov=mai --cov-report=term-missing

lint: ## Lint with ruff
	uv run ruff check src tests

format: ## Auto-format with ruff
	uv run ruff format src tests

typecheck: ## Static type check with mypy
	uv run mypy

build: ## Build wheel + sdist into dist/
	uv build

package-check: build ## Check dist/* package metadata
	$(TWINE) check dist/*

release-check: lint typecheck test package-check ## Run all checks before publishing

publish-pypi: package-check ## Publish dist/* to PyPI using PYPI_USERNAME/PYPI_PASSWORD
	@username="$${PYPI_USERNAME:-$${TWINE_USERNAME:-}}"; \
	password="$${PYPI_PASSWORD:-$${TWINE_PASSWORD:-}}"; \
	if [ -z "$$username" ] || [ -z "$$password" ]; then \
		echo "Set PYPI_USERNAME and PYPI_PASSWORD before publishing."; \
		exit 1; \
	fi; \
	TWINE_USERNAME="$$username" TWINE_PASSWORD="$$password" $(TWINE) upload --verbose dist/*

publish-testpypi: package-check ## Publish dist/* to TestPyPI
	@username="$${PYPI_USERNAME:-$${TWINE_USERNAME:-}}"; \
	password="$${PYPI_PASSWORD:-$${TWINE_PASSWORD:-}}"; \
	if [ -z "$$username" ] || [ -z "$$password" ]; then \
		echo "Set PYPI_USERNAME and PYPI_PASSWORD before publishing."; \
		exit 1; \
	fi; \
	TWINE_USERNAME="$$username" TWINE_PASSWORD="$$password" \
		TWINE_REPOSITORY_URL="https://test.pypi.org/legacy/" \
		$(TWINE) upload --verbose dist/*

publish-pip: package-check ## Publish dist/* to custom PYPI_REPOSITORY_URL
	@username="$${PYPI_USERNAME:-$${TWINE_USERNAME:-}}"; \
	password="$${PYPI_PASSWORD:-$${TWINE_PASSWORD:-}}"; \
	url="$${PYPI_REPOSITORY_URL:-$${TWINE_REPOSITORY_URL:-}}"; \
	if [ -z "$$url" ]; then \
		echo "Set PYPI_REPOSITORY_URL for custom pip repository publishing."; \
		exit 1; \
	fi; \
	if [ -z "$$username" ] || [ -z "$$password" ]; then \
		echo "Set PYPI_USERNAME and PYPI_PASSWORD before publishing."; \
		exit 1; \
	fi; \
	TWINE_USERNAME="$$username" TWINE_PASSWORD="$$password" TWINE_REPOSITORY_URL="$$url" \
		$(TWINE) upload --verbose dist/*

clean: ## Remove build/venv artifacts
	rm -rf dist build .pytest_cache .ruff_cache .mypy_cache **/__pycache__

buildx-create: ## Create or select Docker buildx builder
	@if ! docker buildx inspect $(BUILDER) >/dev/null 2>&1; then \
		docker buildx create --name $(BUILDER) --use; \
	else \
		docker buildx use $(BUILDER); \
	fi
	docker buildx inspect --bootstrap >/dev/null

image-build-local: ## Build local Docker image
	docker build -t $(IMAGE) .

image-push: ## Push Docker image
	docker push $(IMAGE)

image-build-push: image-build-local image-push ## Build and push Docker image

image-buildx: buildx-create ## Build multi-platform image without pushing
	docker buildx build \
		--platform $(PLATFORMS) \
		-t $(IMAGE) \
		.

image-buildx-push: buildx-create ## Build and push multi-platform image
	docker buildx build \
		--platform $(PLATFORMS) \
		-t $(IMAGE) \
		--push \
		.

image-run-help: ## Run mai --help in Docker image
	docker run --rm $(IMAGE) mai --help
