#!/bin/bash

set -e

DIRECTUS_URL="${DIRECTUS_URL:-http://localhost:8055}"
ADMIN_EMAIL="${ADMIN_EMAIL:-admin@deusens.com}"
ADMIN_PASSWORD="${ADMIN_PASSWORD:-Admin1234!}"

TOKEN=$(curl -sf -X POST "$DIRECTUS_URL/auth/login" \
  -H "Content-Type: application/json" \
  -d "{\"email\":\"$ADMIN_EMAIL\",\"password\":\"$ADMIN_PASSWORD\"}" \
  | grep -o '"access_token":"[^"]*"' | head -1 | cut -d'"' -f4)

if [ -z "$TOKEN" ]; then
  echo "Authentication failed"
  exit 1
fi

curl -s -o /dev/null -w "%{http_code}" \
  -X POST "$DIRECTUS_URL/fields/homepage_content" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"field":"featured_poi_slugs","type":"json","meta":{"interface":"input-code","note":"Array de slugs para las 4 tarjetas No te pierdas"}}' \
  | grep -Eq "200|201|400|409"

echo "homepage_content.featured_poi_slugs ensured"
