LANGS := $(notdir $(wildcard plugin/languages/*))

.PHONY: build clean bootstrap \
        install lock sync \
        lint format typecheck docs-check test check tidy \
        check-version version-dev version-release

##############
# Plugin     #
##############

build: clean
	@for lang in $(LANGS); do \
		echo "Assembling plugin ($$lang) → dist-$$lang/"; \
		rsync -a --exclude='languages/' plugin/ dist-$$lang/; \
		cp -R plugin/languages/$$lang/agents/ dist-$$lang/agents/; \
		cp -R plugin/languages/$$lang/references/ dist-$$lang/references/; \
		cp -R plugin/languages/$$lang/skills/ dist-$$lang/skills/; \
		find dist-$$lang -type f -name '*.md' -exec \
			sed -i '' "s|languages/$$lang/references/|references/|g" {} +; \
		find dist-$$lang -type f -name '*.md' -exec \
			sed -i '' "s|languages/$$lang/agents/|agents/|g" {} +; \
		find dist-$$lang -type f -name '*.md' -exec \
			sed -i '' "s|languages/$$lang/skills/|skills/|g" {} +; \
		find dist-$$lang -name '.DS_Store' -delete; \
		echo "Done. dist-$$lang/"; \
		echo ""; \
	done
	@echo "Built: $(foreach l,$(LANGS),dist-$l/)"

clean:
	rm -rf $(foreach l,$(LANGS),dist-$l/)

##############
# Scaffold   #
##############

# Scaffold optimization projects: make bootstrap ORG=roboflow PROJECTS="supervision inference"
bootstrap:
ifndef ORG
	$(error ORG is required. Usage: make bootstrap ORG=roboflow PROJECTS="supervision inference")
endif
ifndef PROJECTS
	$(error PROJECTS is required. Usage: make bootstrap ORG=roboflow PROJECTS="supervision inference")
endif
	@echo "Scaffolding projects under: .codeflash/$(ORG)/"
	@for proj in $(PROJECTS); do \
		bash scripts/scaffold.sh $(ORG) $$proj .codeflash/$(ORG)/$$proj; \
	done
	@echo ""
	@echo "Next steps:"
	@echo "  1. Fill in infra/cloud-init.yaml with project-specific setup"
	@echo "  2. Add benchmark scripts to bench/"
	@echo "  3. Edit README.md with project description and methodology"
	@echo "  4. Update status.md with current project state"

##############
# Install    #
##############

install: ## Install all workspace packages
	uv sync --locked --all-packages

lock: ## Re-lock the workspace
	uv lock

sync: ## Sync a specific package: make sync PKG=codeflash-core
	uv sync --locked --package $(PKG)

##############
# Quality    #
##############

lint: ## Run ruff linter on packages
	uv run ruff check packages/

format: ## Check formatting on packages
	uv run ruff format --check packages/

typecheck: ## Run mypy on package sources
	uv run mypy packages/codeflash-core/src/ packages/codeflash-python/src/

docs-check: ## Check docstring coverage
	uv run interrogate packages/codeflash-core/src/ packages/codeflash-python/src/

test: ## Run all package tests
	uv run pytest packages/ -v

check: lint format typecheck docs-check ## Run all checks (no tests)

tidy: ## Auto-fix formatting and lint issues
	uv run ruff format packages/
	uv run ruff check --fix-only --show-fixes packages/

##############
# Versioning #
##############

check-version: ## Verify version was bumped (CI)
	uv run python scripts/versioning.py check-version $(ARGS)

version-dev: ## Bump to pre-release version with changelog entry
	uv run python scripts/versioning.py version-dev $(ARGS)

version-release: ## Release version, aggregate changelogs
	uv run python scripts/versioning.py version-release $(ARGS)
	uv run python scripts/combine-changelogs.py $(ARGS)
