#!/bin/bash
#
# DSS Admin UI - Complete Test Automation Suite
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
#
# Orchestrates Phase 1, Phase 2, and Phase 3 testing
# Framework: Pytest-Playwright
# Purpose: Complete validation of all 51 components and 79+ API endpoints
#
# Generated: 2025-12-08
# Author: Gemini 3 Pro Expert Analysis
# Status: Ready for Implementation
#

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

# Configuration
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
DSS_DIR="$PROJECT_ROOT/.dss"
ADMIN_UI_DIR="$PROJECT_ROOT/admin-ui"
LOG_DIR="$DSS_DIR/test-logs"
REPORT_FILE="$LOG_DIR/test-report-$(date +%Y%m%d-%H%M%S).txt"

# Ensure log directory exists
mkdir -p "$LOG_DIR"

# ────────────────────────────────────────────────────────────────────────────
# Helper Functions
# ────────────────────────────────────────────────────────────────────────────

print_header() {
    echo -e "\n${BLUE}╔═══════════════════════════════════════════════════════════════════════════════╗${NC}"
    echo -e "${BLUE}║${NC} $1"
    echo -e "${BLUE}╚═══════════════════════════════════════════════════════════════════════════════╝${NC}\n"
}

print_section() {
    echo -e "\n${YELLOW}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${YELLOW}$1${NC}"
    echo -e "${YELLOW}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}\n"
}

print_success() {
    echo -e "${GREEN}✅ $1${NC}"
}

print_error() {
    echo -e "${RED}❌ $1${NC}"
}

print_info() {
    echo -e "${BLUE}ℹ️  $1${NC}"
}

check_command() {
    if ! command -v "$1" &> /dev/null; then
        print_error "Required command not found: $1"
        exit 1
    fi
}

wait_for_service() {
    local url=$1
    local max_attempts=30
    local attempt=0

    print_info "Waiting for service: $url"

    while [ $attempt -lt $max_attempts ]; do
        if curl -s "$url" > /dev/null 2>&1; then
            print_success "Service available: $url"
            return 0
        fi
        attempt=$((attempt + 1))
        echo -n "."
        sleep 1
    done

    print_error "Service timeout: $url"
    return 1
}

# ────────────────────────────────────────────────────────────────────────────
# Pre-flight Checks
# ────────────────────────────────────────────────────────────────────────────

print_header "DSS Admin UI - Complete Test Automation Suite"

echo "Checking prerequisites..."

# Check Python
check_command "python3"
print_success "Python 3 found"

# Check pytest
if ! python3 -m pytest --version &> /dev/null; then
    print_error "pytest not installed"
    echo "Installing: pip3 install pytest pytest-playwright pytest-asyncio"
    pip3 install pytest pytest-playwright pytest-asyncio httpx
fi
print_success "pytest available"

# Check Playwright
if ! python3 -c "from playwright.sync_api import sync_playwright" &> /dev/null; then
    print_error "Playwright not installed"
    echo "Installing: pip3 install playwright"
    pip3 install playwright
    python3 -m playwright install
fi
print_success "Playwright available"

# Check Node/npm
check_command "node"
print_success "Node.js found"

check_command "npm"
print_success "npm found"

print_success "All prerequisites met\n"

# ────────────────────────────────────────────────────────────────────────────
# Environment Setup
# ────────────────────────────────────────────────────────────────────────────

print_section "PHASE 0: Environment Setup"

print_info "Project directory: $PROJECT_ROOT"
print_info "Admin UI directory: $ADMIN_UI_DIR"
print_info "Test directory: $DSS_DIR"
print_info "Log directory: $LOG_DIR"

# Check if services are already running
DEV_SERVER_RUNNING=false
API_SERVER_RUNNING=false

if curl -s http://localhost:5173 > /dev/null 2>&1; then
    print_success "Dev server (Vite) already running on :5173"
    DEV_SERVER_RUNNING=true
else
    print_info "Starting Vite dev server..."
    cd "$ADMIN_UI_DIR"
    npm run dev > "$LOG_DIR/vite.log" 2>&1 &
    VITE_PID=$!
    print_info "Vite PID: $VITE_PID"
    if wait_for_service "http://localhost:5173"; then
        DEV_SERVER_RUNNING=true
    else
        print_error "Failed to start Vite dev server"
        exit 1
    fi
    cd - > /dev/null
