.PHONY: help install install-dev test test-verbose test-cov clean build format lint type-check check publish publish-test

# 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%-15s\033[0m %s\n", $$1, $$2}'
	@echo ""
	@echo "Publishing (requires TWINE_PASSWORD or .pypirc; username __token__ for API token):"
	@echo "  make build              - Build package (python -m build)"
	@echo "  make publish-test       - Build and publish to TestPyPI"
	@echo "  make publish            - Build and publish to PyPI"

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=pyfortis --cov-report=html --cov-report=term

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

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

format: ## Format code with ruff (same paths and config as CI)
	@if [ -f "venv/bin/ruff" ]; then \
		venv/bin/ruff format src tests; \
	else \
		ruff format src tests; \
	fi

lint: ## Run linting checks (same as CI - run before pushing)
	@if [ -f "venv/bin/ruff" ]; then \
		venv/bin/ruff check src tests && venv/bin/ruff format --check src tests; \
	else \
		ruff check src tests && ruff format --check src tests; \
	fi

type-check: ## Run type checking with mypy
	@if $(PYTHON) -c "import mypy" 2>/dev/null; then \
		$(PYTHON_CMD) mypy src/; \
	elif command -v mypy > /dev/null 2>&1; then \
		mypy src/; \
	else \
		echo "Install mypy: pip install mypy"; exit 1; \
	fi

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

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

publish: clean build ## Build and upload to PyPI (requires TWINE_PASSWORD)
	@echo "⚠️  WARNING: This will publish to PyPI!"
	@echo "Press Ctrl+C to cancel, or Enter to continue..."
	@read dummy
	@if ! $(PYTHON) -c "import twine" 2>/dev/null; then $(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. Check https://pypi.org/project/pyfortis/"
