#!/bin/bash

# Test script to validate wildcard folder pattern functionality
# This script tests the various wildcard patterns against the test environment

set -e

echo "🧪 Testing Wildcard Folder Pattern Functionality"
echo "================================================"

# Make sure we're in the right directory
cd "$(dirname "$0")/.."

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

# Function to run mail2couch with a specific config and capture output
run_test() {
    local test_name="$1"
    local config_file="$2"
    local max_messages="$3"
    
    echo -e "\n${BLUE}Testing: $test_name${NC}"
    echo "Config: $config_file"
    echo "Max messages: $max_messages"
    echo "----------------------------------------"
    
    # Run mail2couch and capture output
    cd go
    if ./mail2couch --config "../test/$config_file" --max-messages "$max_messages" 2>&1; then
        echo -e "${GREEN}✅ Test completed successfully${NC}"
    else
        echo -e "${RED}❌ Test failed${NC}"
        return 1
    fi
    cd ..
}

# Function to check if containers are running
check_containers() {
    echo "🔍 Checking if test containers are running..."
    
    if ! podman ps | grep -q "greenmail"; then
        echo -e "${RED}❌ GreenMail container not running${NC}"
        echo "Please run: cd test && ./start-test-env.sh"
        exit 1
    fi
    
    if ! podman ps | grep -q "couchdb"; then
        echo -e "${RED}❌ CouchDB container not running${NC}"
        echo "Please run: cd test && ./start-test-env.sh"
        exit 1
    fi
    
    echo -e "${GREEN}✅ Test containers are running${NC}"
}

# Function to populate test data
populate_test_data() {
    echo "📧 Populating test data..."
    cd test
    if python3 populate-greenmail.py; then
        echo -e "${GREEN}✅ Test data populated successfully${NC}"
    else
        echo -e "${RED}❌ Failed to populate test data${NC}"
        exit 1
    fi
    cd ..
}

# Function to build the application
build_app() {
    echo "🔨 Building mail2couch..."
    cd go
    if go build -o mail2couch .; then
        echo -e "${GREEN}✅ Build successful${NC}"
    else
        echo -e "${RED}❌ Build failed${NC}"
        exit 1
    fi
    cd ..
}

# Main test execution
main() {
    echo "Starting wildcard pattern tests..."
    
    # Pre-test checks
    check_containers
    build_app
    populate_test_data
    
    # Wait a moment for test data to be fully ready
    echo "⏳ Waiting for test data to settle..."
    sleep 3
    
    # Test 1: Wildcard all folders (*)
    echo -e "\n${YELLOW}Test 1: Wildcard All Folders Pattern (*)${NC}"
    echo "Expected: Should process all folders except Drafts and Trash"
    run_test "Wildcard All Folders" "config-test.json" 3
    
    # Test 2: Work pattern (Work*)
    echo -e "\n${YELLOW}Test 2: Work Pattern (Work*)${NC}"
    echo "Expected: Should process Work/Projects, Work/Archive but not Work/Temp (excluded by *Temp*)"
    run_test "Work Pattern" "config-test.json" 3
    
    # Test 3: Specific folders only
    echo -e "\n${YELLOW}Test 3: Specific Folders Only${NC}"
    echo "Expected: Should only process INBOX, Sent, and Personal folders"
    run_test "Specific Folders" "config-test.json" 3
    
    # Test 4: Advanced wildcard examples
    echo -e "\n${YELLOW}Test 4: Advanced Wildcard Examples${NC}"
    echo "Expected: Various complex patterns should work correctly"
    run_test "Advanced Patterns" "config-wildcard-examples.json" 2
    
    echo -e "\n${GREEN}🎉 All wildcard pattern tests completed!${NC}"
    echo ""
    echo "To verify results, check the CouchDB databases:"
    echo "  http://localhost:5984/_utils"
    echo ""
    echo "Expected databases should be created for each account:"
    echo "  - wildcard_all_folders_test"
    echo "  - work_pattern_test" 
    echo "  - specific_folders_only"
    echo ""
    echo "Each database should contain documents with 'mailbox' field showing origin folder."
}

# Run main function if executed directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    main "$@"
fi