SYSTEM_PYTHON ?= python3
VENV ?= .venv
PYTHON := $(VENV)/bin/python

.PHONY: help venv install install-dev test lint isort coverage build check-package publish clean distclean

help:
	@echo "Targets disponibles:"
	@echo "  venv          Crea el entorno virtual $(VENV)"
	@echo "  install       Instala soapfish en modo editable (en $(VENV))"
	@echo "  install-dev   Instala soapfish + dependencias de tests/lint"
	@echo "  test          Corre la suite de pruebas (pytest)"
	@echo "  coverage      Corre pytest con reporte de cobertura"
	@echo "  lint          Corre flake8 sobre el proyecto"
	@echo "  isort         Verifica el orden de imports (isort --check)"
	@echo "  build         Genera sdist y wheel en dist/"
	@echo "  check-package Valida los artefactos generados en dist/ (twine check)"
	@echo "  publish       Sube dist/ a PyPI (twine upload, requiere build)"
	@echo "  clean         Elimina artefactos de build, cache y __pycache__"
	@echo "  distclean     clean + elimina el entorno virtual $(VENV)"

$(PYTHON):
	$(SYSTEM_PYTHON) -m venv $(VENV)
	$(PYTHON) -m pip install --quiet --upgrade pip

venv: $(PYTHON)

install: venv
	$(PYTHON) -m pip install -e .

install-dev: install
	$(PYTHON) -m pip install -r tests/requirements.txt

test: install-dev
	$(PYTHON) -m pytest

coverage: install-dev
	$(PYTHON) -m pytest --cov=soapfish

lint: install-dev
	$(PYTHON) -m flake8 soapfish tests

isort: install-dev
	$(PYTHON) -m isort --check-only --diff soapfish tests

build: venv clean
	$(PYTHON) -m pip install --quiet --upgrade build
	$(PYTHON) -m build

check-package: build
	$(PYTHON) -m pip install --quiet --upgrade twine
	$(PYTHON) -m twine check dist/*

publish: check-package
	$(PYTHON) -m twine upload dist/*

clean:
	rm -rf build dist *.egg-info .coverage .pytest_cache
	find . -name '__pycache__' -o -name '*.pyc' | xargs rm -rf

distclean: clean
	rm -rf $(VENV)
