.DEFAULT_GOAL := help
PYTHON        := uv run python
UV            := uv

.PHONY: help install install-dev sync test test-unit test-property test-integration
.PHONY: lint fmt typecheck check fix pre-commit build publish-test publish clean

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

# ── Environment ──────────────────────────────────────────────────────────────

install:        ## Install package + all optional extras + dev tools
	$(UV) sync --all-extras

install-dev:    ## Install package + dev tools only (no LangChain / LlamaIndex)
	$(UV) sync --extra dev

sync:           ## Re-sync environment from lockfile (use after pulling)
	$(UV) sync --all-extras

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

test:           ## Run full test suite with coverage
	$(UV) run pytest tests/ -v \
		--cov=credens \
		--cov-report=term-missing \
		--cov-report=html:htmlcov \
		--cov-fail-under=85

test-unit:      ## Unit tests only (fast, no extras needed)
	$(UV) run pytest tests/unit/ -v

test-property:  ## Property-based tests only (hypothesis)
	$(UV) run pytest tests/property/ -v --hypothesis-seed=0

test-integration: ## Integration smoke tests (requires extras installed)
	$(UV) run pytest tests/integration/ -v

test-fast:      ## Unit + property tests, no coverage overhead
	$(UV) run pytest tests/unit/ tests/property/ -v

# ── Code quality ──────────────────────────────────────────────────────────────

lint:           ## Check lint rules without modifying files
	$(UV) run ruff check credens/ tests/

fmt:            ## Format source files in-place
	$(UV) run ruff format credens/ tests/

fmt-check:      ## Check formatting without modifying files (used in CI)
	$(UV) run ruff format --check credens/ tests/

typecheck:      ## Run mypy in strict mode
	$(UV) run mypy credens/

check: lint fmt-check typecheck  ## Run all checks (no modifications)
	@echo "All checks passed."

fix:            ## Auto-fix lint issues and reformat (local dev only)
	$(UV) run ruff check --fix credens/ tests/
	$(UV) run ruff format credens/ tests/

pre-commit:     ## Run pre-commit hooks on all files
	$(UV) run pre-commit run --all-files

# ── Build & publish ───────────────────────────────────────────────────────────

build:          ## Build wheel and sdist into dist/
	$(UV) build

publish-test:   ## Upload to TestPyPI (dry run)
	$(UV) publish --repository testpypi

publish:        ## Upload to PyPI (production — requires credentials)
	$(UV) publish

# ── Housekeeping ──────────────────────────────────────────────────────────────

clean:          ## Remove build artefacts, caches, coverage data
	rm -rf dist/ build/ *.egg-info htmlcov/ .coverage .mypy_cache .ruff_cache
	find . -type d -name __pycache__ -exec rm -rf {} +
	find . -type d -name .pytest_cache -exec rm -rf {} +
