#!/bin/bash
# ORIGIN Error Discovery Script
# Ce script découvre automatiquement toutes les erreurs existantes

set -e

echo "🔍 Découverte des erreurs existantes - Projet Veza"
echo "=================================================="
echo ""

# Créer les répertoires nécessaires
mkdir -p docs/ORIGIN/error-logs
mkdir -p logs

# Date pour les fichiers
DATE=$(date +%Y%m%d-%H%M%S)

echo "📅 Date d'analyse: $(date)"
echo ""

# ===========================================
# BACKEND GO
# ===========================================
echo "📡 1/4 - Analyse du Backend Go..."
echo "   ├─ Build..."
cd veza-backend-api
go build ./... > ../docs/ORIGIN/error-logs/backend-build-${DATE}.log 2>&1 && echo "   │  ✅ Build OK" || echo "   │  ❌ Build FAILED"

echo "   ├─ Tests..."
go test ./... -v -coverprofile=../docs/ORIGIN/error-logs/backend-coverage-${DATE}.out > ../docs/ORIGIN/error-logs/backend-tests-${DATE}.log 2>&1 && echo "   │  ✅ Tests OK" || echo "   │  ❌ Tests FAILED"

echo "   ├─ Vet..."
go vet ./... > ../docs/ORIGIN/error-logs/backend-vet-${DATE}.log 2>&1 && echo "   │  ✅ Vet OK" || echo "   │  ❌ Vet found issues"

echo "   └─ Lint (si golangci-lint disponible)..."
if command -v golangci-lint &> /dev/null; then
    golangci-lint run --out-format=checkstyle > ../docs/ORIGIN/error-logs/backend-lint-${DATE}.xml 2>&1 && echo "      ✅ Lint OK" || echo "      ❌ Lint found issues"
else
    echo "      ⚠️  golangci-lint non installé (skip)"
fi

cd ..
echo ""

# ===========================================
# FRONTEND REACT
# ===========================================
echo "🌐 2/4 - Analyse du Frontend React..."
echo "   ├─ Build..."
cd apps/web
npm run build > ../../docs/ORIGIN/error-logs/frontend-build-${DATE}.log 2>&1 && echo "   │  ✅ Build OK" || echo "   │  ❌ Build FAILED"

echo "   ├─ Tests..."
npm test -- --run --reporter=verbose > ../../docs/ORIGIN/error-logs/frontend-tests-${DATE}.log 2>&1 && echo "   │  ✅ Tests OK" || echo "   │  ❌ Tests FAILED"

echo "   ├─ Lint..."
npm run lint > ../../docs/ORIGIN/error-logs/frontend-lint-${DATE}.log 2>&1 && echo "   │  ✅ Lint OK" || echo "   │  ❌ Lint found issues"

echo "   └─ TypeScript check..."
npx tsc --noEmit > ../../docs/ORIGIN/error-logs/frontend-tsc-${DATE}.log 2>&1 && echo "      ✅ TypeScript OK" || echo "      ❌ TypeScript errors found"

cd ../..
echo ""

# ===========================================
# CHAT SERVER RUST (si existe)
# ===========================================
if [ -d "veza-chat-server" ]; then
    echo "💬 3/4 - Analyse du Chat Server Rust..."
    echo "   ├─ Build..."
    cd veza-chat-server
    cargo build --release > ../docs/ORIGIN/error-logs/chat-build-${DATE}.log 2>&1 && echo "   │  ✅ Build OK" || echo "   │  ❌ Build FAILED"
    
    echo "   ├─ Tests..."
    cargo test > ../docs/ORIGIN/error-logs/chat-tests-${DATE}.log 2>&1 && echo "   │  ✅ Tests OK" || echo "   │  ❌ Tests FAILED"
    
    echo "   └─ Clippy..."
    cargo clippy --all-targets --all-features -- -D warnings > ../docs/ORIGIN/error-logs/chat-clippy-${DATE}.log 2>&1 && echo "      ✅ Clippy OK" || echo "      ❌ Clippy found issues"
    
    cd ..
else
    echo "💬 3/4 - Chat Server Rust... ⏭️  (non trouvé)"
fi
echo ""

# ===========================================
# STREAM SERVER RUST (si existe)
# ===========================================
if [ -d "veza-stream-server" ]; then
    echo "🎵 4/4 - Analyse du Stream Server Rust..."
    echo "   ├─ Build..."
    cd veza-stream-server
    cargo build --release > ../docs/ORIGIN/error-logs/stream-build-${DATE}.log 2>&1 && echo "   │  ✅ Build OK" || echo "   │  ❌ Build FAILED"
    
    echo "   ├─ Tests..."
    cargo test > ../docs/ORIGIN/error-logs/stream-tests-${DATE}.log 2>&1 && echo "   │  ✅ Tests OK" || echo "   │  ❌ Tests FAILED"
    
    echo "   └─ Clippy..."
    cargo clippy --all-targets --all-features -- -D warnings > ../docs/ORIGIN/error-logs/stream-clippy-${DATE}.log 2>&1 && echo "      ✅ Clippy OK" || echo "      ❌ Clippy found issues"
    
    cd ..
else
    echo "🎵 4/4 - Stream Server Rust... ⏭️  (non trouvé)"
fi
echo ""

# ===========================================
# DOCKER
# ===========================================
echo "🐳 Analyse Docker..."
echo "   ├─ docker-compose.yml syntax..."
if command -v docker-compose &> /dev/null; then
    docker-compose config > docs/ORIGIN/error-logs/docker-compose-${DATE}.log 2>&1 && echo "   │  ✅ Syntax OK" || echo "   │  ❌ Syntax FAILED"
else
    echo "   │  ⚠️  docker-compose non disponible (skip)"
fi

echo "   └─ Docker daemon..."
if docker ps &> /dev/null; then
    echo "      ✅ Docker daemon running"
else
    echo "      ❌ Docker daemon NOT running"
fi
echo ""

# ===========================================
# GÉNÉRATION DU RÉSUMÉ
# ===========================================
echo "📊 Génération du résumé..."
./scripts/generate-error-summary.sh ${DATE}

echo ""
echo "✅ Découverte terminée!"
echo ""
echo "📁 Logs sauvegardés dans: docs/ORIGIN/error-logs/"
echo "📄 Résumé: docs/ORIGIN/error-logs/summary-${DATE}.md"
echo ""
echo "🔍 Prochaines étapes:"
echo "   1. Consulter le résumé: cat docs/ORIGIN/error-logs/summary-${DATE}.md"
echo "   2. Mettre à jour ORIGIN_ERROR_REGISTRY.md"
echo "   3. Créer les tâches TERR-XXX dans ORIGIN_IMPLEMENTATION_TASKS.md"
echo "   4. Commencer les corrections par ordre de priorité (P0 > P1 > P2)"
echo ""

