#!/bin/bash
# Frontend design-system lint guards.
# See docs/design-system.md "Inline-style rules" and docs/ux-audit/remediation-plan.md Step 10.
# Exit 0 = clean. Exit non-zero = violations found (printed with file:line).

set -u
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
FRONTEND="$ROOT/src-tauri/frontend"
SRC_JS="$FRONTEND/js"
SRC_HTML="$FRONTEND/index.html $FRONTEND/compose.html"
SRC_CSS="$FRONTEND/css/styles.css"

violations=0

report() {
    local rule="$1"; shift
    local msg="$1"; shift
    if [ -n "$*" ]; then
        echo
        echo "[$rule] $msg"
        echo "$*"
        violations=$((violations + 1))
    fi
}

# 1. No raw hex literals in JS or source HTML (HTML entities &#NNNN; are OK;
#    <meta name="theme-color"> is a documented exception — value is set
#    programmatically by js/themes.js to track the active theme).
hits=$(grep -rnE '#[0-9a-fA-F]{3,8}\b' "$SRC_JS" $SRC_HTML 2>/dev/null \
    | grep -vE '&#[0-9]+;' \
    | grep -vE 'meta name="theme-color"' \
    || true)
report "no-raw-hex" "Raw hex literal in JS/HTML — use a CSS class or token instead." "$hits"

# 2. No style.cssText anywhere in JS.
hits=$(grep -rn 'cssText' "$SRC_JS" 2>/dev/null || true)
report "no-csstext" "style.cssText injection — move styles into a CSS class." "$hits"

# 3. No var(--token, #fallback). Fallback hex defeats theme switching.
hits=$(grep -rnE 'var\(--[a-z-]+,\s*#' "$FRONTEND" --include='*.js' --include='*.html' --include='styles.css' 2>/dev/null | grep -v styles.min.css || true)
report "no-var-fallback-hex" "var(--token, #fallback) — drop the fallback; it bypasses themes." "$hits"

# 4. No window.confirm / bare confirm() — route through GoingsOn.ui.showConfirmDialog.
hits=$(grep -rnE '\b(window\.)?confirm\(' "$SRC_JS" 2>/dev/null | grep -vE 'showConfirmDialog|confirmDelete|//\s*\*|\*\s' || true)
report "no-window-confirm" "window.confirm() — use GoingsOn.ui.showConfirmDialog instead." "$hits"

# 5. No inline style= that touches color / background / border / shadow / font / padding values.
hits=$(grep -rnE 'style="[^"]*(color|background|border|shadow|font-size|font-family|padding)' "$SRC_JS" $SRC_HTML 2>/dev/null || true)
report "no-styled-attrs" "Inline style= with color/background/border/shadow/font/padding — use a class." "$hits"

# 6. Deprecated empty-state classes have been removed.
hits=$(grep -rnE 'empty-dashboard-list|kanban-empty|virtual-scroller-empty' "$FRONTEND" --include='*.js' --include='*.html' --include='styles.css' 2>/dev/null | grep -v styles.min.css || true)
report "no-deprecated-empty-states" "Deprecated class — use .empty-state with --compact / --dashboard / --error." "$hits"

# 7. No native browser dialogs. Charter rule from Phase 7 roll-up.
hits=$(grep -rnE '\b(window\.)?(confirm|prompt|alert)\(' "$SRC_JS" 2>/dev/null \
    | grep -vE 'showConfirmDialog|showPromptDialog|confirmDelete|//\s|\*\s' || true)
report "no-native-dialogs" "window.confirm/prompt/alert are banned — use GoingsOn.ui.show{Confirm,Prompt}Dialog or showToast." "$hits"

if [ $violations -eq 0 ]; then
    echo "frontend lint: clean"
    exit 0
else
    echo
    echo "frontend lint: $violations rule(s) failed"
    exit 1
fi
