#!/usr/bin/env bash
# Warms the R2 image bucket by requesting every known team / competition /
# country image once through the image-proxy Worker, which mirrors each one
# into R2 (see workers/image-proxy).
#
# Run on the production server (needs docker access to iddaai-postgres):
#   ./warm-image-cache.sh https://files.example.com
set -euo pipefail

BASE_URL="${1:?Usage: $0 <image-base-url>}"
BASE_URL="${BASE_URL%/}"
PSQL=(docker exec iddaai-postgres psql -U iddaai_user -d iddaai_db -At -c)

{
  "${PSQL[@]}" "SELECT 'teams/' || id FROM teams"
  "${PSQL[@]}" "SELECT 'competitions/' || id FROM leagues"
  "${PSQL[@]}" "SELECT 'areas/' || id FROM countries"
} | xargs -P 8 -I{} curl -sS -o /dev/null -w "%{http_code}\n" "$BASE_URL/{}" \
  | sort | uniq -c \
  | awk '{printf "HTTP %s: %s istek\n", $2, $1}'
