#!/usr/bin/env bash
# Fetches city/destination listings from incredibleindia.gov.in AEM gmap.json endpoint.
# Loops over all states, merges into a single JSON array, and saves to data/.
#
# Endpoint: POST /en/destinations/{state}/jcr:content.gmap.json?path=...&type=citypage&option1=
# Output:   data/incredibleindia_cities.json
#
# Usage:
#   ./incredibleindia-cities.sh
#   ./incredibleindia-cities.sh --refresh

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
DATA_DIR="$SCRIPT_DIR/../data"
OUTPUT="$DATA_DIR/incredibleindia_cities.json"
BASE_URL="https://www.incredibleindia.gov.in"

REFRESH=0
while [[ $# -gt 0 ]]; do
  case "$1" in
    --refresh) REFRESH=1; shift ;;
    *) echo "Unknown arg: $1"; exit 1 ;;
  esac
done

if [[ -f "$OUTPUT" && "$REFRESH" -eq 0 ]]; then
  echo "Already exists: $OUTPUT (use --refresh to re-fetch)"
  jq '. | length' "$OUTPUT"
  exit 0
fi

STATES=(
  "andaman-and-nicobar-islands"
  "andhra-pradesh"
  "arunachal-pradesh"
  "assam"
  "bihar"
  "chandigarh"
  "chhattisgarh"
  "dadra-and-nagar-haveli-and-daman-and-diu"
  "delhi"
  "goa"
  "gujarat"
  "haryana"
  "himachal-pradesh"
  "jammu-and-kashmir"
  "jharkhand"
  "karnataka"
  "kerala"
  "ladakh"
  "lakshadweep"
  "madhya-pradesh"
  "maharashtra"
  "manipur"
  "meghalaya"
  "mizoram"
  "nagaland"
  "odisha"
  "puducherry"
  "punjab"
  "rajasthan"
  "sikkim"
  "tamil-nadu"
  "telangana"
  "tripura"
  "uttar-pradesh"
  "uttarakhand"
  "west-bengal"
)

echo "=== Incredible India — cities pipeline ==="
echo "States: ${#STATES[@]}"
echo "Output: $OUTPUT"
echo ""

TEMP_ALL=$(mktemp)
echo "[]" > "$TEMP_ALL"

for state in "${STATES[@]}"; do
  URL="${BASE_URL}/en/destinations/${state}/jcr:content.gmap.json?path=/content/incredible-india/en/destinations/${state}&type=citypage&option1="
  TEMP_RESP=$(mktemp)

  HTTP_STATUS=$(curl -s -o "$TEMP_RESP" -w "%{http_code}" -X POST -d '' "$URL")

  if [[ "$HTTP_STATUS" != "200" ]]; then
    echo "  SKIP  $state (HTTP $HTTP_STATUS)"
    rm "$TEMP_RESP"
    sleep 0.3
    continue
  fi

  COUNT=$(jq '.mapAttribute | length' "$TEMP_RESP" 2>/dev/null || echo 0)

  # Merge into accumulator
  TEMP_MERGED=$(mktemp)
  jq -s '.[0] + .[1].mapAttribute' "$TEMP_ALL" "$TEMP_RESP" > "$TEMP_MERGED"
  mv "$TEMP_MERGED" "$TEMP_ALL"

  echo "  OK    $state ($COUNT cities)"
  rm "$TEMP_RESP"
  sleep 0.3
done

mv "$TEMP_ALL" "$OUTPUT"

TOTAL=$(jq '. | length' "$OUTPUT")
echo ""
echo "=== Done: $TOTAL cities saved to $OUTPUT ==="
