.PHONY: help install install-deps lint check-cjk check-datetime openapi check-openapi format test integration cov ci clean

help:
	@echo "Targets:"
	@echo "  install       Install deps + pre-commit hooks (full dev setup)"
	@echo "  install-deps  Install deps only (uv sync --frozen, used by CI)"
	@echo "  lint          ruff (check + format-check) + import-linter + datetime discipline + openapi drift"
	@echo "  check-cjk     Scan for CJK outside the language-policy allowlist (advisory)"
	@echo "  check-datetime Scan for code that bypasses component/utils/datetime (HARD gate, run via lint)"
	@echo "  openapi       Regenerate docs/openapi.json from the FastAPI app"
	@echo "  check-openapi Verify docs/openapi.json matches app.openapi() (HARD gate, run via lint)"
	@echo "  format        Format src/tests with ruff"
	@echo "  test          pytest tests/unit"
	@echo "  integration   pytest tests/integration"
	@echo "  cov           pytest tests/unit + tests/integration with coverage (fail under 80%)"
	@echo "  ci            full CI: lint + test + integration"
	@echo "  clean         Remove caches"

# Sync deps from uv.lock; CI calls this directly. --frozen means "lock is the
# source of truth — fail rather than update it".
install-deps:
	uv sync --frozen

# One-stop dev setup: deps + pre-commit hooks (both pre-commit and commit-msg
# stages — gitlint runs on commit-msg).
install: install-deps
	uv run pre-commit install
	uv run pre-commit install --hook-type commit-msg

lint:
	uv run ruff check src tests
	uv run ruff format --check src tests
	uv run lint-imports
	uv run python scripts/check_datetime_discipline.py
	uv run python scripts/dump_openapi.py --check

# Advisory CJK scan (see .claude/rules/language-policy.md). Deliberately NOT
# wired into `lint` / `ci`: the policy is enforced by review and the rules
# doc, not a hard gate. Run on demand when touching potentially-CJK files.
check-cjk:
	uv run python scripts/check_cjk.py

# Datetime two-zone discipline scanner (see .claude/rules/datetime-handling.md).
# Wired into `lint` (and therefore `ci`) as a HARD gate — any code that
# bypasses ``component/utils/datetime`` (raw ``datetime.now()``,
# ``time.time()``, naked ``datetime(...)`` constructor, etc.) fails the build.
check-datetime:
	uv run python scripts/check_datetime_discipline.py

# OpenAPI schema export — produce docs/openapi.json from the FastAPI app.
# Run this after touching any HTTP route / DTO; commit the result.
openapi:
	uv run python scripts/dump_openapi.py

# OpenAPI drift gate (wired into `lint`). Re-renders the schema in memory
# and diffs it against the committed ``docs/openapi.json``; any drift
# fails the build with a unified diff. Forces the contract doc to track
# the code on every PR that touches the API surface.
check-openapi:
	uv run python scripts/dump_openapi.py --check

format:
	uv run ruff check --fix src tests
	uv run ruff format src tests

test:
	uv run pytest tests/unit -v

integration:
	uv run pytest tests/integration -v

# Coverage runs unit + integration so the number matches what CI's `test` and
# `integration` jobs actually exercise. Threshold starts at 80% (unit-only is
# currently 87%, unit+integration 91% — 80% leaves ~10pp headroom for normal
# churn). Bump as the suite stabilises.
cov:
	uv run pytest tests/unit tests/integration --cov=src/everos --cov-report=term-missing --cov-branch --cov-fail-under=80

ci: lint test integration

clean:
	rm -rf .pytest_cache .ruff_cache .uv-cache .mypy_cache .coverage htmlcov
	find . -type d -name __pycache__ -exec rm -rf {} +
	find . -type f -name '*.pyc' -delete
