#!/bin/bash

# ============================================================================
# Script de Setup Environnement pour Tests MVP
# ============================================================================
# Ce script vérifie et configure l'environnement pour les tests MVP
# ============================================================================

set -euo pipefail

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

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

log_success() {
    echo -e "${GREEN}[✓]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[!]${NC} $1"
}

log_error() {
    echo -e "${RED}[✗]${NC} $1"
}

# ============================================================================
# Vérifications
# ============================================================================

check_command() {
    if command -v "$1" &> /dev/null; then
        log_success "$1 is installed"
        return 0
    else
        log_error "$1 is not installed"
        return 1
    fi
}

check_service() {
    local service=$1
    local port=$2
    
    if curl -s "http://localhost:$port" > /dev/null 2>&1; then
        log_success "$service is running on port $port"
        return 0
    else
        log_warning "$service is not running on port $port"
        return 1
    fi
}

# ============================================================================
# Main
# ============================================================================

main() {
    echo "============================================================================"
    echo "SETUP ENVIRONNEMENT TESTS MVP"
    echo "============================================================================"
    echo ""
    
    # Vérifier les commandes nécessaires
    log_info "Checking required commands..."
    check_command "curl" || exit 1
    check_command "jq" || log_warning "jq not installed (JSON parsing may fail)"
    check_command "node" || log_warning "node not installed (frontend tests may fail)"
    check_command "npm" || log_warning "npm not installed (frontend tests may fail)"
    check_command "go" || log_warning "go not installed (backend tests may fail)"
    
    echo ""
    
    # Vérifier les services
    log_info "Checking services..."
    BACKEND_RUNNING=false
    FRONTEND_RUNNING=false
    
    if check_service "Backend" 8080; then
        BACKEND_RUNNING=true
    fi
    
    if check_service "Frontend" 5173; then
        FRONTEND_RUNNING=true
    elif check_service "Frontend" 3000; then
        FRONTEND_RUNNING=true
    fi
    
    echo ""
    
    # Instructions
    if [ "$BACKEND_RUNNING" = false ]; then
        log_warning "Backend is not running. Start it with:"
        echo "  cd veza-backend-api"
        echo "  go run cmd/api/main.go"
        echo "  # OR"
        echo "  go run cmd/server/main.go"
        echo ""
    fi
    
    if [ "$FRONTEND_RUNNING" = false ]; then
        log_warning "Frontend is not running. Start it with:"
        echo "  cd apps/web"
        echo "  npm install"
        echo "  npm run dev"
        echo ""
    fi
    
    # Variables d'environnement
    log_info "Setting up environment variables..."
    export API_URL="${API_URL:-http://localhost:8080/api/v1}"
    export FRONTEND_URL="${FRONTEND_URL:-http://localhost:5173}"
    export TEST_EMAIL="test-$(date +%s)@example.com"
    export TEST_PASSWORD="TestPassword123!"
    export TEST_USERNAME="testuser_$(date +%s)"
    
    log_success "Environment variables set:"
    echo "  API_URL=$API_URL"
    echo "  FRONTEND_URL=$FRONTEND_URL"
    echo "  TEST_EMAIL=$TEST_EMAIL"
    echo "  TEST_USERNAME=$TEST_USERNAME"
    echo ""
    
    if [ "$BACKEND_RUNNING" = true ] && [ "$FRONTEND_RUNNING" = true ]; then
        log_success "Environment is ready for testing!"
        echo ""
        echo "Run tests with:"
        echo "  ./scripts/test-mvp-api.sh"
        echo "  cd apps/web && npm run test:e2e -- e2e/mvp-integration.spec.ts"
    else
        log_warning "Some services are not running. Please start them before running tests."
    fi
}

main

