#!/bin/bash

# Simple validation script for ghost content extraction
echo "🔍 Validating ghost profile implementation..."

# Check if required files exist
echo "📁 Checking required files..."
if [ ! -f "_quarto.yml" ]; then
    echo "❌ _quarto.yml not found"
    exit 1
fi

if [ ! -f "ghost-iframe.css" ]; then
    echo "❌ ghost-iframe.css not found"
    exit 1
fi

if [ ! -f "ghost-upload/src/main.rs" ]; then
    echo "❌ Rust source not found"
    exit 1
fi

echo "✅ All required files present"

# Check if ghost profile is defined in _quarto.yml
echo "📋 Checking ghost profile configuration..."
if grep -q "ghost:" _quarto.yml; then
    echo "✅ Ghost profile found in _quarto.yml"
else
    echo "❌ Ghost profile not found in _quarto.yml"
    exit 1
fi

# Check if GitLab CI builds both versions
echo "🔧 Checking GitLab CI configuration..."
if grep -q "ghost-content" .gitlab-ci.yml; then
    echo "✅ GitLab CI configured for dual output"
else
    echo "❌ GitLab CI not configured for ghost-content"
    exit 1
fi

# Check if Rust code has extract_article_content function
echo "🦀 Checking Rust implementation..."
if grep -q "extract_article_content" ghost-upload/src/main.rs; then
    echo "✅ Content extraction function found"
else
    echo "❌ Content extraction function not found"
    exit 1
fi

# Check if force update functionality is available
if grep -q "FORCE_UPDATE" ghost-upload/src/main.rs; then
    echo "✅ Force update functionality found"
else
    echo "❌ Force update functionality not found"
    exit 1
fi

# Check if manual CI job is configured
if grep -q "force-update-ghost" ghost-upload/.gitlab-ci.yml; then
    echo "✅ Manual force update CI job found"
else
    echo "❌ Manual force update CI job not found"
    exit 1
fi

# Verify Rust code compiles
echo "🛠️  Building Rust code..."
cd ghost-upload
if cargo check --quiet; then
    echo "✅ Rust code compiles successfully"
else
    echo "❌ Rust compilation failed"
    exit 1
fi

cd ..
echo ""
echo "🎉 All validations passed!"
echo "📋 Summary of changes:"
echo "   • Quarto profiles for dual-output rendering"
echo "   • Ghost-optimized CSS styling"
echo "   • GitLab CI builds both main site and ghost-content"
echo "   • Rust extracts HTML content instead of using iframes"
echo "   • Force update mode to refresh existing posts"
echo "   • Manual CI trigger for content updates"
echo ""
echo "🚀 Ready for testing in CI/CD pipeline!"