#!/bin/bash

API_URL="http://solaria.solivarez.com.ar:8001"

echo "Testing Medication Management API"
echo "=================================="

echo ""
echo "1. Health Check"
curl -s "$API_URL/health"
echo ""

echo ""
echo "2. Register User"
REGISTER=$(curl -s -X POST "$API_URL/api/auth/register" \
  -H "Content-Type: application/json" \
  -d '{"email":"med-test@example.com","username":"medtest","password":"SecurePass123!","first_name":"Test","last_name":"User"}')
echo "$REGISTER"

echo ""
echo "3. Login"
LOGIN=$(curl -s -X POST "$API_URL/api/auth/login" \
  -H "Content-Type: application/json" \
  -d '{"email":"med-test@example.com","password":"SecurePass123!"}')
echo "$LOGIN"

TOKEN=$(echo "$LOGIN" | grep -o '"access_token":"[^"]*' | cut -d'"' -f4)
echo ""
echo "Token obtained"

echo ""
echo "4. Create Medication"
CREATE=$(curl -s -X POST "$API_URL/api/medications" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{"profile_id":null,"medication_name":"Lisinopril","dosage":"10mg","frequency":"once_daily","instructions":"Take with breakfast"}')
echo "$CREATE"

echo ""
echo "5. List Medications"
curl -s -X GET "$API_URL/api/medications" \
  -H "Authorization: Bearer $TOKEN"
echo ""

echo ""
echo "Tests complete!"
