#!/bin/bash

# InstaRank SEO Plugin - Automated Testing Script
# This script will test the plugin on your WordPress sandbox

echo "════════════════════════════════════════════════════════════"
echo "  InstaRank SEO Plugin - Automated Test Suite v1.0.1"
echo "════════════════════════════════════════════════════════════"
echo ""

# Configuration
SANDBOX_URL="https://eu2.wpsandbox.org/site/s-q954dl4flaj8l"
WP_ADMIN="$SANDBOX_URL/wp-admin"
API_BASE="$SANDBOX_URL/wp-json/instarank/v1"

# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Test counters
TESTS_RUN=0
TESTS_PASSED=0
TESTS_FAILED=0

# Function to run a test
run_test() {
    local test_name="$1"
    local command="$2"
    local expected_pattern="$3"

    ((TESTS_RUN++))
    echo -n "Testing: $test_name... "

    result=$(eval "$command" 2>&1)

    if echo "$result" | grep -q "$expected_pattern"; then
        echo -e "${GREEN}✓ PASS${NC}"
        ((TESTS_PASSED++))
        return 0
    else
        echo -e "${RED}✗ FAIL${NC}"
        echo "  Expected: $expected_pattern"
        echo "  Got: $result"
        ((TESTS_FAILED++))
        return 1
    fi
}

echo "Please enter your API key from WordPress admin:"
read -r API_KEY

if [ -z "$API_KEY" ]; then
    echo -e "${RED}Error: API key is required${NC}"
    exit 1
fi

echo ""
echo "Starting tests..."
echo "─────────────────────────────────────────────────────────────"
echo ""

# Test 1: Health Check
run_test "Health endpoint" \
    "curl -s '$API_BASE/health'" \
    "api_version"

# Test 2: Connect endpoint
run_test "Connect endpoint" \
    "curl -s -X POST '$API_BASE/connect' -H 'X-InstaRank-API-Key: $API_KEY' -H 'Content-Type: application/json' -d '{\"project_id\":\"test-123\",\"integration_id\":\"int-456\"}'" \
    "success"

# Test 3: Submit change
run_test "Submit change" \
    "curl -s -X POST '$API_BASE/changes' -H 'X-InstaRank-API-Key: $API_KEY' -H 'Content-Type: application/json' -d '{\"post_id\":1,\"change_type\":\"meta_title\",\"old_value\":\"Hello World\",\"new_value\":\"Optimized Title\",\"reason\":\"Testing\"}'" \
    "change_id"

# Test 4: Invalid API key should fail
run_test "Invalid API key rejection" \
    "curl -s -X POST '$API_BASE/changes' -H 'X-InstaRank-API-Key: INVALID_KEY' -H 'Content-Type: application/json' -d '{\"post_id\":1,\"change_type\":\"meta_title\",\"old_value\":\"Test\",\"new_value\":\"Test\"}'" \
    "401\\|Unauthorized\\|Invalid API key"

echo ""
echo "─────────────────────────────────────────────────────────────"
echo "Test Results:"
echo "─────────────────────────────────────────────────────────────"
echo "Total Tests: $TESTS_RUN"
echo -e "${GREEN}Passed: $TESTS_PASSED${NC}"
echo -e "${RED}Failed: $TESTS_FAILED${NC}"
echo ""

if [ $TESTS_FAILED -eq 0 ]; then
    echo -e "${GREEN}✓ All tests passed!${NC}"
    echo ""
    echo "Next steps:"
    echo "1. Login to $WP_ADMIN"
    echo "2. Go to InstaRank SEO > Changes"
    echo "3. Verify the change appears in Pending tab"
    echo "4. Test approve/reject/rollback functionality"
    exit 0
else
    echo -e "${RED}✗ Some tests failed${NC}"
    echo "Please check the output above for details"
    exit 1
fi
