#!/bin/bash
# validate-links.sh - Check all <a href> links in a PHP/HTML file for broken URLs
# Usage: ./validate-links.sh path/to/index.php
#
# IMPORTANT: 403 warnings (especially x.com) MUST be verified with WebSearch!
# X.com blocks bots but also returns 403 for INVALID URLs.
# Always search to confirm the URL is correct before marking complete.

set -e

if [ -z "$1" ]; then
    echo "Usage: ./validate-links.sh <path-to-html-or-php-file>"
    exit 1
fi

FILE="$1"

if [ ! -f "$FILE" ]; then
    echo "Error: File not found: $FILE"
    exit 1
fi

echo "Validating links in: $FILE"
echo "================================"

# Extract only <a href="..."> URLs (not <link href> for stylesheets/preconnect)
URLS=$(sed -n 's/.*<a [^>]*href="\([^"]*\)".*/\1/gp' "$FILE" | grep -E '^https?://' | sort -u)

if [ -z "$URLS" ]; then
    echo "No external URLs found in <a> tags."
    exit 0
fi

# User agent to avoid bot detection
USER_AGENT="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"

TOTAL=0
BROKEN=0
WARNINGS=0
BROKEN_LIST=""
WARNING_LIST=""

while IFS= read -r url; do
    TOTAL=$((TOTAL + 1))

    # Get HTTP status code with browser user-agent
    STATUS=$(curl -sI -L -o /dev/null -w "%{http_code}" \
        -A "$USER_AGENT" \
        --max-time 10 \
        "$url" 2>/dev/null || echo "000")

    case "$STATUS" in
        200|301|302|303|307|308)
            echo "[OK $STATUS] $url"
            ;;
        401|404|000)
            # Definitely broken - 401 unauthorized/not found, 404 not found, 000 connection failed
            echo "[BROKEN $STATUS] $url"
            BROKEN=$((BROKEN + 1))
            BROKEN_LIST="$BROKEN_LIST\n- $url (status: $STATUS)"
            ;;
        403)
            # Possibly bot protection - warn but verify manually
            echo "[WARN $STATUS] $url (may be bot protection)"
            WARNINGS=$((WARNINGS + 1))
            WARNING_LIST="$WARNING_LIST\n- $url"
            ;;
        *)
            echo "[BROKEN $STATUS] $url"
            BROKEN=$((BROKEN + 1))
            BROKEN_LIST="$BROKEN_LIST\n- $url (status: $STATUS)"
            ;;
    esac
done <<< "$URLS"

echo "================================"
echo "Total: $TOTAL links"
echo "OK: $((TOTAL - BROKEN - WARNINGS))"
echo "Broken: $BROKEN"
echo "Warnings: $WARNINGS"

if [ "$BROKEN" -gt 0 ]; then
    echo ""
    echo "BROKEN LINKS (must fix):"
    echo -e "$BROKEN_LIST"
fi

if [ "$WARNINGS" -gt 0 ]; then
    echo ""
    echo "WARNINGS (MUST verify with WebSearch):"
    echo -e "$WARNING_LIST"
    echo ""
    echo "IMPORTANT: 403 warnings (especially x.com/twitter) MUST be verified!"
    echo "Use WebSearch to confirm the URL is correct. X.com blocks bots but"
    echo "may also return 403 for INVALID URLs. Always search to verify."
fi

if [ "$BROKEN" -gt 0 ]; then
    echo ""
    echo "ACTION: Use WebSearch to find correct URLs for broken links."
    exit 1
elif [ "$WARNINGS" -gt 0 ]; then
    echo ""
    echo "ACTION: Use WebSearch to verify all 403 warnings before marking complete."
    exit 1
else
    echo ""
    echo "All links validated successfully."
    exit 0
fi