fi

if curl -s http://localhost:8002/api/system/status > /dev/null 2>&1; then
    print_success "API server (FastAPI) already running on :8002"
    API_SERVER_RUNNING=true
else
    print_info "Note: FastAPI backend not running on :8002"
    print_info "Some API tests may be skipped"
fi

# ────────────────────────────────────────────────────────────────────────────
# Phase 1: Smoke Test (Component Loading)
# ────────────────────────────────────────────────────────────────────────────

print_section "PHASE 1: Smoke Test - Component Loading"

print_info "Testing all 51 components for load success and console errors"
print_info "Timeout per component: 3000ms"
print_info "Expected result: 51/51 components load without critical errors"

PHASE1_LOG="$LOG_DIR/phase1-smoke-test.log"
PHASE1_REPORT="$LOG_DIR/phase1-report.txt"

if python3 -m pytest "$DSS_DIR/test_smoke_phase1.py" \
    -v \
    --tb=short \
    --html="$LOG_DIR/phase1-report.html" \
    --self-contained-html \
    2>&1 | tee "$PHASE1_LOG"; then
    print_success "Phase 1 Smoke Test: PASSED"
    PHASE1_RESULT="PASSED"
else
    print_error "Phase 1 Smoke Test: FAILED"
    PHASE1_RESULT="FAILED"
fi

# ────────────────────────────────────────────────────────────────────────────
# Phase 2: Category-Based Testing
# ────────────────────────────────────────────────────────────────────────────

print_section "PHASE 2: Category-Based Testing"

print_info "Testing component categories with specific interactions:"
print_info "  - Tools: Input → Execute → Result validation"
print_info "  - Metrics: Chart rendering, data validation"
print_info "  - Layout: Navigation, sidebar, panels"
print_info "  - Admin: CRUD operations, permissions"
print_info "  - UI Elements: Component behavior"

PHASE2_LOG="$LOG_DIR/phase2-category-test.log"
PHASE2_REPORT="$LOG_DIR/phase2-report.txt"

if python3 -m pytest "$DSS_DIR/test_category_phase2.py" \
    -v \
    --tb=short \
    --html="$LOG_DIR/phase2-report.html" \
    --self-contained-html \
    2>&1 | tee "$PHASE2_LOG"; then
    print_success "Phase 2 Category Testing: PASSED"
    PHASE2_RESULT="PASSED"
else
    print_error "Phase 2 Category Testing: FAILED"
    PHASE2_RESULT="FAILED"
fi

# ────────────────────────────────────────────────────────────────────────────
# Phase 3: API Integration Testing
# ────────────────────────────────────────────────────────────────────────────

print_section "PHASE 3: API Integration Testing"

if [ "$API_SERVER_RUNNING" = true ]; then
    print_info "Testing all 79+ API endpoints:"
    print_info "  - Authentication endpoints"
    print_info "  - Project management"
    print_info "  - Browser logging"
    print_info "  - Figma integration"
    print_info "  - MCP tools"
    print_info "  - System & admin"
    print_info "  - Audit & discovery"

    PHASE3_LOG="$LOG_DIR/phase3-api-test.log"
    PHASE3_REPORT="$LOG_DIR/phase3-report.txt"

    if python3 -m pytest "$DSS_DIR/test_api_phase3.py" \
        -v \
        --tb=short \
        --html="$LOG_DIR/phase3-report.html" \
        --self-contained-html \
        2>&1 | tee "$PHASE3_LOG"; then
        print_success "Phase 3 API Testing: PASSED"
        PHASE3_RESULT="PASSED"
    else
        print_error "Phase 3 API Testing: FAILED"
        PHASE3_RESULT="FAILED"
    fi
else
    print_error "FastAPI backend not running - skipping Phase 3"
    print_info "To run Phase 3: Start FastAPI backend on localhost:8002"
    PHASE3_RESULT="SKIPPED"
fi

# ────────────────────────────────────────────────────────────────────────────
# Generate Final Report
# ────────────────────────────────────────────────────────────────────────────

print_section "TEST EXECUTION COMPLETE"

cat > "$REPORT_FILE" << EOF
╔═══════════════════════════════════════════════════════════════════════════════╗
║                                                                               ║
║               DSS Admin UI - Complete Test Automation Report                  ║
║                                                                               ║
╚═══════════════════════════════════════════════════════════════════════════════╝

