.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 install-hooks activate-scripts

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

all: setup

# Full setup for local development
setup: $(VENV)/bin/activate .env activate-scripts install-tools
	@$(BIN)/pip install -e '.[dev]' 2>/dev/null; \
	export PATH="$(HOME)/.local/bin:$$PATH"; \
	$(BIN)/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; \
	$(BIN)/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"; \
	$(BIN)/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; \
	$(BIN)/python -m devx.tools.install_tools --tool git-cliff --tool tea; \
	export PATH="$(HOME)/.local/bin:$$PATH"; \
	$(BIN)/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

activate-scripts: $(VENV)/bin/activate
	@test -f activate.sh || (echo '#!/usr/bin/env bash' > activate.sh && echo 'source "$$(cd "$$(dirname "$${BASH_SOURCE[0]}")" && pwd)/.venv/bin/activate"' >> activate.sh && chmod +x activate.sh)
	@test -f activate.fish || (echo '#!/usr/bin/env fish' > activate.fish && echo 'set -l script_dir (dirname (status --current-filename))' >> activate.fish && echo 'source "$$script_dir/.venv/bin/activate.fish"' >> activate.fish && chmod +x activate.fish)
	@test -f activate.zsh || (echo '#!/usr/bin/env zsh' > activate.zsh && echo '0="$${ZERO:-$${0:#$$ZSH_ARGZERO}}"' >> activate.zsh && echo '0="$${$${(M)0:#/*}:-$$PWD/$$0}"' >> activate.zsh && echo 'source "$${0:A:h}/.venv/bin/activate"' >> activate.zsh && chmod +x activate.zsh)

install-hooks:
	@cp hooks/pre-commit .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit
	@cp hooks/pre-push .git/hooks/pre-push && chmod +x .git/hooks/pre-push
	@echo "Git hooks installed."

install-tools: $(VENV)/bin/activate
	@$(BIN)/pip install -e '.' 2>/dev/null; \
	$(BIN)/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/
