#!/bin/bash
# =====================================================================
# update-all.sh - Run all update scripts
# =====================================================================
# This script runs all update scripts in the correct order:
# 1. Generate sitemap.xml
# 2. Update navigation menu from sitemap
# 3. Update stories page from sitemap
# 4. Update CSP hashes
# 5. Apply accessibility fixes
# =====================================================================

set -e

echo "=== Running all update scripts ==="

# Get the directory of this script
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"

# Generate sitemap.xml
echo "=== Generating sitemap.xml ==="
if [ -f "$SCRIPT_DIR/generate-sitemap.sh" ]; then
  "$SCRIPT_DIR/generate-sitemap.sh"
else
  echo "⚠️ generate-sitemap.sh not found, skipping"
fi

# Update navigation from sitemap
echo "=== Updating navigation from sitemap ==="
if [ -f "$SCRIPT_DIR/update-nav-from-sitemap.js" ]; then
  node "$SCRIPT_DIR/update-nav-from-sitemap.js"
else
  echo "⚠️ update-nav-from-sitemap.js not found, skipping"
fi

# Update stories page from sitemap
echo "=== Updating stories page from sitemap ==="
if [ -f "$SCRIPT_DIR/update-stories-page.js" ]; then
  node "$SCRIPT_DIR/update-stories-page.js"
else
  echo "⚠️ update-stories-page.js not found, skipping"
fi

# Update CSP hashes
echo "=== Updating CSP hashes ==="
if [ -f "$SCRIPT_DIR/update-csp-hashes.sh" ]; then
  "$SCRIPT_DIR/update-csp-hashes.sh"
else
  echo "⚠️ update-csp-hashes.sh not found, skipping"
fi

# Ensure accessibility fixes are applied
echo "=== Ensuring accessibility fixes are applied ==="
# Check if stories.css exists
if [ -f "$SCRIPT_DIR/stories/stories.css" ]; then
    # Check if the file already has the accessibility fixes
    if ! grep -q "color: #004494;" "$SCRIPT_DIR/stories/stories.css"; then
        echo "Applying accessibility fixes to stories.css..."
        # Use sed to add the color property to story-nav-link class
        sed -i '' 's/\.story-nav-link {/\.story-nav-link {\n    color: #004494; \/* Darker blue for 7:1+ contrast ratio *\//g' "$SCRIPT_DIR/stories/stories.css"
        sed -i '' 's/\.story-nav-link:hover {/\.story-nav-link:hover {\n    color: #003366; \/* Even darker on hover for better visibility *\//g' "$SCRIPT_DIR/stories/stories.css"
        
        # Update placeholder-notice links
        sed -i '' 's/\.placeholder-notice a {/\.placeholder-notice a {\n    color: #004494; \/* Darker blue for 7:1+ contrast ratio *\//g' "$SCRIPT_DIR/stories/stories.css"
        
        # Add hover state for placeholder-notice links if it doesn't exist
        if ! grep -q "\.placeholder-notice a:hover" "$SCRIPT_DIR/stories/stories.css"; then
            echo -e "\n.placeholder-notice a:hover {\n    color: #003366; /* Even darker on hover */\n}" >> "$SCRIPT_DIR/stories/stories.css"
        fi
        
        echo "Accessibility fixes applied to stories.css"
    else
        echo "Accessibility fixes already present in stories.css"
    fi
else
    echo "⚠️ stories/stories.css not found, skipping accessibility fixes"
fi

# Generate PDFs (this is now done in Dockerfile, but kept here for manual runs)
echo "=== PDF generation ==="
echo "Note: PDFs are generated during Docker build. Skipping in update-all.sh"
echo "To generate PDFs manually, run: npm run generate-pdfs"

echo "=== All updates completed successfully ==="
echo "To apply changes, restart the server using: ./caddy.sh"
