#!/bin/bash

# Test script to verify deployment configuration
# This script should be run before committing deployment changes

set -e

echo "🚀 Testing Docusaurus deployment configuration..."

# Test TypeScript compilation
echo "📝 Running TypeScript check..."
npm run typecheck

# Test build (allow warnings for broken links in incomplete docs)
echo "🔨 Building site..."
npm run build || {
    echo "⚠️  Build completed with warnings (this is expected for incomplete documentation)"
    if [ -d "build" ]; then
        echo "✅ Build directory created successfully"
    else
        echo "❌ Build failed - no build directory created"
        exit 1
    fi
}

# Verify critical files exist in build output
echo "🔍 Verifying build output..."

# Check for CNAME file
if [ -f "build/CNAME" ]; then
    echo "✅ CNAME file found in build output"
    echo "   Content: $(cat build/CNAME)"
else
    echo "❌ CNAME file missing from build output"
    exit 1
fi

# Check for index.html
if [ -f "build/index.html" ]; then
    echo "✅ Index page generated"
else
    echo "❌ Index page missing"
    exit 1
fi

# Check for sitemap
if [ -f "build/sitemap.xml" ]; then
    echo "✅ Sitemap generated"
else
    echo "❌ Sitemap missing"
    exit 1
fi

# Check for proper asset handling
if [ -d "build/assets" ]; then
    echo "✅ Assets directory exists"
else
    echo "❌ Assets directory missing"
    exit 1
fi

# Check for robots.txt
if [ -f "build/robots.txt" ]; then
    echo "✅ Robots.txt generated"
else
    echo "⚠️  Robots.txt missing (this is optional)"
fi

# Verify configuration
echo "🔧 Verifying configuration..."
echo "   Organization: TrainLoop"
echo "   Project: evals"
echo "   URL: https://evals.docs.trainloop.ai"
echo "   Base URL: /"

# Check build size
BUILD_SIZE=$(du -sh build | cut -f1)
echo "📦 Build size: $BUILD_SIZE"

echo "✅ All deployment configuration tests passed!"
echo "📋 Ready for GitHub Pages deployment"

# Optional: Start local server for manual testing
read -p "🌐 Start local server for testing? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "Starting local server at http://localhost:3000..."
    npm run serve
fi