.PHONY: install install-dev lint format typecheck test test-unit test-integration \
        coverage build publish clean init add list delete run help

# ── Setup ──────────────────────────────────────────────────────────────────────

install:
	pip install -e .

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

# ── Code Quality ───────────────────────────────────────────────────────────────

lint:
	ruff check rsil/ tests/

format:
	ruff format rsil/ tests/

typecheck:
	mypy rsil/

check: lint typecheck

# ── Tests ──────────────────────────────────────────────────────────────────────

test:
	pytest

test-unit:
	pytest tests/unit/

test-integration:
	pytest tests/integration/

coverage:
	pytest --cov=rsil --cov-report=html
	@echo "Report at htmlcov/index.html"

# ── Build & Publish ────────────────────────────────────────────────────────────

build:
	pip install build --quiet
	python -m build
	@echo "Artifacts in dist/"

publish: build
	pip install twine --quiet
	twine upload dist/*

publish-test: build
	pip install twine --quiet
	twine upload --repository testpypi dist/*

# ── CLI shortcuts (uses RSIL_HOME=~/.rsil by default) ─────────────────────────

init:
	rsil init

# Usage: make add KEY=STRIPE_KEY VALUE=sk_test_abc SERVICE=my-app
add:
	rsil add $(KEY)=$(VALUE) --service $(SERVICE)

# Usage: make list SERVICE=my-app
list:
	rsil list --service $(SERVICE)

# Usage: make delete KEY=STRIPE_KEY SERVICE=my-app
delete:
	rsil delete $(KEY) --service $(SERVICE)

# Usage: make run SERVICE=my-app CMD="python app.py"
run:
	rsil run --service $(SERVICE) -- $(CMD)

# ── Cleanup ────────────────────────────────────────────────────────────────────

clean:
	find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null; true
	find . -type f -name "*.pyc" -delete
	rm -rf .coverage htmlcov/ .pytest_cache/ .mypy_cache/ .ruff_cache/ dist/ *.egg-info/

# ── Help ───────────────────────────────────────────────────────────────────────

help:
	@echo "RSIL — Makefile targets"
	@echo ""
	@echo "  Setup"
	@echo "    install          Install RSIL (no dev deps)"
	@echo "    install-dev      Install with dev deps + pre-commit hooks"
	@echo ""
	@echo "  Code quality"
	@echo "    lint             Run ruff linter"
	@echo "    format           Run ruff formatter"
	@echo "    typecheck        Run mypy"
	@echo "    check            Run lint + typecheck"
	@echo ""
	@echo "  Tests"
	@echo "    test             Run all tests"
	@echo "    test-unit        Run unit tests only"
	@echo "    test-integration Run integration tests only"
	@echo "    coverage         Run tests with HTML coverage report"
	@echo ""
	@echo "  CLI"
	@echo "    init             rsil init"
	@echo "    add              rsil add KEY=\$$KEY VALUE=\$$VALUE SERVICE=\$$SERVICE"
	@echo "    list             rsil list SERVICE=\$$SERVICE"
	@echo "    delete           rsil delete KEY=\$$KEY SERVICE=\$$SERVICE"
	@echo "    run              rsil run SERVICE=\$$SERVICE CMD='\$$CMD'"
	@echo ""
	@echo "  Build & publish"
	@echo "    build            Build sdist + wheel into dist/"
	@echo "    publish          Build and upload to PyPI (requires PyPI credentials)"
	@echo "    publish-test     Build and upload to TestPyPI"
	@echo ""
	@echo "  Misc"
	@echo "    clean            Remove build artifacts and caches"
