.PHONY: help install install-dev test clean build format lint type-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:"
	@echo "  make build              - Build package (python -m build)"
	@echo "  make publish-test       - Build and publish to TestPyPI (local, requires token)"
	@echo "  make publish            - Build and publish to PyPI (local, requires token)"
	@echo ""
	@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=pycharter --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/
	rm -rf dist/
	rm -rf *.egg-info
	rm -rf src/*.egg-info
	rm -rf src/pycharter.egg-info
	rm -rf .pytest_cache
	rm -rf .mypy_cache
	rm -rf htmlcov/
	rm -rf .coverage
	find . -type d -name __pycache__ -exec rm -r {} +
	find . -type f -name "*.pyc" -delete

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

format: ## Format code with black and isort (same paths and config as CI)
	@if [ -f "venv/bin/black" ]; then \
		venv/bin/black --config pyproject.toml src/pycharter tests && venv/bin/isort --settings-path pyproject.toml src/pycharter tests; \
	else \
		black --config pyproject.toml src/pycharter tests && isort --settings-path pyproject.toml src/pycharter tests; \
	fi

lint: ## Run linting checks (same as CI - run before pushing)
	@if [ -f "venv/bin/black" ]; then \
		venv/bin/black --check --config pyproject.toml src/pycharter tests && venv/bin/isort --check --settings-path pyproject.toml src/pycharter tests; \
	else \
		black --check --config pyproject.toml src/pycharter tests && isort --check --settings-path pyproject.toml src/pycharter tests; \
	fi

type-check: ## Run type checking with mypy
	@if [ -f "venv/bin/mypy" ]; then \
		venv/bin/mypy pycharter; \
	else \
		mypy pycharter; \
	fi

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

publish-test: clean build ## Build and upload to TestPyPI (for testing, requires TESTPYPI_API_TOKEN)
	@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/ pycharter"

publish: clean build ## Build and upload to PyPI (production, requires PYPI_API_TOKEN)
	@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"

