#!/bin/bash
# Run All Tests Script for mockupAWS v1.0.0
# Executes Performance, E2E, and Security test suites

set -e

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

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
REPORT_DIR="$SCRIPT_DIR/reports/$TIMESTAMP"

mkdir -p "$REPORT_DIR"

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}  mockupAWS v1.0.0 - All Tests${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
echo "Report Directory: $REPORT_DIR"
echo "Timestamp: $TIMESTAMP"
echo ""

# Track results
PERF_RESULT=0
E2E_RESULT=0
SEC_RESULT=0

# ============================================
# 1. PERFORMANCE TESTS
# ============================================
echo -e "${YELLOW}Running Performance Tests...${NC}"
echo "----------------------------------------"

if [ -d "$SCRIPT_DIR/performance" ]; then
    cd "$SCRIPT_DIR/performance"
    
    # Run smoke test
    if command -v k6 &> /dev/null; then
        k6 run --out json="$REPORT_DIR/perf-smoke.json" scripts/smoke-test.js || PERF_RESULT=1
    else
        echo -e "${RED}k6 not installed, skipping performance tests${NC}"
        PERF_RESULT=2
    fi
else
    echo -e "${RED}Performance tests not found${NC}"
    PERF_RESULT=2
fi

echo ""

# ============================================
# 2. E2E TESTS
# ============================================
echo -e "${YELLOW}Running E2E Tests...${NC}"
echo "----------------------------------------"

if [ -d "$SCRIPT_DIR/../frontend" ]; then
    cd "$SCRIPT_DIR/../frontend"
    
    if [ -f "package.json" ]; then
        # Install dependencies if needed
        if [ ! -d "node_modules" ]; then
            npm ci
        fi
        
        # Install Playwright browsers if needed
        if [ ! -d "$HOME/.cache/ms-playwright" ]; then
            npx playwright install
        fi
        
        # Run E2E tests
        npm run test:e2e:ci 2>&1 | tee "$REPORT_DIR/e2e.log" || E2E_RESULT=1
        
        # Copy HTML report
        if [ -d "e2e-v100-report" ]; then
            cp -r e2e-v100-report "$REPORT_DIR/"
        fi
    else
        echo -e "${RED}Frontend not configured${NC}"
        E2E_RESULT=2
    fi
else
    echo -e "${RED}Frontend directory not found${NC}"
    E2E_RESULT=2
fi

echo ""

# ============================================
# 3. SECURITY TESTS
# ============================================
echo -e "${YELLOW}Running Security Tests...${NC}"
echo "----------------------------------------"

if [ -d "$SCRIPT_DIR/security" ]; then
    cd "$SCRIPT_DIR/security"
    
    if [ -f "scripts/run-security-tests.sh" ]; then
        ./scripts/run-security-tests.sh 2>&1 | tee "$REPORT_DIR/security.log" || SEC_RESULT=1
        
        # Copy reports
        if [ -d "reports" ]; then
            cp reports/*.json "$REPORT_DIR/" 2>/dev/null || true
        fi
    else
        echo -e "${RED}Security test script not found${NC}"
        SEC_RESULT=2
    fi
else
    echo -e "${RED}Security tests not found${NC}"
    SEC_RESULT=2
fi

echo ""

# ============================================
# SUMMARY
# ============================================
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}  TEST SUMMARY${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""

print_result() {
    local name=$1
    local result=$2
    
    if [ $result -eq 0 ]; then
        echo -e "${GREEN}✓ $name: PASSED${NC}"
    elif [ $result -eq 2 ]; then
        echo -e "${YELLOW}! $name: SKIPPED${NC}"
    else
        echo -e "${RED}✗ $name: FAILED${NC}"
    fi
}

print_result "Performance Tests" $PERF_RESULT
print_result "E2E Tests" $E2E_RESULT
print_result "Security Tests" $SEC_RESULT

echo ""
echo "Reports saved to: $REPORT_DIR"
echo ""

# Overall result
TOTAL_RESULT=$((PERF_RESULT + E2E_RESULT + SEC_RESULT))

if [ $TOTAL_RESULT -eq 0 ]; then
    echo -e "${GREEN}========================================${NC}"
    echo -e "${GREEN}  ALL TESTS PASSED!${NC}"
    echo -e "${GREEN}========================================${NC}"
    exit 0
else
    echo -e "${RED}========================================${NC}"
    echo -e "${RED}  SOME TESTS FAILED${NC}"
    echo -e "${RED}========================================${NC}"
    exit 1
fi