Execution Date: $(date)
Project: Design System Admin UI
Framework: Pytest-Playwright (Python)

═══════════════════════════════════════════════════════════════════════════════

PHASE 1: SMOKE TEST (Component Loading)
Status: $PHASE1_RESULT
Purpose: Verify all 51 components load without critical errors
Coverage: Component loading, DOM rendering, console error detection
Log: $PHASE1_LOG

PHASE 2: CATEGORY-BASED TESTING
Status: $PHASE2_RESULT
Purpose: Validate component categories with specific interactions
Coverage: Tools, Metrics, Layout, Admin, UI Elements
Log: $PHASE2_LOG

PHASE 3: API INTEGRATION TESTING
Status: $PHASE3_RESULT
Purpose: Verify all 79+ API endpoints are functional
Coverage: Authentication, Projects, Logs, Figma, MCP, Admin, Audit
Log: $PHASE3_LOG

═══════════════════════════════════════════════════════════════════════════════

SUMMARY:
  Phase 1 (Components): $PHASE1_RESULT
  Phase 2 (Categories): $PHASE2_RESULT
  Phase 3 (API): $PHASE3_RESULT

Test Reports:
  Phase 1: $LOG_DIR/phase1-report.html
  Phase 2: $LOG_DIR/phase2-report.html
  Phase 3: $LOG_DIR/phase3-report.html

Log Files:
  Vite Dev Server: $LOG_DIR/vite.log
  Phase 1 Details: $PHASE1_LOG
  Phase 2 Details: $PHASE2_LOG
  Phase 3 Details: $PHASE3_LOG

═══════════════════════════════════════════════════════════════════════════════

NEXT STEPS:
1. Review HTML reports in browser:
   file://$LOG_DIR/phase1-report.html
   file://$LOG_DIR/phase2-report.html
   file://$LOG_DIR/phase3-report.html

2. Check log files for details:
   cat $PHASE1_LOG
   cat $PHASE2_LOG
   cat $PHASE3_LOG

3. Fix any failing tests:
   pytest <test-file> -k <test-name> -v

4. Run specific category:
   pytest test_category_phase2.py::TestToolsCategory -v

═══════════════════════════════════════════════════════════════════════════════

Generated: $(date)
Report saved to: $REPORT_FILE
EOF

# Print summary to console
echo -e "\n${BLUE}╔═══════════════════════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║                            TEST EXECUTION SUMMARY                              ║${NC}"
echo -e "${BLUE}╚═══════════════════════════════════════════════════════════════════════════════╝${NC}\n"

echo "Phase 1 - Smoke Test: $PHASE1_RESULT"
echo "Phase 2 - Category Testing: $PHASE2_RESULT"
echo "Phase 3 - API Testing: $PHASE3_RESULT"

echo -e "\n${BLUE}Log Files:${NC}"
echo "  $REPORT_FILE"
echo "  $PHASE1_LOG"
echo "  $PHASE2_LOG"
echo "  $PHASE3_LOG"

echo -e "\n${BLUE}HTML Reports:${NC}"
echo "  $LOG_DIR/phase1-report.html"
echo "  $LOG_DIR/phase2-report.html"
echo "  $LOG_DIR/phase3-report.html"

# ────────────────────────────────────────────────────────────────────────────
# Cleanup
# ────────────────────────────────────────────────────────────────────────────

if [ "$DEV_SERVER_RUNNING" = false ] && [ ! -z "$VITE_PID" ]; then
    print_info "Cleaning up: Stopping Vite dev server (PID: $VITE_PID)"
    kill $VITE_PID 2>/dev/null || true
fi

# ────────────────────────────────────────────────────────────────────────────
# Final Status
# ────────────────────────────────────────────────────────────────────────────

if [ "$PHASE1_RESULT" = "PASSED" ] && [ "$PHASE2_RESULT" = "PASSED" ] && \
   ([ "$PHASE3_RESULT" = "PASSED" ] || [ "$PHASE3_RESULT" = "SKIPPED" ]); then
    print_success "\nAll tests completed successfully!\n"
    exit 0
else
    print_error "\nSome tests failed. Review logs for details.\n"
    exit 1
fi
