#!/bin/bash
# build-brand-registry.sh
# Scans all Symphony products and outputs a brand fingerprint registry.
# Used during design phase to ensure new brands don't visually collide with existing ones.
#
# Usage: bash build-brand-registry.sh [--output /path/to/output.md]
# Default output: /tmp/symphony-brand-registry.md

OUTPUT="${2:-/tmp/symphony-brand-registry.md}"
PRODUCTS_DIR="/home/aria/products"

cat > "$OUTPUT" << 'HEADER'
# Symphony Brand Registry
# Auto-generated by build-brand-registry.sh
# This file lists the visual fingerprint of every product in the Symphony platform.
# Use this during design phase to ensure new brands are visually distinct.

HEADER

echo "Generated: $(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$OUTPUT"
echo "" >> "$OUTPUT"
echo "---" >> "$OUTPUT"
echo "" >> "$OUTPUT"

COUNT=0

for product_dir in "$PRODUCTS_DIR"/*/; do
  slug=$(basename "$product_dir")
  design_yaml="$product_dir/design/design.yaml"
  brand_md="$product_dir/design/brand-guidelines.md"

  # Skip if no design assets at all
  if [ ! -f "$design_yaml" ] && [ ! -f "$brand_md" ]; then
    continue
  fi

  COUNT=$((COUNT + 1))

  echo "## $slug" >> "$OUTPUT"
  echo "" >> "$OUTPUT"

  if [ -f "$design_yaml" ]; then
    # Extract key fields using grep/awk (no yq dependency)
    PRIMARY=$(grep -E "^\s*primary:\s*'#" "$design_yaml" | head -1 | sed "s/.*'#/#/" | tr -d "'")
    SECONDARY=$(grep -E "^\s*secondary:\s*'#" "$design_yaml" | head -1 | sed "s/.*'#/#/" | tr -d "'")
    ACCENT=$(grep -E "^\s*accent:\s*'#|accent_cyan|accent_orange" "$design_yaml" | head -1 | sed "s/.*'#/#/" | tr -d "'")
    BG=$(grep -E "^\s*background:\s*'#" "$design_yaml" | head -1 | sed "s/.*'#/#/" | tr -d "'")
    HEADING_FONT=$(grep -E "^\s*heading:\s" "$design_yaml" | head -1 | awk '{print $2}' | tr -d "'\"")
    BODY_FONT=$(grep -E "^\s*body:\s" "$design_yaml" | head -1 | awk '{print $2}' | tr -d "'\"")

    # Determine dark/light theme
    THEME="light"
    if echo "$BG" | grep -qiE "^#0|^#1|^#2"; then
      THEME="dark"
    fi

    echo "- **Primary color:** \`${PRIMARY:-unknown}\`" >> "$OUTPUT"
    [ -n "$SECONDARY" ] && echo "- **Secondary color:** \`$SECONDARY\`" >> "$OUTPUT"
    [ -n "$ACCENT" ] && echo "- **Accent color:** \`$ACCENT\`" >> "$OUTPUT"
    [ -n "$BG" ] && echo "- **Background:** \`$BG\` ($THEME theme)" >> "$OUTPUT"
    [ -n "$HEADING_FONT" ] && echo "- **Heading font:** $HEADING_FONT" >> "$OUTPUT"
    [ -n "$BODY_FONT" ] && echo "- **Body font:** $BODY_FONT" >> "$OUTPUT"
  fi

  if [ -f "$brand_md" ]; then
    # Pull first few meaningful lines to capture brand personality
    PERSONALITY=$(grep -A2 -i "personality\|voice\|adjective\|feel\|tone" "$brand_md" | head -6 | grep -v "^--" | tr '\n' ' ' | cut -c1-200)
    [ -n "$PERSONALITY" ] && echo "- **Brand personality notes:** $PERSONALITY..." >> "$OUTPUT"
  fi

  echo "" >> "$OUTPUT"
done

echo "---" >> "$OUTPUT"
echo "" >> "$OUTPUT"
echo "**Total products with brand assets:** $COUNT" >> "$OUTPUT"

echo "Brand registry built: $OUTPUT ($COUNT products)"
