.PHONY: install test lint fix clean check start finish merge tag status audit docker docs serve simulate

# ── Core ──

install:
	pip install -e ".[dev]"
	pre-commit install

test:
	pytest tests/ -v --tb=short --cov=src/orxaq \
		--cov-report=term-missing --cov-fail-under=90

lint:
	ruff check src/ tests/

fix:
	ruff check --fix src/ tests/
	ruff format src/ tests/

check: lint test  ## Full quality gate (lint + test + coverage)

clean:
	find . -type d -name __pycache__ -exec rm -rf {} +
	find . -type d -name .pytest_cache -exec rm -rf {} +
	find . -type d -name .ruff_cache -exec rm -rf {} +
	rm -rf *.egg-info dist build .coverage htmlcov

# ── Docker ──

docker:  ## Build Docker image
	docker build -t orxaq/orxaq:latest .

# ── Docs ──

docs:  ## Build documentation site
	mkdocs build

docs-serve:  ## Serve docs locally with live reload
	mkdocs serve

# ── Application ──

serve:  ## Launch Observatory dashboard
	python3 -m orxaq.cli serve

simulate:  ## Run adverse scenario simulation
	python3 -m orxaq.cli simulate --scenario adverse

# ── Prompt Lifecycle (segregation of duty) ──

start:  ## Start a prompt: make start P=credit-risk
	@test -n "$(P)" || (echo "Usage: make start P=<branch-name>" && exit 1)
	git checkout main && git pull
	git checkout -b feat/$(P)
	pip install -e ".[dev]"
	pytest tests/ -v --tb=short -q
	@echo "\n═══ Branch feat/$(P) ready. Paste prompt into Claude Code. ═══"

finish:  ## After Claude Code completes: make finish
	ruff check src/ tests/
	pytest tests/ -v --tb=short --cov=src/orxaq --cov-fail-under=90
	git push origin HEAD
	gh pr create --fill
	@echo "\n═══ PR created. Review and merge with: make merge ═══"

merge:  ## Merge current PR (Operator only — segregation of duty)
	gh pr merge --squash --delete-branch
	git checkout main && git pull
	@echo "\n═══ Merged to main. ═══"

tag:  ## Tag release: make tag V=0.2.0
	@test -n "$(V)" || (echo "Usage: make tag V=<version>" && exit 1)
	git tag -s v$(V) -m "Release v$(V)"
	git push origin v$(V)

# ── Status & Audit ──

status:  ## Show repo status
	@echo "── Recent commits ──"
	@git log --oneline -10
	@echo "\n── Branches ──"
	@git branch -a
	@echo "\n── Open PRs ──"
	@gh pr list 2>/dev/null || echo "(gh CLI not configured)"
	@echo "\n── Open Issues ──"
	@gh issue list --limit 10 2>/dev/null || echo "(gh CLI not configured)"

audit:  ## Show compliance audit trail
	@echo "── ADR commits ──"
	@git log --oneline --all --grep="adr\|ADR\|compliance\|governance"
	@echo "\n── Security commits ──"
	@git log --oneline --all --grep="security\|SAST\|secret\|vuln"
