set_model_id() {
  if  [ -z "$1" ] && [ -z "$2" ]; then
    yq -o json '.| select(.data | has("MODEL_ID"))| {"ConfigMap": .metadata.name, "MODEL_ID": .data.MODEL_ID}'  *.yaml
    echo "usage:"
    echo "  set_model_id \${ConfigMap} \${MODEL_ID}"
    return
  fi
  conf=$1
  file=${1%%-*}
  sed -i '/name: '"${conf}"'/,/---/s|\(MODEL_ID:\).*|\1 "'"${2}"'"|' ${file}.yaml
}

set_model_mirror() {
  if  [ -z "$1" ] ; then
    yq -o json '.| select(.data | has("MODEL_ID"))| {"ConfigMap": .metadata.name, "MODEL_MIRROR": .data.HF_ENDPOINT}'  *.yaml
    echo "usage:"
    echo "  set_model_mirror \${MODEL_MIRROR}"
    return
  fi
  cm=$(yq -r -o json '.| select(.data | has("MODEL_ID"))| .metadata.name'  *.yaml)
  mirror=$1
  for i in $cm; do
    conf=$i
    file=${i%%-*}
    echo "ConfigMap: $conf set mirror as $mirror"
    has_mirror=$(yq -r -o json '.| select(.metadata.name == "'"${conf}"'")| .data.HF_ENDPOINT' ${file}.yaml)
    if [ "$has_mirror" == "null" ]; then
      sed -i '/name: '"${conf}"'/,/---/s|\(data:\)|\1\n  HF_ENDPOINT: "'"${mirror}"'"|' ${file}.yaml
    else
      sed -i '/name: '"${conf}"'/,/---/s|\(HF_ENDPOINT:\).*|\1 "'"${1}"'"|' ${file}.yaml
    fi
  done
}

set_hf_token() {
  if  [ -z "$1" ] ; then
    echo "usage:"
    echo "  set_hf_token \${HF_TOKEN}"
    return
  fi
  sed -i "s/\(HF_TOKEN:\).*/\1 \"${1}\"/g" *.yaml
  sed -i "s/\(HUGGINGFACEHUB_API_TOKEN:\).*/\1 \"${1}\"/g" *.yaml
  sed -i "s/\(HUGGING_FACE_HUB_TOKEN:\).*/\1 \"${1}\"/g" *.yaml
}

set_https_proxy() {
  if  [ -z "$1" ] ; then
    echo "usage:"
    echo "  set_https_proxy \${https_proxy}"
    return
  fi
  https_proxy=$1
  sed -i -e "s|\(https_proxy:\)\s*\"\"|\1 \"$https_proxy\"|g" *.yaml
  sed -i '/https_proxy/{n;s|\(value:\)\s.*""|\1 "'"$https_proxy"'"|g}' *.yaml
}

set_http_proxy() {
  if  [ -z "$1" ] ; then
    echo "usage:"
    echo "  set_http_proxy \${http_proxy}"
    return
  fi
  http_proxy=$1
  sed -i -e "s|\(http_proxy:\)\s*\"\"|\1 \"$http_proxy\"|g" *.yaml
  sed -i '/http_proxy/{n;s|\(value:\)\s.*""|\1 "'"$http_proxy"'"|g}' *.yaml
}

set_no_proxy() {
  if  [ -z "$1" ] ; then
    echo "usage:"
    echo "  set_no_proxy \${no_proxy}"
    return
  fi
  no_proxy=$1
  sed -i -e "s|\(no_proxy:\)\s*\"\"|\1 \"$no_proxy\"|g" *.yaml
  sed -i '/no_proxy/{n;s|\(value:\)\s.*""|\1 "'"$no_proxy"'"|g}' *.yaml
}

set_backend_service_endpoint() {
  for i in $(grep -oP "(?<=APP_BACKEND_SERVICE_ENDPOINT_).*"  *.yaml); do
    echo $i
    name=${i##*:}
    file=${name,,}.yaml
    svc=$(yq -o json '. | select(.metadata.name == "'"${name,,}"'" and .kind=="Service")' $file)
    port=$(jq .spec.ports[0].port <<< $svc)

    url=http://${name,,}.${ns:-default}.svc.cluster.local:${port}
    echo $url
    sed -i -e '/APP_BACKEND_SERVICE_ENDPOINT_'"$name"'/{n;s|\(value:\)\s.*|\1 "'"$url"'"|}' productivity_suite_reactui.yaml
  done
}


set_dataprep_service_endpoint() {
  name=chatqna-data-prep
  file=chatqna.yaml
  svc=$(yq -o json '. | select(.metadata.name == "'"$name"'" and .kind=="Service")' $file)
  port=$(jq .spec.ports[0].port <<< $svc)
  url=http://${name}.${ns:-default}.svc.cluster.local:${port}
  echo $url
  for i in $(grep -oP "(?<=APP_)DATAPREP.*(?=_ENDPOINT)" *.yaml); do
    echo $i
    curd=${i##*:};
    sed -i -e '/'"$curd"'/{n;s|\(value:\)\s.*|\1 "'"$url"'"|}' productivity_suite_reactui.yaml;
  done
}


set_chat_history_endpoint() {
  for i in $(grep -oP "(?<=APP_)CHAT_HISTORY.*(?=_ENDPOINT)" *.yaml); do
    echo $i;
    curd=${i##*:};
    name=${curd%_*};
    file=${name,,}.yaml;
    name=${name/_/-};
    svc=$(yq -o json '. | select(.metadata.name == "'"${name,,}"'" and .kind=="Service")' $file)
    port=$(jq .spec.ports[0].port <<< $svc)
    url=http://${name,,}.${ns:-default}.svc.cluster.local:${port};
    echo $url;
    sed -i -e '/'"$curd"'/{n;s|\(value:\)\s.*|\1 "'"$url"'"|}' productivity_suite_reactui.yaml;
  done
}


set_prompt_service_endpoint() {
  for i in $(grep -oP "(?<=APP_)PROMPT_SERVICE.*(?=_ENDPOINT)" *.yaml); do
    echo $i;
    curd=${i##*:};
    curdr=${curd/SERVICE/REGISTRY};
    name=${curdr%_*};
    file=${name,,}.yaml;
    name=${name/_/-};
    svc=$(yq -o json '. | select(.metadata.name == "'"${name,,}"'" and .kind=="Service")' $file)
    port=$(jq .spec.ports[0].port <<< $svc)
    url=http://${name,,}.${ns:-default}.svc.cluster.local:${port};
    echo $url;
    sed -i -e '/'"$curd"'/{n;s|\(value:\)\s.*|\1 "'"$url"'"|}' productivity_suite_reactui.yaml ;
  done
}

set_keycloak_service_endpoint() {
  name=keycloak
  file=keycloak_install.yaml
  svc=$(yq -o json '. | select(.metadata.name == "'"$name"'" and .kind=="Service")' $file)
  port=$(jq .spec.ports[0].port <<< $svc)
  url=http://${name}.${ns:-default}.svc.cluster.local:${port}
  echo $url
  sed -i -e '/APP_KEYCLOAK_SERVICE_ENDPOINT/{n;s|\(value:\)\s.*|\1 "'"$url"'"|}' productivity_suite_reactui.yaml
}

set_services_endpoint() {
  set_backend_service_endpoint
  set_keycloak_service_endpoint
  set_chat_history_endpoint
  set_prompt_service_endpoint
  set_dataprep_service_endpoint
}
