#!/usr/bin/env bash

# === Detect arch and OS

RAW_ARCH="$(uname -m)"
# See https://stackoverflow.com/a/45125525
if  [[ "$RAW_ARCH" == aarch64* || "$RAW_ARCH" == armv8* || "$RAW_ARCH" == arm64* ]]; then
  ARCH=arm64
elif [[ "$RAW_ARCH" == x86_64* ]]; then
  ARCH=x64
elif [[ "$RAW_ARCH" == i*86 ]]; then
  ARCH=x86
else
  echo "ERROR: Detected processor architecture is not recognized or supported: $RAW_ARCH"
  exit 1
fi

if [[ "$OSTYPE" == linux* ]]; then
  OS=linux
elif [[ "$OSTYPE" == darwin* ]]; then
  OS=osx
else
  # Windows not supported yet.
  echo "ERROR: Detected OS is not recognized or supported: $OSTYPE"
  exit 1
fi

PACKAGE_NAME="$OS-$ARCH"

# ===

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

SED_NAME=sed
if [[ "$OS" == osx ]]; then
  SED_NAME=gsed
fi

# TODO move the kiota-version.csproj to it's own folder?
VERSION=$(cat $SCRIPT_DIR/go-sdk.csproj | grep Version | sed -n 's/.*Version="\([^"]*\)".*/\1/p')
URL="https://github.com/microsoft/kiota/releases/download/v${VERSION}/${PACKAGE_NAME}.zip"

if [[ ! -f $SCRIPT_DIR/target/kiota_tmp/kiota ]]
then
  echo "Local kiota could not be found, downloading"
  rm -rf $SCRIPT_DIR/target/kiota_tmp
  mkdir -p $SCRIPT_DIR/target/kiota_tmp

  # Download with retry logic
  MAX_RETRIES=5
  RETRY_COUNT=0
  DOWNLOAD_SUCCESS=false

  while [[ $RETRY_COUNT -lt $MAX_RETRIES ]]; do
    echo "Attempting to download kiota (attempt $((RETRY_COUNT + 1))/$MAX_RETRIES)..."

    # Use -f to fail on HTTP errors, --retry for built-in retries
    if curl -fL --retry 2 --retry-delay 2 "$URL" -o "$SCRIPT_DIR/target/kiota_tmp/kiota.zip"; then
      # Validate the zip file is not corrupted
      if unzip -t "$SCRIPT_DIR/target/kiota_tmp/kiota.zip" > /dev/null 2>&1; then
        echo "Download successful and zip file validated"
        DOWNLOAD_SUCCESS=true
        break
      else
        echo "Downloaded zip file is corrupted, retrying..."
        rm -f "$SCRIPT_DIR/target/kiota_tmp/kiota.zip"
      fi
    else
      echo "Download failed with HTTP error, retrying..."
    fi

    RETRY_COUNT=$((RETRY_COUNT + 1))
    if [[ $RETRY_COUNT -lt $MAX_RETRIES ]]; then
      # Exponential backoff: 2, 4, 8, 16 seconds
      DELAY=$((2 ** RETRY_COUNT))
      echo "Waiting ${DELAY} seconds before retry..."
      sleep $DELAY
    fi
  done

  if [[ "$DOWNLOAD_SUCCESS" != "true" ]]; then
    echo "ERROR: Failed to download kiota after $MAX_RETRIES attempts"
    echo "URL: $URL"
    exit 1
  fi

  unzip $SCRIPT_DIR/target/kiota_tmp/kiota.zip -d $SCRIPT_DIR/target/kiota_tmp
  chmod u+x $SCRIPT_DIR/target/kiota_tmp/kiota
fi
COMMAND="$SCRIPT_DIR/target/kiota_tmp/kiota"

rm -rf $SCRIPT_DIR/pkg/registryclient-v2
mkdir -p $SCRIPT_DIR/pkg/registryclient-v2
rm -rf $SCRIPT_DIR/pkg/registryclient-v3
mkdir -p $SCRIPT_DIR/pkg/registryclient-v3

cp $SCRIPT_DIR/../common/src/main/resources/META-INF/openapi-v2.json $SCRIPT_DIR/v2.json
cp $SCRIPT_DIR/../common/src/main/resources/META-INF/openapi.json $SCRIPT_DIR/v3.json

$COMMAND generate \
  --language go \
  --openapi $SCRIPT_DIR/v2.json \
  --clean-output \
  -o $SCRIPT_DIR/pkg/registryclient-v2 \
  --namespace-name github.com/apicurio/apicurio-registry/go-sdk/v3/pkg/registryclient-v2

$COMMAND generate \
  --language go \
  --openapi $SCRIPT_DIR/v3.json \
  --clean-output \
  -o $SCRIPT_DIR/pkg/registryclient-v3 \
  --namespace-name github.com/apicurio/apicurio-registry/go-sdk/v3/pkg/registryclient-v3
