#!/bin/bash

# Windows Container Testing Setup Script
# Sets up multiple approaches for testing Windows compatibility locally

set -e

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

print_status() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

print_status "Setting up Windows container testing environment..."

# Check current system
print_status "Checking system capabilities..."
echo "Docker version: $(docker --version)"
echo "Docker info:"
docker system info | grep -E "(Operating System|OSType|Architecture|Kernel Version)"

# Method 1: Check if Windows containers are available
print_status "Method 1: Checking for native Windows container support..."
if docker pull mcr.microsoft.com/windows/nanoserver:ltsc2022 2>/dev/null; then
    print_success "Native Windows containers are available!"
    WINDOWS_NATIVE=true
else
    print_warning "Native Windows containers not available (expected in WSL2/Linux Docker)"
    WINDOWS_NATIVE=false
fi

# Method 2: Set up Node.js Windows simulation
print_status "Method 2: Setting up Node.js Windows simulation..."
if docker pull node:18-alpine 2>/dev/null; then
    print_success "Node.js Alpine images available for cross-platform testing"
    NODE_SIMULATION=true
else
    print_error "Node.js images not available"
    NODE_SIMULATION=false
fi

# Method 3: Create Windows environment simulation (inline)
print_status "Method 3: Windows environment simulation (using inline variables)..."
cat > scripts/windows-tests/windows-env.sh << 'EOF'
#!/bin/bash
# Windows environment simulation - no sensitive files
export RUNNER_OS=Windows
export PATH="/c/Windows/System32:/c/Windows:/c/Windows/System32/Wbem:$PATH"
export USERPROFILE=/c/Users/runneradmin
export TEMP=/c/Users/runneradmin/AppData/Local/Temp
export TMP=/c/Users/runneradmin/AppData/Local/Temp
export HOMEDRIVE=C:
export HOMEPATH=/Users/runneradmin
export PATHEXT=.COM:.EXE:.BAT:.CMD:.VBS:.VBE:.JS:.JSE:.WSF:.WSH:.MSC
echo "Windows environment variables set"
EOF
chmod +x scripts/windows-tests/windows-env.sh

# Method 4: Enhanced Act configuration for Windows testing
print_status "Method 4: Creating enhanced Act configuration..."
cp .actrc .actrc.backup
cat > .actrc.windows-enhanced << 'EOF'
# Enhanced Windows Testing Configuration

# Primary testing (Linux-based but Windows-compatible Node.js testing)
-P ubuntu-latest=catthehacker/ubuntu:act-22.04
-P windows-latest=node:18-alpine
-P windows-2022=node:18-alpine  
-P windows-2019=node:16-alpine
-P macos-latest=catthehacker/ubuntu:act-22.04

# Environment variables for Windows simulation
--env NODE_ENV=test
--env CI=true
--env RUNNER_OS=Windows
--env RUNNER_TEMP=/tmp
--env RUNNER_TOOL_CACHE=/opt/hostedtoolcache

# Enhanced settings
--verbose
--container-daemon-socket unix:///var/run/docker.sock
--artifact-server-path /tmp/act-artifacts
--bind
EOF

# Method 5: Create Windows-specific test scripts
print_status "Method 5: Creating Windows-specific test scenarios..."
mkdir -p scripts/windows-tests

cat > scripts/windows-tests/test-windows-paths.js << 'EOF'
// Test Windows path handling
const path = require('path');
const os = require('os');

console.log('Testing Windows-compatible path handling...');
console.log('Platform:', os.platform());
console.log('Path separator:', path.sep);
console.log('Path delimiter:', path.delimiter);

// Test path operations that should work cross-platform
const testPath = path.join('server', 'src', 'index.ts');
console.log('Cross-platform path:', testPath);

// Test environment variables
console.log('NODE_ENV:', process.env.NODE_ENV);
console.log('RUNNER_OS:', process.env.RUNNER_OS);

console.log('✅ Windows compatibility test passed');
EOF

cat > scripts/windows-tests/test-windows-startup.sh << 'EOF'
#!/bin/bash
# Test Windows-like startup scenarios

echo "Testing Windows-compatible startup..."

# Simulate Windows environment
export RUNNER_OS=Windows
export PATH="/c/Windows/System32:$PATH"

# Test Node.js startup
cd server
echo "Testing Node.js startup in Windows-like environment..."
node --version
npm --version

# Test our application
echo "Testing MCP server startup..."
npm run help

echo "✅ Windows startup test completed"
EOF

chmod +x scripts/windows-tests/test-windows-startup.sh

# Method 6: Create multi-platform test runner
print_status "Method 6: Creating comprehensive test runner..."
cat > scripts/test-all-platforms.sh << 'EOF'
#!/bin/bash

# Comprehensive multi-platform testing script

set -e

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

print_test() {
    echo -e "${BLUE}[TEST]${NC} $1"
}

print_pass() {
    echo -e "${GREEN}[PASS]${NC} $1"
}

