# mcp-fact-finder Makefile
# Semantic versioning, quality gates, PyPI + GitHub Release + Homebrew publishing

.DEFAULT_GOAL := help

-include .makefiles/common.mk
-include .makefiles/quality.mk
-include .makefiles/testing.mk
-include .makefiles/deps.mk

PROJECT_NAME := mcp-fact-finder
PYTHON_VERSION := 3.12
UV := uv
PYTHON := uv run python  # use project venv, not system Python

VERSION_PY := src/mcp_fact_finder/__init__.py

# ============================================================================
# Help
# ============================================================================

help: ## Show available targets
	@printf '\033[0;34mmcp-fact-finder -- Available targets:\033[0m\n'
	@awk 'BEGIN{FS=":.*## "} /^[a-zA-Z_-]+:.*## /{printf "  \033[0;32m%-28s\033[0m %s\n", $$1, $$2}' \
		Makefile .makefiles/*.mk 2>/dev/null

# ============================================================================
# Development
# ============================================================================

.PHONY: install install-dev sync run-mcp run-serve run-index setup-corpus

install: ## Install package (production deps)
	$(UV) sync

install-dev: ## Install with all dev extras
	$(UV) sync --all-extras

sync: ## Sync dependencies from lockfile
	$(UV) sync --all-extras

run-mcp: ## Start MCP server pointing at default corpus
	$(UV) run mcp-fact-finder mcp --project-root ~/Duetto/cto

run-serve: ## Start web UI on port 8765
	$(UV) run mcp-fact-finder serve --port 8765

run-index: ## Run pass-1 indexing on default corpus
	$(UV) run mcp-fact-finder index --path ~/Duetto/cto --pass 1

setup-corpus: ## Zero-interaction setup against default corpus
	$(UV) run mcp-fact-finder setup --no-index

# ============================================================================
# Quality (delegated to .makefiles/quality.mk)
# ============================================================================

.PHONY: check

check: quality ## Alias for quality (lint + type-check)

# ============================================================================
# Publish
# ============================================================================

.PHONY: publish publish-minor publish-major publish-only _do-publish

publish: pre-publish ## Bump patch version, build, publish PyPI + GitHub + Homebrew
	@echo "$(BLUE)══════════════════════════════════════════$(NC)"
	@echo "$(BLUE)  Publishing Patch Release$(NC)"
	@echo "$(BLUE)══════════════════════════════════════════$(NC)"
	@$(MAKE) _do-publish BUMP=patch

publish-minor: pre-publish ## Bump minor version, publish
	@echo "$(BLUE)══════════════════════════════════════════$(NC)"
	@echo "$(BLUE)  Publishing Minor Release$(NC)"
	@echo "$(BLUE)══════════════════════════════════════════$(NC)"
	@$(MAKE) _do-publish BUMP=minor

publish-major: pre-publish ## Bump major version, publish
	@echo "$(BLUE)══════════════════════════════════════════$(NC)"
	@echo "$(BLUE)  Publishing Major Release$(NC)"
	@echo "$(BLUE)══════════════════════════════════════════$(NC)"
	@$(MAKE) _do-publish BUMP=major

publish-only: ## Publish current version to PyPI (no version bump)
	@rm -rf dist/
	@$(UV) build
	@if [ -f .env.local ]; then \
		. .env.local && UV_PUBLISH_TOKEN="$$PYPI_TOKEN" $(UV) publish; \
		echo "$(GREEN)✓ Published to PyPI$(NC)"; \
	else \
		echo "$(RED)✗ .env.local not found -- set PYPI_TOKEN$(NC)"; \
		exit 1; \
	fi

_do-publish: ## Internal: bump + build + publish (use publish/publish-minor/publish-major)
	@CURRENT=$$(grep -E '^__version__' $(VERSION_PY) | sed 's/.*"\(.*\)"/\1/'); \
	MAJOR=$$(echo $$CURRENT | cut -d. -f1); \
	MINOR=$$(echo $$CURRENT | cut -d. -f2); \
	PATCH=$$(echo $$CURRENT | cut -d. -f3); \
	case "$(BUMP)" in \
	  patch) NEW="$$MAJOR.$$MINOR.$$((PATCH + 1))";; \
	  minor) NEW="$$MAJOR.$$((MINOR + 1)).0";; \
	  major) NEW="$$((MAJOR + 1)).0.0";; \
	  *) echo "$(RED)Unknown BUMP: $(BUMP)$(NC)"; exit 1;; \
	esac; \
	echo "$(YELLOW)Version: $$CURRENT --> $$NEW$(NC)"; \
	sed -i '' "s/__version__ = \".*\"/__version__ = \"$$NEW\"/" $(VERSION_PY); \
	sed -i '' "s/^version = \".*\"/version = \"$$NEW\"/" pyproject.toml; \
	echo "$(GREEN)✓ Version bumped in __init__.py + pyproject.toml$(NC)"; \
	git add $(VERSION_PY) pyproject.toml; \
	git commit -m "chore: bump version to $$NEW"; \
	git tag "v$$NEW"; \
	git push && git push --tags; \
	echo "$(GREEN)✓ Tagged + pushed v$$NEW$(NC)"; \
	rm -rf dist/; \
	$(UV) build; \
	echo "$(GREEN)✓ Built package$(NC)"; \
	if [ -f .env.local ]; then \
		. .env.local && UV_PUBLISH_TOKEN="$$PYPI_TOKEN" $(UV) publish; \
		echo "$(GREEN)✓ Published to PyPI$(NC)"; \
	else \
		echo "$(RED)✗ .env.local not found -- set PYPI_TOKEN$(NC)"; \
		exit 1; \
	fi; \
	gh release create "v$$NEW" \
		--title "v$$NEW" \
		--generate-notes \
		dist/*; \
	echo "$(GREEN)✓ GitHub Release created$(NC)"; \
	if [ -f .env.local ]; then . .env.local; fi; \
	if [ -n "$$HOMEBREW_TAP_TOKEN" ]; then \
		echo "$(BLUE)Updating Homebrew formula...$(NC)"; \
		HOMEBREW_TAP_TOKEN="$$HOMEBREW_TAP_TOKEN" scripts/wait_and_update_homebrew.sh $$NEW || \
			echo "$(YELLOW)⚠ Homebrew update failed (non-blocking)$(NC)"; \
	else \
		echo "$(YELLOW)⚠ HOMEBREW_TAP_TOKEN not set -- skipping Homebrew$(NC)"; \
	fi; \
	echo "$(GREEN)══════════════════════════════════════════$(NC)"; \
	echo "$(GREEN)  ✓ Published mcp-fact-finder $$NEW$(NC)"; \
	echo "$(GREEN)══════════════════════════════════════════$(NC)"

# ============================================================================
# Homebrew
# ============================================================================

.PHONY: homebrew-update homebrew-update-dry-run

homebrew-update: ## Update Homebrew tap formula with latest version
	@if [ -z "$$HOMEBREW_TAP_TOKEN" ] && [ -f .env.local ]; then . .env.local; fi; \
	VERSION=$$(grep -E '^__version__' $(VERSION_PY) | sed 's/.*"\(.*\)"/\1/'); \
	python3 scripts/update_homebrew_formula.py --version $$VERSION --verbose

homebrew-update-dry-run: ## Dry-run Homebrew formula update
	@VERSION=$$(grep -E '^__version__' $(VERSION_PY) | sed 's/.*"\(.*\)"/\1/'); \
	python3 scripts/update_homebrew_formula.py --version $$VERSION --dry-run --verbose

# ============================================================================
# Build / Clean
# ============================================================================

.PHONY: build clean

build: ## Build distribution packages (wheel + sdist)
	@rm -rf dist/
	@$(UV) build
	@echo "$(GREEN)✓ Built:$(NC)"; ls -lh dist/

clean: ## Remove build artifacts and caches
	@rm -rf dist/ build/ *.egg-info .ruff_cache .mypy_cache
	@find . -type d -name __pycache__ -not -path "*/.venv/*" -exec rm -rf {} + 2>/dev/null || true
	@echo "$(GREEN)✓ Clean$(NC)"

# ============================================================================
# Version
# ============================================================================

.PHONY: version

version: ## Show current version
	@grep -E '^__version__' $(VERSION_PY) | sed 's/.*"\(.*\)"/mcp-fact-finder \1/'
