# pydantic-versions Makefile
# Core development commands for local work and CI parity.

REVISION ?= HEAD
RANGE ?= origin/main...HEAD

.PHONY: all install format lint typecheck dead-code test test-cov check ci build clean help
.PHONY: docs-build docs-build-strict docs-serve
.PHONY: policy-check commit-check change-check workflow-lint

# Default target - run all checks
all: format lint typecheck dead-code test

# Install dependencies
install:
	uv sync --frozen

# Format code with Ruff
format:
	uv run ruff format .

# Lint code with Ruff (with auto-fix)
lint:
	uv run ruff check . --fix

# Type check the implementation and public consumer contract
typecheck:
	uv run ty check
	uv run mypy --strict tests/typing/schema_family_contract.py

# Scan production code for unreachable or unreferenced definitions
dead-code:
	uv run vulture

# Run all tests
test:
	uv run pytest

# Run tests with coverage
test-cov:
	uv run pytest --cov=src --cov-report=html --cov-report=term

# YAGA snapshot policies inspect committed objects; commit edits first.
policy-check:
	uv run --frozen yaga repo check --plan .yaga/checks/repository.toml --revision "$(REVISION)"

commit-check:
	uv run --frozen yaga commit check --commit "$(REVISION)"

change-check:
	uv run --frozen yaga change check --policy .yaga/change-policy.toml --range "$(RANGE)"

# Requires Docker with Linux containers.
workflow-lint:
	uv run --frozen yaga workflow lint .github/workflows

# Run lint, typecheck, dead-code, and repository analysis (no formatting)
check: policy-check
	uv run ruff check .
	uv run ty check
	uv run mypy --strict tests/typing/schema_family_contract.py
	uv run vulture

# CI check - all validations without modifications
ci: policy-check workflow-lint
	uv run ruff format --check .
	uv run ruff check .
	uv run ty check
	uv run mypy --strict tests/typing/schema_family_contract.py
	uv run vulture
	uv run pytest --cov=src --cov-report=xml --cov-report=term
	@echo "All CI checks passed!"

# Build the package
build:
	uv build

# Build docs
docs-build:
	uv run zensical build

# Build docs in strict mode (CI)
docs-build-strict:
	uv run zensical build --strict

# Serve docs locally at http://127.0.0.1:8000
docs-serve:
	uv run zensical serve --dev-addr 127.0.0.1:8000

# Clean up cache and build files
clean:
	python -c "import pathlib, shutil; [shutil.rmtree(path, ignore_errors=True) for path in map(pathlib.Path, ['.pytest_cache', '.ruff_cache', 'htmlcov', 'dist', 'build', 'site'])]; [path.unlink(missing_ok=True) for path in map(pathlib.Path, ['.coverage', 'coverage.xml'])]; [shutil.rmtree(path, ignore_errors=True) for path in pathlib.Path('.').rglob('__pycache__')]; [shutil.rmtree(path, ignore_errors=True) for path in pathlib.Path('src').glob('*.egg-info')]"

# Show help
help:
	@echo "pydantic-versions Development Commands"
	@echo ""
	@echo "Setup:"
	@echo "  install           - Install dependencies with uv"
	@echo ""
	@echo "Development:"
	@echo "  format            - Format code with Ruff"
	@echo "  lint              - Lint code with Ruff (auto-fix)"
	@echo "  typecheck         - Run ty and the external mypy consumer contract"
	@echo "  dead-code         - Scan production code with Vulture"
	@echo "  check             - Run lint, typecheck, and dead-code analysis"
	@echo "  policy-check      - Check workflows and committed repository policies"
	@echo "  commit-check      - Check one commit message (REVISION=HEAD)"
	@echo "  change-check      - Require tests with source changes (RANGE=origin/main...HEAD)"
	@echo "  workflow-lint     - Lint workflows with YAGA (requires Docker)"
	@echo ""
	@echo "Testing:"
	@echo "  test              - Run all tests"
	@echo "  test-cov          - Run tests with coverage"
	@echo "  docs-build        - Build Zensical site"
	@echo "  docs-build-strict - Build Zensical site (strict mode)"
	@echo "  docs-serve        - Serve docs locally at http://127.0.0.1:8000"
	@echo ""
	@echo "CI/CD:"
	@echo "  all               - Run format, lint, typecheck, dead-code, test"
	@echo "  ci                - Run all checks (no modifications)"
	@echo "  build             - Build the package"
	@echo "  clean             - Clean cache and build files"
