#!/bin/bash

# Script de test curl pour l'API Gateway
echo "🧪 Test de l'API Gateway espacepaie_getContrats"
echo "================================================="

# Configuration
API_BASE="https://0rryyjj6vh.execute-api.eu-west-3.amazonaws.com/default"
TOKEN="patPt20DBCYkQJp2r.69126ad7bf59cf77d75a21dffb01a2af3f3741d56e24d901f2d7cef4ac2ae2fb"
COMPANY_NAME="Compagnie Krapo Roy"
COMPANY_B64=$(echo -n "$COMPANY_NAME" | base64)
ORG_ID="a24ef93e-b05c-469f-99bc-82fe56ea08ac"
CONTRACT_ID="recgRcpVvsZgKYAyO"

echo "Configuration:"
echo "  API Base: $API_BASE"
echo "  Company: $COMPANY_NAME"
echo "  Company B64: $COMPANY_B64"
echo "  Org ID: $ORG_ID"
echo "  Contract ID: $CONTRACT_ID"
echo ""

# Test 1: Racine de l'API
echo "=== TEST 1: Racine de l'API ==="
curl -s -w "\nStatus: %{http_code} | Time: %{time_total}s\n" \
  -H "Accept: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  "$API_BASE"
echo -e "\n"

# Test 2: Liste des contrats
echo "=== TEST 2: Liste des contrats ==="
curl -s -w "\nStatus: %{http_code} | Time: %{time_total}s\n" \
  -H "Accept: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -H "x-company-name: $COMPANY_NAME" \
  -H "x-company-name-b64: $COMPANY_B64" \
  -H "x-active-org-id: $ORG_ID" \
  "$API_BASE/api/contrats"
echo -e "\n"

# Test 3: Contrat spécifique
echo "=== TEST 3: Contrat spécifique ==="
curl -s -w "\nStatus: %{http_code} | Time: %{time_total}s\n" \
  -H "Accept: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -H "x-company-name: $COMPANY_NAME" \
  -H "x-company-name-b64: $COMPANY_B64" \
  -H "x-active-org-id: $ORG_ID" \
  "$API_BASE/api/contrats/$CONTRACT_ID"
echo -e "\n"

# Test 4: Paies du contrat (l'endpoint qui nous intéresse)
echo "=== TEST 4: Paies du contrat (PRINCIPAL) ==="
curl -s -w "\nStatus: %{http_code} | Time: %{time_total}s\n" \
  -H "Accept: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -H "x-company-name: $COMPANY_NAME" \
  -H "x-company-name-b64: $COMPANY_B64" \
  -H "x-active-org-id: $ORG_ID" \
  -H "x-forwarded-from: odentas-espace-paie" \
  "$API_BASE/api/contrats/$CONTRACT_ID/paies?regime=CDDU_MULTI"
echo -e "\n"

# Test 5: Test avec verbose pour voir les headers de réponse
echo "=== TEST 5: Test verbose des paies ==="
curl -v \
  -H "Accept: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -H "x-company-name: $COMPANY_NAME" \
  -H "x-company-name-b64: $COMPANY_B64" \
  -H "x-active-org-id: $ORG_ID" \
  "$API_BASE/api/contrats/$CONTRACT_ID/paies?regime=CDDU_MULTI" \
  2>&1 | head -30
echo -e "\n"

# Test 6: Sans le préfixe /api au cas où
echo "=== TEST 6: Sans préfixe /api ==="
curl -s -w "\nStatus: %{http_code} | Time: %{time_total}s\n" \
  -H "Accept: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -H "x-company-name: $COMPANY_NAME" \
  -H "x-company-name-b64: $COMPANY_B64" \
  "$API_BASE/contrats/$CONTRACT_ID/paies?regime=CDDU_MULTI"
echo -e "\n"

# Test 7: Test des autres stages
echo "=== TEST 7: Test stage 'prod' ==="
curl -s -w "\nStatus: %{http_code} | Time: %{time_total}s\n" \
  -H "Accept: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -H "x-company-name: $COMPANY_NAME" \
  "https://0rryyjj6vh.execute-api.eu-west-3.amazonaws.com/prod/api/contrats/$CONTRACT_ID/paies?regime=CDDU_MULTI"
echo -e "\n"

echo "=== TEST 8: Test stage 'dev' ==="
curl -s -w "\nStatus: %{http_code} | Time: %{time_total}s\n" \
  -H "Accept: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -H "x-company-name: $COMPANY_NAME" \
  "https://0rryyjj6vh.execute-api.eu-west-3.amazonaws.com/dev/api/contrats/$CONTRACT_ID/paies?regime=CDDU_MULTI"

echo ""
echo "✅ Tests terminés!"
echo ""
echo "💡 Points à vérifier :"
echo "  - Quel test retourne un status différent de 404 ?"
echo "  - Y a-t-il des messages d'erreur dans les réponses ?"
echo "  - Les headers de réponse contiennent-ils des infos utiles ?"
