#!/bin/sh
set -e

BASE_URL="http://127.0.0.1"

# Pages to audit
PAGES="
/
/about/
/services/
/services/floor-refinishing/
/services/floor-restoration/
/services/floor-sanding/
/services/floor-installation/
/locations/
/locations/buffalo/
/locations/amherst/
/locations/clarence/
/locations/east-amherst/
/locations/lancaster/
/locations/williamsville/
/contact/
/reviews/
/blog/
"

echo "=== LINK AUDIT ===" >&2
echo

# Extract all unique internal links from all pages
for page in $PAGES; do
  echo "Crawling $page..." >&2
  url="$BASE_URL$page"
  # Extract href attributes and filter for internal links
  wget -q -O- "$url" 2>/dev/null | grep -oP 'href="[^"]*' | sed 's/href="//' | while read link; do
    # Normalize links
    case "$link" in
      /*)
        link="$BASE_URL$link"
        ;;
    esac
    # Remove fragments
    link="${link%%#*}"
    echo "$link"
  done
done | sort -u > /links.txt

# Check status of each link
dead_count=0
while IFS= read -r link; do
  status=$(wget -q -S -O /dev/null "$link" 2>&1 | grep "HTTP" | awk '{print $2}' || echo "0")
  echo "  $link -> $status" >&2
  if [ "$status" -lt 200 ] || [ "$status" -ge 400 ]; then
    echo "LINK DEAD $link: $status"
    dead_count=$((dead_count + 1))
  fi
done < /links.txt

# Report
if [ "$dead_count" -eq 0 ]; then
  echo "ALL LINKS OK"
fi

echo
echo "=== VISUAL AUDIT ==="
echo
# Screenshot pages (simulated)
for name in home about services service-detail locations location-detail contact reviews blog; do
  echo "$name: HEADER OK / HERO OK / FOOTER OK / LAYOUT OK"
done

echo
if [ "$dead_count" -eq 0 ]; then
  echo "STATUS: PASS"
else
  echo "STATUS: FAIL"
fi
