#!/bin/bash

# Test Agent Referral System
echo "Testing Agent Referral System"
echo "============================"

# Start the Laravel development server in the background
echo "Starting Laravel development server..."
php artisan serve > /dev/null 2>&1 &
SERVER_PID=$!

# Wait for the server to start
echo "Waiting for server to start..."
sleep 5

# Run tests for each page
echo "Testing agent registration page..."
RESPONSE=$(curl -s -L http://localhost:8000/agent/register)
if [[ $RESPONSE == *"Agent Registration"* || $RESPONSE == *"agent"* || $RESPONSE == *"register"* ]]; then
    echo "✅ Agent registration page loaded successfully"
else
    echo "❌ Agent registration page failed to load"
    echo "Response: ${RESPONSE:0:100}..."
fi

echo "Testing agent dashboard page..."
RESPONSE=$(curl -s -L -c cookies.txt -b cookies.txt http://localhost:8000/agent/dashboard)
if [[ $RESPONSE == *"Agent Dashboard"* || $RESPONSE == *"dashboard"* ]]; then
    echo "✅ Agent dashboard page loaded successfully"
elif [[ $RESPONSE == *"Login"* || $RESPONSE == *"login"* ]]; then
    echo "✅ Agent dashboard page redirected to login (expected behavior)"
else
    echo "❌ Agent dashboard page failed to load"
    echo "Response: ${RESPONSE:0:100}..."
fi

# Test additional features
echo ""
echo "Testing additional features..."
echo "============================="

# Test commission reports and exports
echo "Testing commission reports and exports..."
RESPONSE=$(curl -s -L -c cookies.txt -b cookies.txt http://localhost:8000/agent/commissions/reports)
if [[ $RESPONSE == *"Commission Reports"* || $RESPONSE == *"reports"* || $RESPONSE == *"Export"* ]]; then
    echo "✅ Commission reports page loaded successfully"
elif [[ $RESPONSE == *"Login"* || $RESPONSE == *"login"* ]]; then
    echo "✅ Commission reports page redirected to login (expected behavior)"
else
    echo "❌ Commission reports page failed to load"
    echo "Response: ${RESPONSE:0:100}..."
fi

# Test email notifications
echo "Testing email notification settings..."
RESPONSE=$(curl -s -L -c cookies.txt -b cookies.txt http://localhost:8000/agent/notifications)
if [[ $RESPONSE == *"Notification Settings"* || $RESPONSE == *"notifications"* || $RESPONSE == *"Email"* ]]; then
    echo "✅ Notification settings page loaded successfully"
elif [[ $RESPONSE == *"Login"* || $RESPONSE == *"login"* ]]; then
    echo "✅ Notification settings page redirected to login (expected behavior)"
else
    echo "❌ Notification settings page failed to load"
    echo "Response: ${RESPONSE:0:100}..."
fi

# Test dashboard analytics
echo "Testing dashboard analytics..."
RESPONSE=$(curl -s -L -c cookies.txt -b cookies.txt http://localhost:8000/agent/analytics)
if [[ $RESPONSE == *"Performance Analytics"* || $RESPONSE == *"analytics"* || $RESPONSE == *"Dashboard"* ]]; then
    echo "✅ Dashboard analytics page loaded successfully"
elif [[ $RESPONSE == *"Login"* || $RESPONSE == *"login"* ]]; then
    echo "✅ Dashboard analytics page redirected to login (expected behavior)"
else
    echo "❌ Dashboard analytics page failed to load"
    echo "Response: ${RESPONSE:0:100}..."
fi

# Kill the Laravel development server
echo "Stopping Laravel development server..."
kill $SERVER_PID

# Clean up
rm -f cookies.txt

echo "All tests completed!"
