.PHONY: help install install-dev test test-cov lint typecheck quality format clean build upload docs run-example proto-sync

PROTO_REPO ?= https://github.com/flipperdevices/bsb-protobuf
PROTO_DIR ?= .cache/bsb-protobuf
PROTO_OUT_DIR ?= src/busylib/state_stream_proto
PROTO_FILES = \
	state.proto \
	frame.proto \
	timer.proto \
	input.proto \
	error.proto \
	state/audio.proto \
	state/ble.proto \
	state/brightness.proto \
	state/device_name.proto \
	state/matter.proto \
	state/power.proto \
	state/timezone.proto \
	state/update.proto \
	state/wifi.proto \
	util/json.proto

ifneq (,$(filter run-example,$(firstword $(MAKECMDGOALS))))
RUN_EXAMPLE_GOALS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
$(eval $(RUN_EXAMPLE_GOALS):;@:)
endif

# Default target
help:
	@echo "Available commands:"
	@echo "  install     - Install the package"
	@echo "  install-dev - Install in development mode with dev dependencies"
	@echo "  test        - Run tests"
	@echo "  test-cov    - Run tests with coverage"
	@echo "  lint        - Run pre-commit style lint checks (ruff + pyproject-fmt --check)"
	@echo "  typecheck   - Run pyright for src/busylib and tests"
	@echo "  quality     - Run full local quality gates (lint + typecheck + test)"
	@echo "  format      - Format code with ruff"
	@echo "  clean       - Clean build artifacts"
	@echo "  build       - Build package"
	@echo "  upload      - Upload to PyPI"
	@echo "  docs        - Generate documentation"
	@echo "  proto-sync  - Pull proto schema from flipperdevices/bsb-protobuf and regenerate state stream Python files"
	@echo "  run-example - Run example main module via uv (usage: make run-example <name> [args...])"

# Install package
install:
	pip install .

# Install in development mode
install-dev:
	pip install --editable .[dev]

# Run tests
test:
	uv run pytest -q

# Run tests with coverage
test-cov:
	uv run pytest -q --cov=src/busylib --cov-report=term --cov-report=html

# Run linting
lint:
	uv run ruff check src/busylib tests
	uv run ruff format --check src/busylib tests
	uv run pyproject-fmt --check pyproject.toml

# Run static typing checks
typecheck:
	uv run pyright src/busylib tests

# Run full quality gates
quality: lint typecheck test

# Format code
format:
	uv run ruff format .

# Clean build artifacts
clean:
	rm -rf build/
	rm -rf dist/
	rm -rf *.egg-info/
	rm -rf .pytest_cache/
	rm -rf .ruff_cache/
	rm -rf htmlcov/
	find . -type d -name __pycache__ -exec rm -rf {} +
	find . -type f -name "*.pyc" -delete

# Build package (requires build)
build: clean
	uv run python -m build

# Upload to PyPI (requires twine)
upload: build
	uv run twine upload dist/*

# Generate documentation (placeholder)
docs:
	@echo "Documentation generation not implemented yet"

# Regenerate protobuf models for status websocket stream support.
# By default this target keeps a local checkout in .cache/bsb-protobuf.
proto-sync:
	@test -n "$(PROTO_DIR)" || (echo "PROTO_DIR is empty" && exit 1)
	@test -n "$(PROTO_REPO)" || (echo "PROTO_REPO is empty" && exit 1)
	@if [ ! -d "$(PROTO_DIR)/.git" ]; then \
		echo "Cloning $(PROTO_REPO) into $(PROTO_DIR)"; \
		mkdir -p "$(dir $(PROTO_DIR))"; \
		git clone "$(PROTO_REPO)" "$(PROTO_DIR)"; \
	else \
		echo "Updating $(PROTO_DIR)"; \
		git -C "$(PROTO_DIR)" fetch --all --tags --prune; \
		git -C "$(PROTO_DIR)" pull --ff-only; \
	fi
	@command -v uv >/dev/null 2>&1 || (echo "uv not found in PATH" && exit 1)
	@mkdir -p "$(PROTO_OUT_DIR)"
	uv run python -m grpc_tools.protoc -I "$(PROTO_DIR)" --python_out="$(PROTO_OUT_DIR)" $(addprefix $(PROTO_DIR)/,$(PROTO_FILES))

# Run example by directory name via uv.
# Usage: make run-example remote -- --flag value
run-example:
	@EXAMPLE="$(word 1,$(RUN_EXAMPLE_GOALS))"; \
	ARGS="$(wordlist 2,$(words $(RUN_EXAMPLE_GOALS)),$(RUN_EXAMPLE_GOALS))"; \
	if [ -z "$$EXAMPLE" ]; then \
		echo "Usage: make run-example <name> [args...]"; \
		echo "Example: make run-example remote -- --help"; \
		exit 1; \
	fi; \
	uv run python -m "examples.$$EXAMPLE.main" $$ARGS
