#!/bin/bash

API_BASE_URL="http://localhost:8080/api/v1"  # Adjusted to include /api/v1
USERNAME="boxuser"
PASSWORD="boxuser"

echo "Sending request to: $API_BASE_URL/login"
echo "Username: $USERNAME"
echo "Password: $PASSWORD"

response=$(curl -vi -w "\n%{http_code}" -X POST -H "Content-Type: application/json" \
  -d "{\"username\":\"$USERNAME\", \"password\":\"$PASSWORD\"}" \
  "$API_BASE_URL/login")

http_status=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')

echo "HTTP Status: $http_status"
echo "Response body: $body"

if [ "$http_status" -eq 200 ]; then
  TOKEN=$(echo "$body" | jq -r '.token // empty')
  if [ -n "$TOKEN" ]; then
    echo "Token obtained successfully:"
    echo "$TOKEN"
  else
    echo "Failed to extract token from response."
  fi
else
  echo "Failed to obtain token. Server returned status $http_status"
fi
