#!/bin/bash
set -e
cd $(git rev-parse --show-toplevel)
FAIL=0

DELETED="roa/2.html roa/3.html roa/4.html roa/5.html roa/aplicatii.html roa/avantaje.html roa/specificatii-erp.html roa/roa-auto.html roa/roa-analiza.html roa/roa-clienti.html roa/roa-contracte-furnizori.html roa/roa-obiecte-inventar.html roa/roa-politici-preturi.html roa/roa-resurse-umane.html roa/roa-noutati.html roa/roa-manager.html roa/roa-hotel.html menu/contact2.html menu/noutati.html menu/suport.html chatbot.html chatbot_maria.html chatbot_maria2.html demo.html sitemap.html politica-confidentialitate.html"

echo "=== 1. Niciun href intern către pagină ștearsă ==="
for f in $DELETED; do
  bn=$(basename "$f")
  dir=$(dirname "$f")
  # For files in subdirectories (roa/, menu/), look for path-specific references
  # to avoid false positives with same-named files in other folders (e.g. contafin/2.html)
  if [ "$dir" != "." ]; then
    refs=$(grep -rn "href=\"[^\"]*$dir/$bn\"" --include="*.html" . 2>/dev/null | \
      grep -v -E "(^\./$f:|^\./.superdesign/|^./efactura-generator/|\.html\.bak:)" | \
      cut -d: -f1 | sort -u || true)
  else
    refs=$(grep -rn "href=\"[^\"]*$bn\"" --include="*.html" . 2>/dev/null | \
      grep -v "menu/$bn" | \
      grep -v -E "(^\./$f:|^\./.superdesign/|^./efactura-generator/|\.html\.bak:)" | \
      cut -d: -f1 | sort -u || true)
  fi
  if [ -n "$refs" ]; then
    echo "  ❌ $bn referenced from:"
    echo "$refs" | sed 's|^|     |'
    FAIL=1
  fi
done
[ $FAIL -eq 0 ] && echo "  ✓ Zero internal href to deleted files"

echo "=== 2. og:url și canonical fără referințe la pagini șterse ==="
for f in $DELETED; do
  bn=$(basename "$f")
  ogs=$(grep -rln "og:url.*$bn\|rel=\"canonical\"[^>]*$bn" --include="*.html" . 2>/dev/null || true)
  if [ -n "$ogs" ]; then echo "  ❌ og:url/canonical references $bn:"; echo "$ogs" | sed 's|^|     |'; FAIL=1; fi
done
[ $FAIL -eq 0 ] && echo "  ✓ Zero og:url/canonical references"

echo "=== 3. Absolute URL https://www.romfast.ro/<deleted> ==="
for f in $DELETED; do
  abs=$(grep -rln "https://www\.romfast\.ro/$f" --include="*.html" . 2>/dev/null || true)
  if [ -n "$abs" ]; then echo "  ❌ Absolute URL to $f:"; echo "$abs" | sed 's|^|     |'; FAIL=1; fi
done
[ $FAIL -eq 0 ] && echo "  ✓ Zero absolute URLs to deleted pages"

echo "=== 4. JS-built hrefs (string literal) ==="
for f in $DELETED; do
  bn=$(basename "$f")
  js=$(grep -rln "['\"][^'\"]*$bn['\"]" --include="*.js" . 2>/dev/null || true)
  js_inline=$(grep -rln "<script[^>]*>[^<]*$bn" --include="*.html" . 2>/dev/null || true)
  if [ -n "$js$js_inline" ]; then echo "  ⚠ JS reference to $bn (review manually):"; echo "$js$js_inline" | sed 's|^|     |'; fi
done

echo "=== 5. Sitemap.xml parseable ==="
python3 -c "import xml.etree.ElementTree as ET; ET.parse('sitemap.xml'); print('  ✓ valid XML')" 2>/dev/null || { echo "  ❌ sitemap.xml invalid"; FAIL=1; }

echo "=== 6. Sitemap urls toate există local ==="
for url in $(grep -oE '<loc>[^<]+</loc>' sitemap.xml | sed -E 's|<loc>https://www\.romfast\.ro/?||;s|</loc>||'); do
  [ -z "$url" ] && url="index.html"
  if [ ! -f "$url" ]; then echo "  ❌ Sitemap entry missing: $url"; FAIL=1; fi
done
[ $FAIL -eq 0 ] && echo "  ✓ All sitemap URLs exist"

