#!/usr/bin/env bash
set -euo pipefail

remote="${EVERY_CHANNEL_REALITY_SMOKE_REMOTE:-root@git.every.channel}"
remote_jsonl="${EVERY_CHANNEL_REALITY_SMOKE_JSONL:-/var/lib/every-channel/manifests/la-cbs/video0.m4s.jsonl}"
remote_identity_file="${EVERY_CHANNEL_REALITY_SMOKE_SSH_IDENTITY_FILE:-${HOME}/.ssh/id_ed25519}"
anvil_port="${EVERY_CHANNEL_REALITY_SMOKE_ANVIL_PORT:-8545}"
results_dir="${EVERY_CHANNEL_REALITY_SMOKE_RESULTS_DIR:-./test-results}"

anvil_pk0="0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
anvil_pk1="0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d"
anvil_pk2="0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a"

mkdir -p "${results_dir}"
anvil_log="${results_dir}/anvil-reality-smoke.log"
deploy_json="${results_dir}/observation-ledger-deploy.json"
result_json="${results_dir}/anvil-reality-smoke.json"

cleanup() {
  if [[ -n "${anvil_pid:-}" ]]; then
    kill "${anvil_pid}" >/dev/null 2>&1 || true
    wait "${anvil_pid}" >/dev/null 2>&1 || true
  fi
}
trap cleanup EXIT

anvil --port "${anvil_port}" >"${anvil_log}" 2>&1 &
anvil_pid=$!

for _ in $(seq 1 30); do
  if curl -fsS -X POST -H 'content-type: application/json' \
    --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' \
    "http://127.0.0.1:${anvil_port}" >/dev/null 2>&1; then
    break
  fi
  sleep 1
done

rpc_url="http://127.0.0.1:${anvil_port}"
owner_file="$(mktemp)"
printf '%s\n' "${anvil_pk0}" >"${owner_file}"

EVERY_CHANNEL_RPC_URL="${rpc_url}" \
EVERY_CHANNEL_PRIVATE_KEY_FILE="${owner_file}" \
EVERY_CHANNEL_OBSERVATION_DEPLOY_OUT="${deploy_json}" \
  ./scripts/op-stack/deploy-observation-ledger.sh >/dev/null

registry="$(jq -r '.registry' <"${deploy_json}")"
ledger="$(jq -r '.ledger' <"${deploy_json}")"

witness1="$(cast wallet address --private-key "${anvil_pk1}")"
witness2="$(cast wallet address --private-key "${anvil_pk2}")"

cast send "${registry}" "addWitness(address)" "${witness1}" --rpc-url "${rpc_url}" --private-key "${anvil_pk0}" >/dev/null
cast send "${registry}" "addWitness(address)" "${witness2}" --rpc-url "${rpc_url}" --private-key "${anvil_pk0}" >/dev/null

remote_jsonl_escaped="$(printf '%q' "${remote_jsonl}")"
entry="$(ssh \
  -i "${remote_identity_file}" \
  -o IdentityAgent=none \
  -o IdentitiesOnly=yes \
  -o BatchMode=yes \
  -o ConnectTimeout=10 \
  "${remote}" \
  "head -n 1 -- ${remote_jsonl_escaped}")"
if [[ -z "${entry}" ]]; then
  echo "no remote archive entry found" >&2
  exit 1
fi

broadcast_name="$(jq -r '.broadcast_name' <<<"${entry}")"
track_name="$(jq -r '.track_name' <<<"${entry}")"
group_sequence="$(jq -r '.group_sequence' <<<"${entry}")"
received_unix_ms="$(jq -r '.received_unix_ms' <<<"${entry}")"
blake3_hex="$(jq -r '.blake3' <<<"${entry}")"
locator_material="$(jq -c '{relay_url,broadcast_name,track_name,group_sequence,cas_path}' <<<"${entry}")"

stream_hash="$(cast keccak "${broadcast_name}:${track_name}")"
epoch_hash="$(cast keccak "${broadcast_name}:${track_name}:${group_sequence}")"
parent_hash="0x0000000000000000000000000000000000000000000000000000000000000000"
data_root="0x${blake3_hex}"
locator_hash="$(cast keccak "${locator_material}")"

observation_tuple="(${stream_hash},${epoch_hash},${parent_hash},${data_root},${locator_hash},${received_unix_ms},${group_sequence})"
cast send "${ledger}" \
  "proposeObservation((bytes32,bytes32,bytes32,bytes32,bytes32,uint64,uint64))" \
  "${observation_tuple}" \
  --rpc-url "${rpc_url}" \
  --private-key "${anvil_pk1}" >/dev/null

observation_hash="$(cast call "${ledger}" \
  "hashObservationHeader((bytes32,bytes32,bytes32,bytes32,bytes32,uint64,uint64))(bytes32)" \
  "${observation_tuple}" \
  --rpc-url "${rpc_url}")"
slot_hash="$(cast call "${ledger}" \
  "observationSlot(bytes32,bytes32)(bytes32)" \
  "${stream_hash}" "${epoch_hash}" \
  --rpc-url "${rpc_url}")"

cast send "${ledger}" "attestObservation(bytes32)" "${observation_hash}" \
  --rpc-url "${rpc_url}" \
  --private-key "${anvil_pk2}" >/dev/null

finalized="$(cast call "${ledger}" "finalizedObservationBySlot(bytes32)(bytes32)" "${slot_hash}" --rpc-url "${rpc_url}")"

cat >"${result_json}" <<EOF
{
  "remote": "${remote}",
  "remote_jsonl": "${remote_jsonl}",
  "broadcast_name": "${broadcast_name}",
  "track_name": "${track_name}",
  "group_sequence": ${group_sequence},
  "stream_hash": "${stream_hash}",
  "epoch_hash": "${epoch_hash}",
  "observation_hash": "${observation_hash}",
  "finalized_hash": "${finalized}",
  "registry": "${registry}",
  "ledger": "${ledger}"
}
EOF

if [[ "${finalized}" != "${observation_hash}" ]]; then
  echo "observation did not finalize" >&2
  exit 1
fi

printf '%s\n' "${result_json}"
