# Requires curl and jq. Keep ODDS_API_KEY in your shell environment.
: "${ODDS_API_KEY:?Set ODDS_API_KEY first}"
BASE="https://api.odds-api.net/v1"

# Discover an upcoming Australian horse race.
curl -fsS -G "$BASE/racing/events" \
  -H "X-API-Key: $ODDS_API_KEY" \
  --data-urlencode "race_type=horse-racing" \
  --data-urlencode "race_country=AU" \
  --data-urlencode "status=fetching" \
  --data-urlencode "start_from=$(date +%s)" \
  --data-urlencode "limit=50" | jq '.items[] | {event_id, race_venue, race_number}'

# Set RACE_EVENT_ID to the race you want to compare.
: "${RACE_EVENT_ID:?Set RACE_EVENT_ID from the race list}"
RACE_PATH=$(printf '%s' "$RACE_EVENT_ID" | jq -sRr @uri)
SNAPSHOT=$(curl -fsS -G "$BASE/racing/events/$RACE_PATH/odds" \
  -H "X-API-Key: $ODDS_API_KEY" \
  --data-urlencode "include_raw_payload=false" \
  --data-urlencode "include_source=true" \
  --data-urlencode "include_unavailable=true")

# Compare the same WIN selection with a positive Betfair lay quote.
# gap_pct is the price difference before exchange commission.
printf '%s' "$SNAPSHOT" | jq '
  def active:
    ((.runner_status // .status // "") | ascii_downcase | gsub("[- ]"; "_")) as $status |
    (["closed","suspended","scratched","removed","inactive","unavailable","off",
      "settled","finished","cancelled","abandoned","withdrawn","non_runner"] |
      index($status) | not) and
    ((.is_scratched | tostring | ascii_downcase) as $flag |
      ["true","1","yes"] | index($flag) | not);
  def fresh:
    . as $book | (.updated_at_ts | tonumber? // 0) as $stamp |
    (if $stamp < 1e12 then $stamp * 1000 else $stamp end) as $ms |
    ($book | active) and $ms > 0 and
    (now * 1000 - $ms >= -5000) and (now * 1000 - $ms <= 30000);
  [.items[] | select(.bookmaker_name == "betfair" and fresh and
    (.exchange_markets.win | active))] as $exchanges |
  select(($exchanges | length) == 1) | $exchanges[0] as $exchange |
  [$exchange.runners[] | select(active)] as $lays |
  .items[] | select((.bookmaker_name == "sportsbet" or .bookmaker_name == "ladbrokes") and fresh) as $book |
  $book.runners[] | select(active and .selection_id != null) as $runner |
  [$lays[] | select((.selection_id | tostring) == ($runner.selection_id | tostring))] as $matches |
  select(($matches | length) == 1) | $matches[0].exchange.win as $win |
  select($win.lay > 1 and $win.lay_size > 0 and $runner.win_odds > 1) |
  {runner: $runner.runner_name, bookmaker: $book.bookmaker_name,
   back: $runner.win_odds, lay: $win.lay, lay_size: $win.lay_size,
   lay_currency: $exchange.size_currency,
   gap_pct: (($runner.win_odds / $win.lay - 1) * 100)}'

# Continue from this snapshot. Node.js/Python maintain and validate live state.
RESUME=$(printf '%s' "$SNAPSHOT" | jq -r '.resume')
curl -fNsS -G "$BASE/racing/events/$RACE_PATH/odds/stream" \
  -H "X-API-Key: $ODDS_API_KEY" \
  --data-urlencode "since=$RESUME" \
  --data-urlencode "catchup=true" \
  --data-urlencode "include_raw_payload=false" \
  --data-urlencode "include_source=true" \
  --data-urlencode "include_unavailable=true"
