#!/usr/bin/env bash
# ─────────────────────────────────────────────────────────────
# icon-swap-check.sh — Enforce outline/fill icon-swap pattern
# in the aside icon bar.
#
# Every <button> and <a> inside aside.aside-icon-bar must use
# data-icon-swap with two <i> children:
#   <i class="bi bi-*" data-icon="outline"></i>
#   <i class="bi bi-*" data-icon="fill"></i>
#
# Usage:  bin/icon-swap-check.sh
# Exit:   0 = OK, 1 = violations found
# ─────────────────────────────────────────────────────────────
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
ICON_BAR_TEMPLATE="$ROOT/templates/partials/app-main-aside-icon-bar.phtml"
ERRORS=0

echo "=== Icon-swap pattern check ==="

# ── 1. Core icon-bar template must exist ──
if [[ ! -f "$ICON_BAR_TEMPLATE" ]]; then
  echo "FAIL: Icon bar template not found: $ICON_BAR_TEMPLATE"
  exit 1
fi

# ── 2. Every <button> and <a> in icon bar must have data-icon-swap ──
BUTTONS_WITH_SWAP=$(grep -c 'data-icon-swap' "$ICON_BAR_TEMPLATE" 2>/dev/null || true)

if [[ "$BUTTONS_WITH_SWAP" -eq 0 ]]; then
  echo "FAIL: No data-icon-swap attributes found in icon bar template"
  ERRORS=$((ERRORS + 1))
fi

# ── 3. Every data-icon-swap element must have both outline and fill children ──
SWAP_COUNT=$(grep -c 'data-icon-swap' "$ICON_BAR_TEMPLATE" 2>/dev/null || true)
OUTLINE_COUNT=$(grep -c 'data-icon="outline"' "$ICON_BAR_TEMPLATE" 2>/dev/null || true)
FILL_COUNT=$(grep -c 'data-icon="fill"' "$ICON_BAR_TEMPLATE" 2>/dev/null || true)

if [[ "$SWAP_COUNT" -ne "$OUTLINE_COUNT" ]]; then
  echo "FAIL: Mismatch — $SWAP_COUNT data-icon-swap elements but $OUTLINE_COUNT data-icon=\"outline\" icons"
  ERRORS=$((ERRORS + 1))
fi

if [[ "$SWAP_COUNT" -ne "$FILL_COUNT" ]]; then
  echo "FAIL: Mismatch — $SWAP_COUNT data-icon-swap elements but $FILL_COUNT data-icon=\"fill\" icons"
  ERRORS=$((ERRORS + 1))
fi

# ── 4. Module manifests with aside.tab_panel must declare icon ──
for manifest in "$ROOT"/modules/*/module.php; do
  [[ -f "$manifest" ]] || continue
  MODULE_DIR="$(dirname "$manifest")"
  MODULE_ID="$(basename "$MODULE_DIR")"

  # Skip modules that don't declare aside.tab_panel
  if ! grep -q "aside\.tab_panel" "$manifest"; then
    continue
  fi

  # Check icon is declared
  if ! grep -qE "'icon'[[:space:]]*=>[[:space:]]*'bi-" "$manifest"; then
    echo "FAIL: Module '$MODULE_ID' declares aside.tab_panel but has no icon"
    ERRORS=$((ERRORS + 1))
  fi
done

# ── 5. CSS must define the data-icon-swap rules ──
CSS_FILE="$ROOT/web/css/layout/app-aside-icon-bar.css"
if [[ -f "$CSS_FILE" ]]; then
  if ! grep -q '\[data-icon-swap\]' "$CSS_FILE"; then
    echo "FAIL: Missing [data-icon-swap] CSS rules in $CSS_FILE"
    ERRORS=$((ERRORS + 1))
  fi
else
  echo "FAIL: Icon bar CSS not found: $CSS_FILE"
  ERRORS=$((ERRORS + 1))
fi

# ── Result ──
if [[ "$ERRORS" -gt 0 ]]; then
  echo ""
  echo "FAILED: $ERRORS violation(s) found."
  echo "All icon-bar buttons/links must use the data-icon-swap pattern."
  exit 1
fi

echo "OK: All icon-bar elements use the outline/fill swap pattern."
exit 0
