.PHONY: help install dev test lint format typecheck clean build docs benchmark-scanner lint-gate check check-full tech-debt-dashboard dashboard-forensic pr-check lint-imports post-phase6-maintenance validate-pr-learning check-pr-learning-reminder

PYTHON := python3
UV := uv
VENV := venruk-env
CI_DIR := scripts/ci

# Gate paths — single source in scripts/ci/*.txt (see docs/engineering/ONBOARDING.md)
GATE_LINT := $(shell $(PYTHON) $(CI_DIR)/read_paths.py $(CI_DIR)/lint-gate-paths.txt)
GATE_FORMAT := $(shell $(PYTHON) $(CI_DIR)/read_paths.py $(CI_DIR)/format-gate-paths.txt)
GATE_TEST := $(shell $(PYTHON) $(CI_DIR)/read_paths.py $(CI_DIR)/test-gate-paths.txt)
GATE_TYPECHECK := $(shell $(PYTHON) $(CI_DIR)/read_paths.py $(CI_DIR)/typecheck-gate-paths.txt)

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

venv: ## Create virtual environment
	$(UV) venv $(VENV) --python 3.12
	@echo "Activate with: source $(VENV)/bin/activate"

install: ## Install in production mode
	$(UV) pip install .

dev: ## Install in development mode with all extras
	$(UV) pip install -e ".[dev]"

dev-all: ## Install with all extras (AI, dashboard, tree-sitter)
	$(UV) pip install -e ".[all]"

test: ## Run tests
	pytest tests/ -v --tb=short

test-cov: ## Run tests with coverage
	pytest tests/ -v --cov=src/venruk --cov-report=html --cov-report=term-missing

test-unit: ## Run only unit tests
	pytest tests/unit/ -v

test-integration: ## Run only integration tests
	pytest tests/integration/ -v -m integration

lint: ## Run linter (full repo — informational)
	ruff check src/ tests/

lint-fix: ## Run linter with auto-fix
	ruff check src/ tests/ --fix

format: ## Format code
	ruff format src/ tests/

format-check: ## Check code formatting (full repo)
	ruff format src/ tests/ --check

format-check-gate: ## Format check on progressive gate paths
	ruff format $(GATE_FORMAT) --check

typecheck: ## Run type checker (full src/venruk)
	mypy src/venruk

typecheck-gate: ## Typecheck progressive gate paths
	mypy $(GATE_TYPECHECK)

benchmark-scanner: ## OWASP scanner benchmark + recall + taint gates
	pytest tests/benchmark/scanner/ -v --tb=short

lint-gate: ## Ruff essential rules on progressive gate paths
	ruff check --select F,E,W,I $(GATE_LINT)

lint-imports: ## Enforce architecture boundaries via import-linter
	lint-imports

test-gate: ## Wave regression tests (scoped — see EXCLUDED_TESTS.md)
	pytest $(GATE_TEST) -v --tb=short

post-phase6-maintenance: ## MP6 frozen baseline + GTM gate (production release)
	bash scripts/ci/run-post-phase6-maintenance.sh

validate-pr-learning: ## Validate PR/CI issue learning dataset JSON
	$(UV) run python scripts/engineering/pr_issue_learning.py validate

check-pr-learning-reminder: ## Fail if CI-fix paths changed without dataset append (PRs)
	$(UV) run python scripts/engineering/pr_issue_learning.py check-pr-reminder --base origin/main

check: validate-pr-learning lint-gate lint-imports format-check-gate typecheck-gate test-gate benchmark-scanner ## PR CI gate (progressive hardening)

check-full: lint format-check typecheck test ## Full repo check (MRP-08 target)

tech-debt-dashboard: ## Regenerate engineering dashboard (Rich terminal + md + html + json)
	$(UV) run python scripts/venruk_dashboard.py

dashboard-forensic: ## Full dashboard with forensic detail
	$(UV) run python scripts/venruk_dashboard.py --density forensic --format all

pr-check: ## PR-comment markdown (skip gates for speed)
	$(UV) run python scripts/venruk_dashboard.py --format pr-comment --skip-gates

clean: ## Clean build artifacts
	rm -rf build/ dist/ *.egg-info .pytest_cache .mypy_cache .ruff_cache htmlcov/ .coverage
	find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true

build: clean ## Build distribution packages
	$(PYTHON) -m build

scan: ## Run venruk on itself (dogfooding)
	venruk scan

scan-full: ## Run full venruk scan on itself
	venruk scan --profile enterprise
