.PHONY: proto run clean setup venv test deps check

# Generate Python code from proto files (for the gRPC infrastructure)
proto:
	python -m grpc_tools.protoc \
		-I./proto \
		--python_out=. \
		--grpc_python_out=. \
		proto/remote_sc.proto

# Create virtual environment and install dependencies
setup: venv
	. venv/bin/activate && pip install -r requirements.txt
	@echo ""
	@echo "Setup complete. Activate the virtual environment with:"
	@echo "  source venv/bin/activate"

# Create virtual environment
venv:
	python3 -m venv venv

# Run the smart contract
run:
	python main.py --config config.yaml

# Clean generated files
clean:
	rm -f *_pb2.py *_pb2_grpc.py
	rm -rf __pycache__
	rm -rf venv

# Run tests
test:
	bash -n process.sh
	@echo "Syntax check passed"
	@echo "Running process.sh with sample transaction..."
	echo '{"version":"1","header":{"tag":"test","dc_id":"test-dc","txn_id":"test-txn-123","block_id":"1","txn_type":"test","timestamp":"2024-01-01T00:00:00Z","invoker":"test-user"},"payload":{"action":"test","value":42}}' | \
		bash process.sh "$$(cat /dev/stdin)" 2>/dev/null && echo "Test passed" || echo "Test failed"

# Install dependencies (without venv)
deps:
	pip install -r requirements.txt

# Check that required tools are available
check:
	@command -v python3 >/dev/null 2>&1 || { echo "python3 is required but not installed"; exit 1; }
	@command -v bash >/dev/null 2>&1 || { echo "bash is required but not installed"; exit 1; }
	@command -v jq >/dev/null 2>&1 || { echo "jq is required but not installed"; exit 1; }
	@echo "All required tools are available"

# Format process.sh with shfmt (if available)
format:
	@command -v shfmt >/dev/null 2>&1 && shfmt -w process.sh || echo "shfmt not installed, skipping format"
