.PHONY: help install install-dev test test-verbose test-cov test-fast test-unit test-integration clean build format lint type-check check publish-test publish docs docs-serve

# Detect if venv exists and set PYTHON/PIP accordingly
ifeq ($(wildcard venv/bin/python),)
    PYTHON := python3
    PIP := pip3
    PYTHON_CMD := python3 -m
else
    PYTHON := venv/bin/python
    PIP := venv/bin/pip
    PYTHON_CMD := venv/bin/python -m
endif

help: ## Show this help message
	@echo "Available commands:"
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-18s\033[0m %s\n", $$1, $$2}'
	@echo ""
	@echo "Publishing:"
	@echo "  For production: Create a GitHub Release (automatically publishes via Trusted Publishing)"

install: ## Install the package in development mode
	python3 -m venv venv
	. venv/bin/activate && pip install --upgrade pip && pip install -e .

install-dev: ## Install package + all development dependencies
	python3 -m venv venv
	. venv/bin/activate && pip install --upgrade pip && pip install -e ".[dev]"
	@echo "To activate: source venv/bin/activate"

test: ## Run tests
	$(PYTHON_CMD) pytest

test-verbose: ## Run tests with verbose output
	$(PYTHON_CMD) pytest -v

test-cov: ## Run tests with coverage
	$(PYTHON_CMD) pytest --cov=pystator --cov-report=html --cov-report=term

test-fast: ## Run fast tests only (exclude slow markers)
	$(PYTHON_CMD) pytest -m "not slow"

test-unit: ## Run unit tests only
	$(PYTHON_CMD) pytest -m "unit"

test-integration: ## Run integration tests only
	$(PYTHON_CMD) pytest -m "integration"

clean: ## Clean build artifacts
	rm -rf build/ dist/ *.egg-info src/*.egg-info src/pystator.egg-info
	rm -rf .pytest_cache .mypy_cache .ruff_cache htmlcov/ .coverage
	find . -type d -name __pycache__ -exec rm -r {} + 2>/dev/null || true
	find . -type f -name "*.pyc" -delete 2>/dev/null || true

build: ## Build distribution packages (UI built automatically via setup.py)
	@if ! $(PYTHON) -c "import build" 2>/dev/null; then \
		echo "Installing build..."; \
		$(PIP) install build; \
	fi
	$(PYTHON_CMD) build

format: ## Format code with ruff
	$(PYTHON_CMD) ruff format src/pystator tests
	$(PYTHON_CMD) ruff check --fix src/pystator tests

lint: ## Run linting checks (same as CI)
	$(PYTHON_CMD) ruff format --check src/pystator tests
	$(PYTHON_CMD) ruff check src/pystator tests

type-check: ## Run type checking with mypy
	$(PYTHON_CMD) mypy src/pystator

check: format lint type-check test ## Run all checks (format, lint, type-check, test)

docs: ## Build documentation
	$(PYTHON_CMD) mkdocs build

docs-serve: ## Serve documentation locally
	$(PYTHON_CMD) mkdocs serve

publish-test: clean build ## Build and upload to TestPyPI (requires TWINE_PASSWORD)
	@echo "Uploading to TestPyPI..."
	@if ! $(PYTHON) -c "import twine" 2>/dev/null; then \
		echo "Installing twine..."; \
		$(PIP) install twine; \
	fi
	@if [ -z "$$TWINE_PASSWORD" ]; then \
		echo "Warning: TWINE_PASSWORD not set. Set it with:"; \
		echo "   export TWINE_PASSWORD=your-testpypi-token"; \
		exit 1; \
	fi
	$(PYTHON_CMD) twine upload --repository testpypi dist/*
	@echo "Published to TestPyPI"
	@echo "Test install: pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ pystator"

publish: clean build ## Build and upload to PyPI (use GitHub Releases for production)
	@echo "WARNING: This will publish to PyPI!"
	@echo "For production releases, use GitHub Releases instead (automatic via Trusted Publishing)"
	@echo "Press Ctrl+C to cancel, or Enter to continue..."
	@read dummy
	@if ! $(PYTHON) -c "import twine" 2>/dev/null; then \
		echo "Installing twine..."; \
		$(PIP) install twine; \
	fi
	@if [ -z "$$TWINE_PASSWORD" ]; then \
		echo "Warning: TWINE_PASSWORD not set. Set it with:"; \
		echo "   export TWINE_PASSWORD=your-pypi-token"; \
		exit 1; \
	fi
	$(PYTHON_CMD) twine upload dist/*
	@echo "Published to PyPI"
