#!/bin/bash
# Demo script to show exactly how to configure the example analyzers

echo "Configuring Example Analyzers Demo"
echo "=================================="

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

# Get Magento root from command line argument or default
if [ -z "$1" ]; then
    echo -e "${RED}Error: Please provide Magento root directory${NC}"
    echo "Usage: $0 <magento-root>"
    echo "Example: $0 /var/www/magento"
    exit 1
fi

MAGENTO_ROOT="$1"
MODULE_PATH="$HOME/.n98-magerun2/modules/performance-review"

echo -e "${BLUE}Magento Root:${NC} $MAGENTO_ROOT"
echo -e "${BLUE}Module Path:${NC} $MODULE_PATH"

# Step 1: Show what example analyzers we have
echo -e "\n${YELLOW}Step 1: Available Example Analyzers${NC}"
echo "======================================"
ls -la "$MODULE_PATH/examples/CustomAnalyzers/"

# Step 2: Create the configuration
echo -e "\n${YELLOW}Step 2: Creating Configuration${NC}"
echo "================================"

cat > "$MAGENTO_ROOT/app/etc/n98-magerun2.yaml" << EOF
# Configuration for Example Analyzers
# Generated by demo-example-analyzers.sh

# 1. Register the namespace (tells PHP where to find the classes)
autoloaders_psr4:
  # Point to the examples directory
  MyCompany\\PerformanceAnalyzer\\: '$MODULE_PATH/examples/CustomAnalyzers'

# 2. Configure the analyzers
commands:
  PerformanceReview\\Command\\PerformanceReviewCommand:
    analyzers:
      # Example analyzers group
      examples:
        - id: redis-memory-example
          class: 'MyCompany\\PerformanceAnalyzer\\RedisMemoryAnalyzer'
          description: 'Example: Check Redis memory usage and fragmentation'
          category: redis
          config:
            fragmentation_threshold: 1.2
            memory_limit_mb: 512
            
        - id: elasticsearch-example
          class: 'MyCompany\\PerformanceAnalyzer\\ElasticsearchHealthAnalyzer'
          description: 'Example: Check Elasticsearch cluster health'
          category: search
      
      # Disable a core analyzer to prove config is loading
      core:
        api:
          enabled: false
EOF

echo -e "${GREEN}✓${NC} Created configuration at: $MAGENTO_ROOT/app/etc/n98-magerun2.yaml"

# Step 3: Show the configuration
echo -e "\n${YELLOW}Step 3: Configuration Content${NC}"
echo "============================="
echo -e "${BLUE}File content:${NC}"
cat "$MAGENTO_ROOT/app/etc/n98-magerun2.yaml"

# Step 4: Test if configuration loads
echo -e "\n${YELLOW}Step 4: Testing Configuration Loading${NC}"
echo "====================================="

# Check if API analyzer is disabled (proves config loads)
echo -e "${BLUE}Testing if core analyzer can be disabled...${NC}"
API_COUNT=$(n98-magerun2.phar --root-dir "$MAGENTO_ROOT" performance:review 2>&1 | grep -c "API Analysis" || true)

if [ "$API_COUNT" -eq "0" ]; then
    echo -e "${GREEN}✓${NC} Configuration is loading! (API analyzer was disabled)"
else
    echo -e "${RED}✗${NC} Configuration may not be loading (API analyzer still runs)"
fi

# Step 5: List analyzers
echo -e "\n${YELLOW}Step 5: Listing All Analyzers${NC}"
echo "============================="
echo -e "${BLUE}Running: --list-analyzers${NC}"
n98-magerun2.phar --root-dir "$MAGENTO_ROOT" performance:review --list-analyzers

# Step 6: Check for example analyzers
echo -e "\n${YELLOW}Step 6: Checking for Example Analyzers${NC}"
echo "======================================"
EXAMPLE_COUNT=$(n98-magerun2.phar --root-dir "$MAGENTO_ROOT" performance:review --list-analyzers 2>&1 | grep -c "example" || true)

if [ "$EXAMPLE_COUNT" -gt "0" ]; then
    echo -e "${GREEN}✓${NC} Found $EXAMPLE_COUNT example analyzer(s)!"
    echo ""
    echo "Example analyzers found:"
    n98-magerun2.phar --root-dir "$MAGENTO_ROOT" performance:review --list-analyzers 2>&1 | grep "example" || true
else
    echo -e "${RED}✗${NC} No example analyzers found"
    echo ""
    echo "Debugging information:"
    echo "1. Check if files exist:"
    ls -la "$MODULE_PATH/examples/CustomAnalyzers/"
    echo ""
    echo "2. Check PHP can load the file:"
    php -l "$MODULE_PATH/examples/CustomAnalyzers/RedisMemoryAnalyzer.php"
    echo ""
    echo "3. Run with verbose mode:"
    n98-magerun2.phar --root-dir "$MAGENTO_ROOT" performance:review --list-analyzers -vvv 2>&1 | head -20
fi

# Step 7: Try to run example analyzers
echo -e "\n${YELLOW}Step 7: Running Example Analyzers${NC}"
echo "================================="

if [ "$EXAMPLE_COUNT" -gt "0" ]; then
    echo -e "${BLUE}Running Redis memory analyzer example...${NC}"
    n98-magerun2.phar --root-dir "$MAGENTO_ROOT" performance:review --category=redis 2>&1 | grep -A5 -B5 "redis" || echo "No Redis output found"
    
    echo -e "\n${BLUE}Running all example analyzers...${NC}"
    n98-magerun2.phar --root-dir "$MAGENTO_ROOT" performance:review --category=examples || echo "No examples category found"
else
    echo -e "${YELLOW}Skipping execution test - no analyzers loaded${NC}"
fi

# Summary
echo -e "\n${YELLOW}Summary${NC}"
echo "======="
echo "Configuration file: $MAGENTO_ROOT/app/etc/n98-magerun2.yaml"
echo "Example analyzers path: $MODULE_PATH/examples/CustomAnalyzers/"
echo ""
echo "To test manually:"
echo "1. List analyzers: n98-magerun2.phar --root-dir $MAGENTO_ROOT performance:review --list-analyzers"
echo "2. Run all: n98-magerun2.phar --root-dir $MAGENTO_ROOT performance:review"
echo "3. Run examples only: n98-magerun2.phar --root-dir $MAGENTO_ROOT performance:review --category=examples"

echo -e "\n${GREEN}Demo complete!${NC}"