SHELL := /bin/bash
.PHONY: proto build run clean tools test deps fix-package-name

# Generate Go code from proto files
proto:
	protoc --go_out=. --go_opt=paths=source_relative \
		--go-grpc_out=. --go-grpc_opt=paths=source_relative \
		proto/remote_sc.proto

# Build the smart contract binary
build:
	go build -o smart-contract .

# Run the smart contract
run:
	go run . -config config.yaml

# Clean build artifacts
clean:
	rm -f smart-contract

# Install required tools
tools:
	go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
	go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

# Run tests
test:
	go test -v ./...

# Download dependencies
deps:
	go mod download
	go mod tidy

# Fix package name based on current working directory
fix-package-name:
	@NEW_MODULE=$$(go list -m 2>/dev/null || echo "$$(basename $$(dirname $$(pwd)))/$$(basename $$(pwd))"); \
	if [ -z "$$NEW_MODULE" ]; then \
		echo "Could not determine module name. Please run 'go mod init <module-name>' first."; \
		exit 1; \
	fi; \
	echo "Updating package name to: $$NEW_MODULE"; \
	find . -type f -name "*.go" ! -path "./proto/*" -exec sed -i 's|github.com/your-org/smart-contract|'$$NEW_MODULE'|g' {} +; \
	echo "Done. Run 'make proto' to generate proto files, then 'make build' to build."
