#!/bin/bash

# Normogen Medication Management API Test Suite

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

TEST_USER="med-test-$(date +%s)@example.com"
TEST_USERNAME="medtest$(date +%s)"
TEST_PASSWORD="SecurePass123!"

echo "=========================================="
echo "Medication Management API Test Suite"
echo "=========================================="
echo ""

# Test 1: Health Check
echo "Test 1: Health Check"
HEALTH=$(curl -s "$API_URL/health")
if echo "$HEALTH" | grep -q "ok"; then
    echo "✓ Health check passed"
else
    echo "✗ Health check failed"
    exit 1
fi
echo ""

# Test 2: Register User
echo "Test 2: Register New User"
REGISTER=$(curl -s -X POST "$API_URL/api/auth/register" -H "Content-Type: application/json" -d '{"email":"'$TEST_USER'","username":"'$TEST_USERNAME'","password":"'$TEST_PASSWORD'","first_name":"Test","last_name":"User"}')
if echo "$REGISTER" | grep -q "access_token"; then
    echo "✓ User registration successful"
    ACCESS_TOKEN=$(echo "$REGISTER" | grep -o '"access_token":"[^"]*' | cut -d'"' -f4)
else
    echo "✗ User registration failed"
    exit 1
fi
echo ""

# Test 3: Login
echo "Test 3: Login"
LOGIN=$(curl -s -X POST "$API_URL/api/auth/login" -H "Content-Type: application/json" -d '{"email":"'$TEST_USER'","password":"'$TEST_PASSWORD'"}')
if echo "$LOGIN" | grep -q "access_token"; then
    echo "✓ Login successful"
    ACCESS_TOKEN=$(echo "$LOGIN" | grep -o '"access_token":"[^"]*' | cut -d'"' -f4)
else
    echo "✗ Login failed"
    exit 1
fi
echo ""

# Test 4: Create Medication
echo "Test 4: Create Medication"
CREATE=$(curl -s -X POST "$API_URL/api/medications" -H "Content-Type: application/json" -H "Authorization: Bearer $ACCESS_TOKEN" -d '{"medication_name":"Lisinopril","dosage":"10mg","frequency":"once_daily","instructions":"Take with breakfast","start_date":"2026-03-05"}')
if echo "$CREATE" | grep -q "medication_name"; then
    echo "✓ Medication created successfully"
    MEDICATION_ID=$(echo "$CREATE" | grep -o '"id":"[^"]*' | head -1 | cut -d'"' -f3)
    echo "Medication ID: $MEDICATION_ID"
else
    echo "✗ Medication creation failed"
    echo "Response: $CREATE"
    exit 1
fi
echo ""

# Test 5: List All Medications
echo "Test 5: List All Medications"
LIST=$(curl -s -X GET "$API_URL/api/medications" -H "Authorization: Bearer $ACCESS_TOKEN")
if echo "$LIST" | grep -q "Lisinopril"; then
    echo "✓ List medications successful"
else
    echo "✗ List medications failed"
    echo "Response: $LIST"
    exit 1
fi
echo ""

# Test 6: Get Specific Medication
echo "Test 6: Get Medication Details"
if [ ! -z "$MEDICATION_ID" ]; then
    GET=$(curl -s -X GET "$API_URL/api/medications/$MEDICATION_ID" -H "Authorization: Bearer $ACCESS_TOKEN")
    if echo "$GET" | grep -q "Lisinopril"; then
        echo "✓ Get medication successful"
    else
        echo "✗ Get medication failed"
    fi
fi
echo ""

# Test 7: Update Medication
echo "Test 7: Update Medication"
if [ ! -z "$MEDICATION_ID" ]; then
    UPDATE=$(curl -s -X PUT "$API_URL/api/medications/$MEDICATION_ID" -H "Content-Type: application/json" -H "Authorization: Bearer $ACCESS_TOKEN" -d '{"medication_name":"Lisinopril","dosage":"20mg","frequency":"once_daily","instructions":"Take with breakfast","start_date":"2026-03-05"}')
    if echo "$UPDATE" | grep -q "20mg"; then
        echo "✓ Update medication successful"
    else
        echo "✗ Update medication failed"
    fi
fi
echo ""

# Test 8: Log Dose - Taken
echo "Test 8: Log Dose (Taken)"
if [ ! -z "$MEDICATION_ID" ]; then
    LOG1=$(curl -s -X POST "$API_URL/api/medications/$MEDICATION_ID/log" -H "Content-Type: application/json" -H "Authorization: Bearer $ACCESS_TOKEN" -d '{"status":"taken","notes":"Taken with breakfast"}')
    if echo "$LOG1" | grep -q "logged_at"; then
        echo "✓ Dose logged successfully (taken)"
    else
        echo "✗ Log dose failed"
    fi
fi
echo ""

# Test 9: Calculate Adherence
echo "Test 9: Calculate Adherence"
if [ ! -z "$MEDICATION_ID" ]; then
    ADHERENCE=$(curl -s -X GET "$API_URL/api/medications/$MEDICATION_ID/adherence" -H "Authorization: Bearer $ACCESS_TOKEN")
    echo "✓ Adherence calculated"
    echo "Response: $ADHERENCE"
fi
echo ""

# Test 10: Unauthorized Access Test
echo "Test 10: Unauthorized Access (should fail)"
UNAUTH=$(curl -s -X GET "$API_URL/api/medications")
if echo "$UNAUTH" | grep -q "401\|Unauthorized"; then
    echo "✓ Unauthorized access properly blocked"
else
    echo "✗ Security issue - unauthorized access not blocked"
fi
echo ""

# Test 11: Delete Medication
echo "Test 11: Delete Medication"
if [ ! -z "$MEDICATION_ID" ]; then
    DELETE=$(curl -s -X DELETE "$API_URL/api/medications/$MEDICATION_ID" -H "Authorization: Bearer $ACCESS_TOKEN")
    echo "✓ Delete medication successful"
fi
echo ""

echo "=========================================="
echo "Test Suite Complete"
echo "=========================================="