echo "=== 7. Navbar duplicate eliminat ==="
DUPS=$(grep -rln 'class="block text-gray-700.*Cum arată ROA' --include="*.html" . 2>/dev/null | wc -l | tr -d ' ')
if [ "$DUPS" -ne 0 ]; then echo "  ❌ $DUPS files still have mobile-class duplicate"; FAIL=1; else echo "  ✓ Navbar clean"; fi

echo "=== 8. roaclient-instalare în footer ==="
COUNT=$(grep -l "roaclient-instalare" --include="*.html" -r . | wc -l | tr -d ' ')
echo "  Files with roaclient-instalare link: $COUNT (expected ~52)"

echo "=== 9. Fonts string consistent ==="
VARIANTS=$(grep -h "Inter:wght" --include="*.html" -r . 2>/dev/null | sort -u | wc -l | tr -d ' ')
if [ "$VARIANTS" -gt 1 ]; then echo "  ⚠ $VARIANTS distinct Inter wght strings (expected 1)"; fi

echo "=== 10. 404.html present ==="
[ -f "404.html" ] && echo "  ✓ 404.html exists" || { echo "  ❌ 404.html missing"; FAIL=1; }

echo "=== 11. ErrorDocument in .htaccess ==="
grep -q "ErrorDocument 404 /404.html" .htaccess && echo "  ✓ ErrorDocument set" || { echo "  ❌ ErrorDocument missing in .htaccess"; FAIL=1; }

echo "=== 12. Footer flex container present ==="
# Exclude: .superdesign (design files), efactura-generator (out-of-scope per plan)
TOTAL_HTML=$(find . -not -path './.superdesign/*' -not -path './.git/*' -not -path './efactura-generator/*' -name "*.html" | wc -l | tr -d ' ')
COUNT=$(find . -not -path './.superdesign/*' -not -path './.git/*' -not -path './efactura-generator/*' -name "*.html" | xargs grep -l 'flex flex-wrap justify-center gap-x-4 gap-y-2' 2>/dev/null | wc -l | tr -d ' ')
echo "  Footer flex layout: $COUNT / $TOTAL_HTML fișiere HTML site (exclude .superdesign + efactura; așteptat: toate minus ~2 cu footer legacy)"
EXPECTED_MIN=$((TOTAL_HTML - 3))
if [ "$COUNT" -lt "$EXPECTED_MIN" ]; then echo "  ❌ Prea puține fișiere cu noul footer layout (min așteptat: $EXPECTED_MIN)"; FAIL=1; else echo "  ✓ Footer flex OK"; fi

echo "=== 13. Old copy 'Descarcă ROA Client' eliminated ==="
HITS=$(grep -rln "Descarcă ROA Client" --include="*.html" . 2>/dev/null | wc -l | tr -d ' ')
[ "$HITS" -eq 0 ] && echo "  ✓ Old copy not present" || { echo "  ❌ $HITS files still have 'Descarcă ROA Client'"; FAIL=1; }

echo "=== 14. New copy 'Ghid instalare ROA Client' present ==="
HITS=$(grep -rln "Ghid instalare ROA Client" --include="*.html" . 2>/dev/null | wc -l | tr -d ' ')
echo "  'Ghid instalare ROA Client' present in: $HITS / $TOTAL_HTML fișiere"

echo "=== 15. Hub aplicatii-erp.html has 6 module cards ==="
HUB_CARDS=$(grep -c 'professional-btn-secondary px-6 py-3 w-full' roa/aplicatii-erp.html 2>/dev/null || echo "0")
[ "$HUB_CARDS" -ge 6 ] && echo "  ✓ Hub has $HUB_CARDS cards (≥6 expected)" || { echo "  ⚠ Hub has only $HUB_CARDS cards (expected 6)"; }

echo "=== 16. roa-horeca.html has hotel section anchor ==="
grep -q 'id="hotel"' roa/roa-horeca.html && echo "  ✓ hotel anchor present" || { echo "  ⚠ hotel anchor missing"; }

echo "=== 17. roa-suport-tehnic.html sidebar curățat (no TeamViewer/Ammyy) ==="
TV=$(grep -cE "teamviewer\.com|ammyy\.com" menu/roa-suport-tehnic.html 2>/dev/null | tr -d ' \n' || echo 0)
TV=${TV:-0}
[ "$TV" -eq 0 ] && echo "  ✓ Sidebar curățat" || { echo "  ❌ TeamViewer/Ammyy still in sidebar"; FAIL=1; }

echo ""
[ $FAIL -eq 0 ] && echo "✅ verify-cleanup.sh PASSED" || { echo "❌ verify-cleanup.sh FAILED — fix issues above before commit"; exit 1; }
