#!/bin/bash
# CE UI Quality Gate — Validate any HTML file against CE brand standards
# Usage: bash validate.sh <file.html>

set -e

if [ -z "$1" ]; then
  echo "Usage: bash validate.sh <file.html>"
  exit 1
fi

FILE="$1"
ERRORS=0
WARNINGS=0

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

echo "╔══════════════════════════════════════╗"
echo "║   CE UI QUALITY GATE                 ║"
echo "╚══════════════════════════════════════╝"
echo ""
echo "File: $FILE"
echo ""

# === BANNED PATTERNS ===
echo "── BANNED PATTERNS ──────────────────"

if grep -n -i "gradient" "$FILE" | grep -v "^[[:space:]]*#\|^[[:space:]]*//\|^[[:space:]]*<!--" | head -3; then
  echo "  ⛔ GRADIENT found — CE uses solid colors only"
  ERRORS=$((ERRORS + 1))
else
  echo "  ✅ No gradients"
fi

if grep -n "box-shadow" "$FILE" | head -3; then
  echo "  ⛔ BOX-SHADOW found — CE doesn't use shadows"
  ERRORS=$((ERRORS + 1))
else
  echo "  ✅ No box-shadows"
fi

if grep -n "font-weight.*\(600\|700\|800\|900\|bold\)" "$FILE" | head -3; then
  echo "  ⛔ BOLD/600+ weight found — CE max is 500"
  ERRORS=$((ERRORS + 1))
else
  echo "  ✅ No bold weights"
fi

if grep -n "#e5e5e5" "$FILE" | head -3; then
  echo "  ⛔ WRONG BORDER COLOR #e5e5e5 — should be #eeeeee"
  ERRORS=$((ERRORS + 1))
else
  echo "  ✅ Border color OK"
fi

if grep -n "border-radius.*\([5-9]px\|[1-9][0-9]px\|[0-9]*rem\)" "$FILE" | grep -v "0px\|1px\|2px\|3px\|4px" | head -3; then
  echo "  ⛔ LARGE BORDER-RADIUS — CE max is 4px"
  ERRORS=$((ERRORS + 1))
else
  echo "  ✅ Border radius OK"
fi

if grep -n "border.*[2-9]px solid\|border.*[1-9][0-9]px solid" "$FILE" | head -3; then
  echo "  ⛔ THICK BORDER — CE uses 1px only"
  ERRORS=$((ERRORS + 1))
else
  echo "  ✅ Border thickness OK"
fi

# Check for non-CE colors (common AI defaults)
if grep -n "#[0-9a-fA-F]\{6\}" "$FILE" | grep -v "cc0000\|1a1a1a\|666666\|999999\|eeeeee\|ffffff\|000000\|fff\|eee" | grep -i "#[0-9a-f]\{6\}" | head -5; then
  echo "  ⚠️  NON-CE COLORS detected — review above (may be OK for client work)"
  WARNINGS=$((WARNINGS + 1))
fi

echo ""

# === REQUIRED PATTERNS ===
echo "── REQUIRED PATTERNS ────────────────"

if grep -q "ffj8sbd" "$FILE"; then
  echo "  ✅ Typekit loaded (Larken)"
else
  echo "  ⛔ MISSING Typekit link for Larken — add: <link rel=\"stylesheet\" href=\"https://use.typekit.net/ffj8sbd.css\">"
  ERRORS=$((ERRORS + 1))
fi

if grep -q "cc0000\|--red" "$FILE"; then
  echo "  ✅ CE red accent present"
else
  echo "  ⚠️  No CE red (#cc0000) found"
  WARNINGS=$((WARNINGS + 1))
fi

if grep -q "1a1a1a\|--black" "$FILE"; then
  echo "  ✅ CE text color present"
else
  echo "  ⚠️  No CE black (#1a1a1a) found — using default black?"
  WARNINGS=$((WARNINGS + 1))
fi

if grep -q "larken\|Larken" "$FILE"; then
  echo "  ✅ Larken font referenced"
else
  echo "  ⛔ MISSING Larken — headings must use Larken"
  ERRORS=$((ERRORS + 1))
fi

if grep -q "Inter" "$FILE"; then
  echo "  ✅ Inter font referenced"
else
  echo "  ⛔ MISSING Inter — body text must use Inter"
  ERRORS=$((ERRORS + 1))
fi

if grep -q "1100px" "$FILE"; then
  echo "  ✅ Container max-width: 1100px"
else
  echo "  ⚠️  Container max-width not 1100px — check layout"
  WARNINGS=$((WARNINGS + 1))
fi

echo ""

# === SUMMARY ===
echo "── SUMMARY ──────────────────────────"
echo "  Errors:   $ERRORS"
echo "  Warnings: $WARNINGS"
echo ""

if [ $ERRORS -eq 0 ]; then
  echo "  ✅ PASSED — Ready for review"
  exit 0
else
  echo "  ⛔ FAILED — Fix $ERRORS error(s) before shipping"
  exit 1
fi