print_fail() {
    echo -e "${RED}[FAIL]${NC} $1"
}

# Test 1: Ubuntu (Linux) - Primary platform
print_test "Testing Ubuntu/Linux platform..."
if ./local-test.sh dry-run code-quality >/dev/null 2>&1; then
    print_pass "Ubuntu/Linux testing works"
else
    print_fail "Ubuntu/Linux testing failed"
fi

# Test 2: Windows simulation with Node.js
print_test "Testing Windows simulation (Node.js)..."
if docker run --rm -v "$PWD":/workspace -w /workspace/server node:18-alpine npm --version >/dev/null 2>&1; then
    print_pass "Windows simulation (Node.js) works"
else
    print_fail "Windows simulation (Node.js) failed"
fi

# Test 3: Cross-platform Node.js compatibility
print_test "Testing cross-platform Node.js compatibility..."
if node scripts/windows-tests/test-windows-paths.js >/dev/null 2>&1; then
    print_pass "Cross-platform compatibility works"
else
    print_fail "Cross-platform compatibility failed"
fi

# Test 4: Windows environment simulation
print_test "Testing Windows environment simulation..."
if source .env.windows && echo "Windows env loaded" >/dev/null 2>&1; then
    print_pass "Windows environment simulation works"
else
    print_fail "Windows environment simulation failed"
fi

echo ""
echo "Multi-platform testing summary completed!"
EOF

chmod +x scripts/test-all-platforms.sh

# Create usage instructions
print_status "Creating usage instructions..."
cat > WINDOWS-TESTING.md << 'EOF'
# Windows Container Testing Setup

This setup provides multiple approaches for testing Windows compatibility locally in a WSL2/Linux Docker environment.

## Available Methods

### Method 1: Native Windows Containers (if available)
```bash
# Only works if Docker is configured for Windows containers
docker pull mcr.microsoft.com/windows/nanoserver:ltsc2022
```

### Method 2: Node.js Windows Simulation
```bash
# Use Alpine Node.js images for lightweight Windows-compatible testing
./local-test.sh run code-quality --actrc .actrc.windows-enhanced
```

### Method 3: Cross-Platform Node.js Testing
```bash
# Test Node.js compatibility across platforms
node scripts/windows-tests/test-windows-paths.js
scripts/windows-tests/test-windows-startup.sh
```

### Method 4: Environment Simulation
```bash
# Load Windows-like environment variables
source scripts/windows-tests/windows-env.sh
```

### Method 5: Comprehensive Testing
```bash
# Run all platform tests
scripts/test-all-platforms.sh
```

## Usage Examples

### Quick Windows Simulation Test
```bash
# Test with Windows-like configuration
ACT_RC=.actrc.windows-enhanced ./local-test.sh dry-run code-quality
```

### Cross-Platform Build Test
```bash
# Test Node.js builds across platforms
docker run --rm -v "$PWD":/workspace -w /workspace/server node:18-alpine npm run build
docker run --rm -v "$PWD":/workspace -w /workspace/server node:18-windowsservercore npm run build
```

### Comprehensive CI Simulation
```bash
# Simulate full CI pipeline with all platforms
./scripts/test-all-platforms.sh
```

## Configuration Files

- `.actrc.windows-enhanced` - Enhanced Act configuration for Windows testing
- `scripts/windows-tests/windows-env.sh` - Windows environment simulation script
- `scripts/windows-tests/` - Windows-specific test scripts
- `scripts/test-all-platforms.sh` - Comprehensive test runner

## Notes

- True Windows containers require Windows host or Docker Desktop Windows mode
- This setup provides the next best thing: cross-platform Node.js testing
- Environment simulation helps catch Windows-specific path and environment issues
- All tests are designed to work in WSL2/Linux Docker environments

## Troubleshooting

1. **Windows containers not available**: This is expected in WSL2. Use Node.js simulation instead.
2. **Path issues**: Use Node.js `path` module for cross-platform path handling.
3. **Environment variables**: Test with both Linux and Windows-style environment variables.
EOF

# Summary and next steps
print_status "Setup complete! Summary:"
echo ""
print_success "✅ Windows testing environment configured"
print_success "✅ Multiple testing approaches available"
print_success "✅ Cross-platform Node.js testing ready"
print_success "✅ Environment simulation configured"
print_success "✅ Comprehensive test runner created"
echo ""
print_status "Next steps:"
echo "1. Review WINDOWS-TESTING.md for usage instructions"
echo "2. Run: scripts/test-all-platforms.sh"
echo "3. Test specific scenarios with the enhanced Act configuration"
echo ""
print_status "Available test commands:"
echo "• scripts/test-all-platforms.sh - Comprehensive testing"
echo "• ACT_RC=.actrc.windows-enhanced ./local-test.sh dry-run code-quality"
echo "• node scripts/windows-tests/test-windows-paths.js"
echo "• scripts/windows-tests/test-windows-startup.sh"