
ETH_RPC_URL ?= https://ethereum-sepolia-rpc.publicnode.com
CARTESI_RISC0_CLI := $(abspath ../rust/target/debug/cartesi-risc0-cli)
FIXTURES_DIR := $(abspath ../test/fixtures)

.PHONY: dep build test image-id clean help

help:
	@echo "Groth16 (seal) Integration Test"
	@echo ""
	@echo "  make test                            Run integration tests (Sepolia fork)"
	@echo "  make dep                             Install Foundry dependencies"
	@echo "  make image-id                        Regenerate src/ImageID.sol"
	@echo "  make clean                           Remove build artifacts"

dep:
	@if [ ! -d lib/risc0-ethereum ]; then \
		forge install risc0/risc0-ethereum@v3.0.1 --no-git; \
	else \
		echo "Dependencies already installed (lib/risc0-ethereum exists)"; \
	fi

src/ImageID.sol:
	@IMAGE_ID=$$($(CARTESI_RISC0_CLI) image-id) && \
	{ echo '// This file is auto-generated by `make image-id` and should not be modified.'; \
	  echo '// SPDX-License-Identifier: LGPL-3.0-or-later'; \
	  echo 'pragma solidity ^0.8.20;'; \
	  echo ''; \
	  echo 'library ImageID {'; \
	  echo "    bytes32 public constant CARTESI_STEP_VERIFIER_ID = bytes32(0x$$IMAGE_ID);"; \
	  echo '}'; \
	} > $@ && \
	echo "Generated $@ (Image ID: $$IMAGE_ID)"

image-id:
	@rm -f src/ImageID.sol
	@$(MAKE) src/ImageID.sol

build: dep
	forge build

test: dep src/ImageID.sol
	@test -f "$(FIXTURES_DIR)/seal.bin" || { echo "Error: fixtures not found. Run 'make -C risc0 test' first."; exit 1; }
	forge test --fork-url $(ETH_RPC_URL) -vvv

clean:
	-@forge clean 2>/dev/null || true
	rm -rf cache out
	rm -f src/ImageID.sol
