#!/bin/bash

CHAINID=11155111
DEPLOY_SCRIPT=DeploySepolia

if [ -f ".env-secret" ]; then
  echo "Reading environment from .env-secret"
  set -a
  source .env-secret
  set +a
fi

if [ -z "$SEPOLIA_RPC_URL" ]; then
  echo "Usage: SEPOLIA_RPC_URL environment variable must be set"
  exit 1
fi
RPC=${SEPOLIA_RPC_URL}

if [ -z "$PRIVATE_KEY" ]; then
  echo "Usage: PRIVATE_KEY environment variable must be set"
  exit 1
fi

if [ -z "$ETHERSCAN_API_KEY" ]; then
  echo "Usage: ETHERSCAN_API_KEY environment variable must be set"
  exit 1
fi

if [ "$1" == "broadcast" ]; then
  # BROADCAST=(--broadcast --verify --etherscan-api-key "$ETHERSCAN_API_KEY")
  # use --slow to avoid triggering rate limits
  BROADCAST=(--slow --broadcast --verify --etherscan-api-key "$ETHERSCAN_API_KEY")
else
  BROADCAST=()
fi


if [ "$1" != "record" ]; then
  forge clean
  forge script --fork-url "$RPC" --private-key "$PRIVATE_KEY" "${BROADCAST[@]}" script/DeploySepolia.sol || exit 1
fi

# Update the liqp-deployments.json file if the contracts were broadcast or record was requested

if [ "$1" == "broadcast" ] || [ "$1" == "record" ]; then
  METADATA_FILE=deployment/liqp-deployments.json
else
  METADATA_FILE=./liqp-deployments.json
fi

address() {
  if [ -z "$1" ]; then
    echo ""
  else
    cast --to-checksum-address "$1"
  fi
}

contract() {
  address "$(jq -r ".transactions[] | select(.contractName == \"$1\" and .transactionType == \"CREATE\") | .contractAddress" broadcast/${DEPLOY_SCRIPT}.sol/${CHAINID}/run-latest.json)"
}

token() {
  address "$(jq -r ".transactions[] | select(.contractName == \"MockERC20\" and .transactionType == \"CREATE\" and (.arguments | contains([\"$1\"]))) | .contractAddress" broadcast/${DEPLOY_SCRIPT}.sol/${CHAINID}/run-latest.json)"
}

PARTY_PLANNER=$(contract PartyPlanner)
PARTY_POOL_VIEWER=$(contract PartyPoolViewer)
PARTY_POOL_MINT_IMPL=$(contract PartyPoolMintImpl)
PARTY_POOL_SWAP_IMPL=$(contract PartyPoolSwapImpl)
PARTY_POOL_DEPLOYER=$(contract PartyPoolDeployer)
PARTY_POOL_BP_DEPLOYER=$(contract PartyPoolBalancedPairDeployer)

USXD=$(token USXD)
FUSD=$(token FUSD)
DIVE=$(token DIVE)
BUTC=$(token BUTC)
WTETH=$(token WTETH)

OUT=deployment/$CHAINID/v1
mkdir -p $OUT
cp -r out $OUT/

# Build and insert the JSON object directly in jq
jq -n \
  --arg chainId "$CHAINID" \
  --arg partyPlanner "$PARTY_PLANNER" \
  --arg partyPoolViewer "$PARTY_POOL_VIEWER" \
  --arg partyPoolMintImpl "$PARTY_POOL_MINT_IMPL" \
  --arg partyPoolSwapImpl "$PARTY_POOL_SWAP_IMPL" \
  --arg partyPoolDeployer "$PARTY_POOL_DEPLOYER" \
  --arg partyPoolBPDeployer "$PARTY_POOL_BP_DEPLOYER" \
  --arg usxd "$USXD" \
  --arg fusd "$FUSD" \
  --arg dive "$DIVE" \
  --arg butc "$BUTC" \
  --arg wteth "$WTETH" \
  '
  {
    v1: {
      PartyPlanner: $partyPlanner,
      PartyPoolViewer: $partyPoolViewer,
      PartyPoolMintImpl: $partyPoolMintImpl,
      PartyPoolSwapImpl: $partyPoolSwapImpl,
      PartyPoolDeployer: $partyPoolDeployer,
      PartyPoolBalancedPairDeployer: $partyPoolBPDeployer,
      USXD: $usxd,
      FUSD: $fusd,
      DIVE: $dive,
      BUTC: $butc,
      WTETH: $wteth
    }
  } as $entry |
  (try (input | . + {($chainId): $entry}) catch {($chainId): $entry})
  ' "$METADATA_FILE" 2>/dev/null > "${METADATA_FILE}.tmp" && mv "${METADATA_FILE}.tmp" "$METADATA_FILE" || \
  jq -n \
    --arg chainId "$CHAINID" \
    --arg partyPlanner "$PARTY_PLANNER" \
    --arg partyPoolViewer "$PARTY_POOL_VIEWER" \
    --arg partyPoolMintImpl "$PARTY_POOL_MINT_IMPL" \
    --arg partyPoolSwapImpl "$PARTY_POOL_SWAP_IMPL" \
    --arg partyPoolDeployer "$PARTY_POOL_DEPLOYER" \
    --arg partyPoolBPDeployer "$PARTY_POOL_BP_DEPLOYER" \
    --arg usxd "$USXD" \
    --arg fusd "$FUSD" \
    --arg dive "$DIVE" \
    --arg butc "$BUTC" \
    --arg wteth "$WTETH" \
    '{($chainId): {
      v1: {
        PartyPlanner: $partyPlanner,
        PartyPoolViewer: $partyPoolViewer,
        PartyPoolMintImpl: $partyPoolMintImpl,
        PartyPoolSwapImpl: $partyPoolSwapImpl,
        PartyPoolDeployer: $partyPoolDeployer,
        PartyPoolBalancedPairDeployer: $partyPoolBPDeployer,
        USXD: $usxd,
        FUSD: $fusd,
        DIVE: $dive,
        BUTC: $butc,
        WTETH: $wteth
      }
    }}' > "$METADATA_FILE"

echo "Wrote $METADATA_FILE"
