#!/bin/bash
# Verify AS4 Settlement Setup
# Checks all prerequisites and configuration

set -e

echo "========================================="
echo "AS4 Settlement Setup Verification"
echo "========================================="

cd "$(dirname "$0")/.."

ERRORS=0
WARNINGS=0

# Check Node.js
echo ""
echo "1. Checking Node.js..."
if command -v node &> /dev/null; then
    NODE_VERSION=$(node --version)
    echo "   ✓ Node.js installed: $NODE_VERSION"
    if [[ $(echo "$NODE_VERSION" | cut -d'v' -f2 | cut -d'.' -f1) -lt 18 ]]; then
        echo "   ⚠ Warning: Node.js 18+ recommended"
        ((WARNINGS++))
    fi
else
    echo "   ✗ Node.js not found"
    ((ERRORS++))
fi

# Check PostgreSQL
echo ""
echo "2. Checking PostgreSQL..."
if command -v psql &> /dev/null; then
    PSQL_VERSION=$(psql --version | head -1)
    echo "   ✓ PostgreSQL installed: $PSQL_VERSION"
    
    # Test connection
    if [ -n "$DATABASE_URL" ]; then
        if psql "$DATABASE_URL" -c "SELECT 1" &> /dev/null; then
            echo "   ✓ Database connection successful"
        else
            echo "   ⚠ Warning: Database connection failed"
            ((WARNINGS++))
        fi
    else
        echo "   ⚠ Warning: DATABASE_URL not set"
        ((WARNINGS++))
    fi
else
    echo "   ✗ PostgreSQL not found"
    ((ERRORS++))
fi

# Check Redis
echo ""
echo "3. Checking Redis..."
if command -v redis-cli &> /dev/null; then
    REDIS_VERSION=$(redis-cli --version | head -1)
    echo "   ✓ Redis installed: $REDIS_VERSION"
    
    # Test connection
    if redis-cli ping &> /dev/null; then
        echo "   ✓ Redis connection successful"
    else
        echo "   ⚠ Warning: Redis connection failed (may not be running)"
        ((WARNINGS++))
    fi
else
    echo "   ⚠ Warning: Redis not found (optional for development)"
    ((WARNINGS++))
fi

# Check Prisma
echo ""
echo "4. Checking Prisma..."
if [ -f "node_modules/.bin/prisma" ]; then
    PRISMA_VERSION=$(npx prisma --version | head -1)
    echo "   ✓ Prisma installed: $PRISMA_VERSION"
else
    echo "   ✗ Prisma not found - run: npm install"
    ((ERRORS++))
fi

# Check certificates
echo ""
echo "5. Checking Certificates..."
if [ -f ".env" ]; then
    source .env 2>/dev/null || true
    
    if [ -n "$AS4_TLS_CERT_PATH" ] && [ -f "$AS4_TLS_CERT_PATH" ]; then
        echo "   ✓ TLS certificate found"
    else
        echo "   ⚠ Warning: TLS certificate not found - run: ./scripts/generate-as4-certificates.sh"
        ((WARNINGS++))
    fi
    
    if [ -n "$AS4_SIGNING_CERT_PATH" ] && [ -f "$AS4_SIGNING_CERT_PATH" ]; then
        echo "   ✓ Signing certificate found"
    else
        echo "   ⚠ Warning: Signing certificate not found"
        ((WARNINGS++))
    fi
else
    echo "   ⚠ Warning: .env file not found"
    ((WARNINGS++))
fi

# Check database models
echo ""
echo "6. Checking Database Models..."
if grep -q "model As4Member" prisma/schema.prisma; then
    echo "   ✓ AS4 models defined in schema"
else
    echo "   ✗ AS4 models not found in schema"
    ((ERRORS++))
fi

# Check routes
echo ""
echo "7. Checking Route Registration..."
if grep -q "as4GatewayRoutes" src/integration/api-gateway/app.ts; then
    echo "   ✓ AS4 routes registered in app.ts"
else
    echo "   ✗ AS4 routes not registered"
    ((ERRORS++))
fi

# Check migration file
echo ""
echo "8. Checking Migration File..."
if [ -f "prisma/migrations/20260119000000_add_as4_settlement_models/migration.sql" ]; then
    echo "   ✓ Migration file exists"
else
    echo "   ⚠ Warning: Migration file not found"
    ((WARNINGS++))
fi

# Check seed script
echo ""
echo "9. Checking Seed Script..."
if [ -f "scripts/seed-as4-settlement-marketplace-offering.ts" ]; then
    echo "   ✓ Seed script exists"
else
    echo "   ✗ Seed script not found"
    ((ERRORS++))
fi

# Summary
echo ""
echo "========================================="
echo "Verification Summary"
echo "========================================="
echo "Errors: $ERRORS"
echo "Warnings: $WARNINGS"
echo ""

if [ $ERRORS -eq 0 ]; then
    echo "✓ Setup verification passed!"
    if [ $WARNINGS -gt 0 ]; then
        echo "⚠ Some warnings found (non-blocking)"
    fi
    exit 0
else
    echo "✗ Setup verification failed - fix errors above"
    exit 1
fi
