#!/bin/bash

GCLOUD=~/google-cloud-sdk/bin/gcloud
PROJECT=indexation-358907

# Utiliser gcloud pour faire un appel à l'API translate v3
echo "Testing Cloud Translation API v3 via gcloud..."

# Créer le JSON payload
PAYLOAD='{"sourceLanguageCode":"en","targetLanguageCode":"fr","contents":["Good morning, how are you?"]}'

# Faire un appel REST avec l'authentification gcloud
echo "Payload: $PAYLOAD"

# Via gcloud config (pas directement disponible en CLI, mais on peut utiliser curl avec le token)
TOKEN=$($GCLOUD auth print-access-token --account=assous.tom@gmail.com 2>&1)
echo "Token: ${TOKEN:0:50}..."

if [[ "$TOKEN" == ERROR* ]] || [[ "$TOKEN" == "ERROR:"* ]]; then
    echo "Failed to get token: $TOKEN"
else
    echo "Got token successfully. Testing REST call..."
    curl -X POST \
      -H "Authorization: Bearer $TOKEN" \
      -H "Content-Type: application/json" \
      -d "$PAYLOAD" \
      "https://translation.googleapis.com/v3/projects/$PROJECT/locations/global:translateText" 2>&1 | head -50
fi
