#!/bin/bash

# Age Verification ZK Proof Demo using the ZKproof MCP server
# This script demonstrates how to use the ZKproof MCP server to build a circuit,
# perform trusted setup, generate a proof, and verify the proof for age verification.

set -e

echo "=== Age Verification ZK Proof Demo ==="

# Step 1: Build the circuit
echo -e "\n1. Building the circuit..."
cline-mcp-cli use-tool zkproof build_circuit '{"circomFile": "age-verification/age-verification.circom"}'

# Step 2: Perform trusted setup
echo -e "\n2. Performing trusted setup..."
cline-mcp-cli use-tool zkproof trusted_setup '{"r1csFile": "age-verification/target/age-verification.r1cs"}'

# Step 3: Generate a proof for the over-18 case
echo -e "\n3. Generating proof for a person over 18..."
cline-mcp-cli use-tool zkproof generate_proof '{
  "zkeyPath": "age-verification/target/age-verification_0001.zkey",
  "wasmPath": "age-verification/target/age-verification_js/age-verification.wasm",
  "inputJsonPath": "age-verification/input-over18.json"
}'

# Step 4: Verify the proof
echo -e "\n4. Verifying the proof..."
VERIFICATION_RESULT=$(cline-mcp-cli use-tool zkproof verify_proof '{
  "verificationKey": "age-verification/target/age-verification_verification_key.json",
  "publicOutput": "age-verification/target/public.json",
  "proofFile": "age-verification/target/proof.json"
}')

# Check the verification result
if [[ $VERIFICATION_RESULT == *"Verification succeeded"* ]]; then
  echo -e "\n✅ Proof verification succeeded!"
  
  # Read the public output to see if the person is over 18
  PUBLIC_OUTPUT=$(cat age-verification/target/public.json)
  echo -e "\nPublic output: $PUBLIC_OUTPUT"
  
  if [[ $PUBLIC_OUTPUT == *"1"* ]]; then
    echo -e "\n🎉 The person is verified to be at least 18 years old!"
    echo -e "\nImportant: The actual birth date remains private and was not revealed."
  else
    echo -e "\n❌ The person is NOT at least 18 years old."
  fi
else
  echo -e "\n❌ Proof verification failed."
fi

echo -e "\n=== Demo completed ==="

# Optional: Try with a person under 18
echo -e "\n\nWould you like to try with a person under 18? (y/n)"
read -r response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
  echo -e "\n=== Running demo with a person under 18 ==="
  
  # Generate a proof for the under-18 case
  echo -e "\n1. Generating proof for a person under 18..."
  cline-mcp-cli use-tool zkproof generate_proof '{
    "zkeyPath": "age-verification/target/age-verification_0001.zkey",
    "wasmPath": "age-verification/target/age-verification_js/age-verification.wasm",
    "inputJsonPath": "age-verification/input-under18.json"
  }'
  
  # Verify the proof
  echo -e "\n2. Verifying the proof..."
  VERIFICATION_RESULT=$(cline-mcp-cli use-tool zkproof verify_proof '{
    "verificationKey": "age-verification/target/age-verification_verification_key.json",
    "publicOutput": "age-verification/target/public.json",
    "proofFile": "age-verification/target/proof.json"
  }')
  
  # Check the verification result
  if [[ $VERIFICATION_RESULT == *"Verification succeeded"* ]]; then
    echo -e "\n✅ Proof verification succeeded!"
    
    # Read the public output to see if the person is over 18
    PUBLIC_OUTPUT=$(cat age-verification/target/public.json)
    echo -e "\nPublic output: $PUBLIC_OUTPUT"
    
    if [[ $PUBLIC_OUTPUT == *"1"* ]]; then
      echo -e "\n🎉 The person is verified to be at least 18 years old!"
    else
      echo -e "\n❌ The person is NOT at least 18 years old."
      echo -e "\nImportant: The actual birth date remains private and was not revealed."
    fi
  else
    echo -e "\n❌ Proof verification failed."
  fi
  
  echo -e "\n=== Demo completed ==="
fi
