#!/bin/bash

# Test Customer Management API Endpoints
# Generated: 2025-10-17

# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Generate admin JWT token
TOKEN=$(php artisan tinker --execute="echo Tymon\JWTAuth\Facades\JWTAuth::fromUser(App\Model\Admin::find(1));")

if [ -z "$TOKEN" ]; then
    echo -e "${RED}ERROR: Failed to generate admin JWT token${NC}"
    exit 1
fi

echo -e "${GREEN}✓ Admin JWT token generated${NC}"
echo ""

# Base URL
BASE_URL="http://localhost:8000/api/admin"

# Test 1: Customer List (Default)
echo -e "${BLUE}======================================"
echo "TEST 1: Customer List (Default - 09-401)"
echo -e "======================================${NC}"
curl -s -X GET "$BASE_URL/customers?per_page=5" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" | jq '.'
echo ""

# Test 2: Customer List with Search
echo -e "${BLUE}======================================"
echo "TEST 2: Customer Search (09-401)"
echo -e "======================================${NC}"
# Get first customer email from database for testing
CUSTOMER_EMAIL=$(mysql -u showprima -p'e8NHZmyguogWUMdRRoZh7wYXtJCnwvxUb@c' showprima_dev -N -e "SELECT customer_email FROM orders WHERE customer_email IS NOT NULL AND customer_email != '' AND is_anonymized = 0 LIMIT 1" 2>/dev/null)

if [ -n "$CUSTOMER_EMAIL" ]; then
    echo -e "${GREEN}Testing search with: $CUSTOMER_EMAIL${NC}"
    curl -s -X GET "$BASE_URL/customers?search=$CUSTOMER_EMAIL" \
      -H "Authorization: Bearer $TOKEN" \
      -H "Content-Type: application/json" | jq '.'
else
    echo -e "${RED}No customer emails found for search test${NC}"
fi
echo ""

# Test 3: Customer Detail
echo -e "${BLUE}======================================"
echo "TEST 3: Customer Detail (09-402)"
echo -e "======================================${NC}"
if [ -n "$CUSTOMER_EMAIL" ]; then
    echo -e "${GREEN}Getting details for: $CUSTOMER_EMAIL${NC}"
    curl -s -X GET "$BASE_URL/customers/$CUSTOMER_EMAIL" \
      -H "Authorization: Bearer $TOKEN" \
      -H "Content-Type: application/json" | jq '.'
else
    echo -e "${RED}No customer email for detail test${NC}"
fi
echo ""

# Test 4: Customer Orders
echo -e "${BLUE}======================================"
echo "TEST 4: Customer Orders (09-403)"
echo -e "======================================${NC}"
if [ -n "$CUSTOMER_EMAIL" ]; then
    echo -e "${GREEN}Getting orders for: $CUSTOMER_EMAIL${NC}"
    curl -s -X GET "$BASE_URL/customers/$CUSTOMER_EMAIL/orders?per_page=5" \
      -H "Authorization: Bearer $TOKEN" \
      -H "Content-Type: application/json" | jq '.'
else
    echo -e "${RED}No customer email for orders test${NC}"
fi
echo ""

# Test 5: Customer Audit Logs
echo -e "${BLUE}======================================"
echo "TEST 5: Customer Audit Logs (09-403)"
echo -e "======================================${NC}"
if [ -n "$CUSTOMER_EMAIL" ]; then
    echo -e "${GREEN}Getting audit logs for: $CUSTOMER_EMAIL${NC}"
    curl -s -X GET "$BASE_URL/customers/$CUSTOMER_EMAIL/audit-logs?per_page=5" \
      -H "Authorization: Bearer $TOKEN" \
      -H "Content-Type: application/json" | jq '.'
else
    echo -e "${RED}No customer email for audit logs test${NC}"
fi
echo ""

# Test 6: Customer Scan History
echo -e "${BLUE}======================================"
echo "TEST 6: Customer Scan History (09-403)"
echo -e "======================================${NC}"
if [ -n "$CUSTOMER_EMAIL" ]; then
    echo -e "${GREEN}Getting scan history for: $CUSTOMER_EMAIL${NC}"
    curl -s -X GET "$BASE_URL/customers/$CUSTOMER_EMAIL/scan-history" \
      -H "Authorization: Bearer $TOKEN" \
      -H "Content-Type: application/json" | jq '.'
else
    echo -e "${RED}No customer email for scan history test${NC}"
fi
echo ""

# Test 7: Error Handling - Invalid Email (404)
echo -e "${BLUE}======================================"
echo "TEST 7: Error Handling - Invalid Email (404)"
echo -e "======================================${NC}"
curl -s -X GET "$BASE_URL/customers/nonexistent@example.com" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" | jq '.'
echo ""

# Test 8: Error Handling - No Auth Token (401)
echo -e "${BLUE}======================================"
echo "TEST 8: Error Handling - No Auth Token (401)"
echo -e "======================================${NC}"
curl -s -X GET "$BASE_URL/customers?per_page=5" \
  -H "Content-Type: application/json" | jq '.'
echo ""

# Test 9: Error Handling - Invalid Parameters (422)
echo -e "${BLUE}======================================"
echo "TEST 9: Error Handling - Invalid Params (422)"
echo -e "======================================${NC}"
curl -s -X GET "$BASE_URL/customers?per_page=999" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" | jq '.'
echo ""

# Test 10: Filter by Lifetime Value
echo -e "${BLUE}======================================"
echo "TEST 10: Filter by Lifetime Value (VIP)"
echo -e "======================================${NC}"
curl -s -X GET "$BASE_URL/customers?min_lifetime_value=100&sort_by=lifetime_value&sort_order=desc&per_page=3" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" | jq '.'
echo ""

echo -e "${GREEN}======================================"
echo "✓ All tests completed!"
echo -e "======================================${NC}"
