.PHONY: all setup setup-ci setup-quality setup-release install update lint lint-ruff lint-format typecheck lint-bandit lint-deps lint-all test test-unit pytest-cov clean workflow-lint workflow-dryrun workflow-check install-tools

PYTHON := python3
VENV := .venv
BIN := $(VENV)/bin

all: setup

# Full setup for local development
setup: $(VENV)/bin/activate .env install-tools
	@$(BIN)/pip install -e '.[dev]' 2>/dev/null; \
	export PATH="$(HOME)/.local/bin:$$PATH"; \
	$(PYTHON) -m devx.tools.setup --bin "$(BIN)"

# Lean setup for CI jobs (pytest + lint + runtime deps)
setup-ci: $(VENV)/bin/activate .env
	@$(BIN)/pip install -e '.[ci,lint]' 2>/dev/null; \
	$(PYTHON) -m devx.tools.setup --bin "$(BIN)" --extras "ci,lint" --no-pre-commit --no-tea-login

# Setup for quality job (lint + test deps, actionlint tool)
setup-quality: $(VENV)/bin/activate .env install-tools
	@$(BIN)/pip install -e '.[ci,lint]' 2>/dev/null; \
	export PATH="$(HOME)/.local/bin:$$PATH"; \
	$(PYTHON) -m devx.tools.setup --bin "$(BIN)" --extras "ci,lint" --no-pre-commit --no-tea-login

# Setup for release jobs (needs git-cliff, tea, lint tools)
setup-release: $(VENV)/bin/activate .env
	@$(BIN)/pip install -e '.[ci,lint]' 2>/dev/null; \
	$(PYTHON) -m devx.tools.install_tools --tool git-cliff --tool tea; \
	export PATH="$(HOME)/.local/bin:$$PATH"; \
	$(PYTHON) -m devx.tools.setup --bin "$(BIN)" --extras "ci,lint" --no-pre-commit

.env:
	@if [ ! -f .env ]; then cp .env.example .env; echo "Created .env from .env.example — please edit it."; fi

$(VENV)/bin/activate:
	@python3 -c "import sys; v=sys.version_info; assert v >= (3, 12), f'Python 3.12+ required, found {v.major}.{v.minor}'; print(f'Python {v.major}.{v.minor}.{v.micro} OK')"
	$(PYTHON) -m venv $(VENV)
	$(BIN)/pip install --upgrade pip setuptools wheel

install-tools:
	@$(BIN)/pip install -e '.' 2>/dev/null; \
	$(PYTHON) -m devx.tools.install_tools

lint-ruff:
	$(BIN)/ruff check src/ tests/

lint-format:
	$(BIN)/ruff format --check src/ tests/

typecheck:
	$(BIN)/pyright

lint-bandit:
	$(BIN)/bandit -r src/

lint: lint-ruff lint-format typecheck lint-bandit

lint-deps:
	@echo "Checking dependencies for known vulnerabilities..."
	@.venv/bin/python -m ensurepip 2>/dev/null || true
	@PIPAPI_PYTHON_LOCATION=$$(pwd)/.venv/bin/python .venv/bin/pip-audit --desc --skip-editable 2>&1 || true

lint-all: lint workflow-lint

workflow-lint:
	@command -v actionlint >/dev/null 2>&1 || { echo "actionlint not found."; exit 1; }
	actionlint -config-file .gitea/actionlint.yaml .gitea/workflows/*.yml

workflow-dryrun:
	@command -v act_runner >/dev/null 2>&1 || { echo "act_runner not found."; exit 1; }
	@echo "Dry-running all workflows..."
	act_runner exec --dryrun -W .gitea/workflows/ 2>&1 | grep -E 'DRYRUN|ERROR|FAIL|Job'

workflow-check: workflow-lint workflow-dryrun
	@echo "Workflow checks passed."

test-unit:
	$(BIN)/pytest tests/unit/ -v --no-cov

pytest-cov:
	$(BIN)/pytest tests/ -v --cov=src/devx --cov-report=term-missing --cov-fail-under=100

test: pytest-cov

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