#!/bin/bash
# Gather all relevant context for a product build
# Usage: gather-context.sh <product-slug> [build-type]
# build-type: landing | app | both (default: both)

set -e

SLUG="$1"
BUILD_TYPE="${2:-both}"

if [ -z "$SLUG" ]; then
  echo "Usage: gather-context.sh <product-slug> [landing|app|both]"
  exit 1
fi

PRODUCT_DIR="/home/aria/products/$SLUG"
WORKSPACE_PRODUCT="/home/aria/.openclaw/workspace/agency/products/$SLUG"
OUTPUT_FILE="/tmp/build-context-${SLUG}.md"
SKILLS_DIR="/home/aria/.openclaw/workspace/skills"

# Check if product exists
if [ ! -d "$PRODUCT_DIR" ] && [ ! -d "$WORKSPACE_PRODUCT" ]; then
  echo "ERROR: Product '$SLUG' not found"
  exit 1
fi

# Use whichever exists
if [ -d "$PRODUCT_DIR" ]; then
  BASE_DIR="$PRODUCT_DIR"
else
  BASE_DIR="$WORKSPACE_PRODUCT"
fi

echo "Gathering context for: $SLUG"
echo "Build type: $BUILD_TYPE"
echo "Output: $OUTPUT_FILE"
echo ""

# Start building context
cat > "$OUTPUT_FILE" << EOF
# Build Context: $SLUG

Generated: $(date -u +"%Y-%m-%dT%H:%M:%SZ")
Build Type: $BUILD_TYPE

---

EOF

# 1. Product Identity
echo "## Product Identity" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"

if [ -f "$BASE_DIR/product.yaml" ]; then
  echo "### product.yaml" >> "$OUTPUT_FILE"
  echo '```yaml' >> "$OUTPUT_FILE"
  cat "$BASE_DIR/product.yaml" >> "$OUTPUT_FILE"
  echo '```' >> "$OUTPUT_FILE"
  echo "" >> "$OUTPUT_FILE"
fi

if [ -f "$BASE_DIR/brain.md" ]; then
  echo "### brain.md (first 200 lines)" >> "$OUTPUT_FILE"
  echo '```markdown' >> "$OUTPUT_FILE"
  head -200 "$BASE_DIR/brain.md" >> "$OUTPUT_FILE"
  echo '```' >> "$OUTPUT_FILE"
  echo "" >> "$OUTPUT_FILE"
fi

# 2. Design Context (CRITICAL)
echo "## Design Context (CRITICAL - Follow These)" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"

if [ -f "$BASE_DIR/design/brand-guidelines.md" ]; then
  echo "### Brand Guidelines" >> "$OUTPUT_FILE"
  echo '```markdown' >> "$OUTPUT_FILE"
  cat "$BASE_DIR/design/brand-guidelines.md" >> "$OUTPUT_FILE"
  echo '```' >> "$OUTPUT_FILE"
  echo "" >> "$OUTPUT_FILE"
else
  echo "⚠️ **WARNING: No brand-guidelines.md found!**" >> "$OUTPUT_FILE"
  echo "" >> "$OUTPUT_FILE"
fi

if [ -f "$BASE_DIR/design/design-system-prompt.md" ]; then
  echo "### Design System Prompt" >> "$OUTPUT_FILE"
  echo '```markdown' >> "$OUTPUT_FILE"
  cat "$BASE_DIR/design/design-system-prompt.md" >> "$OUTPUT_FILE"
  echo '```' >> "$OUTPUT_FILE"
  echo "" >> "$OUTPUT_FILE"
else
  echo "" >> "$OUTPUT_FILE"
  echo "## 🚨 BLOCKER: design-system-prompt.md MISSING" >> "$OUTPUT_FILE"
  echo "" >> "$OUTPUT_FILE"
  echo "**Cannot proceed with build.** This file is required for brand-consistent output." >> "$OUTPUT_FILE"
  echo "" >> "$OUTPUT_FILE"
  echo "**Options:**" >> "$OUTPUT_FILE"
  echo "1. Ask the user if they want you to generate it from brand-guidelines.md" >> "$OUTPUT_FILE"
  echo "2. Run Design phase Step 12 to generate it properly" >> "$OUTPUT_FILE"
  echo "3. User can manually create it using template: \`agency/templates/design-system-prompt.md\`" >> "$OUTPUT_FILE"
  echo "" >> "$OUTPUT_FILE"
  
  # Also print to stderr for visibility
  echo "" >&2
  echo "🚨 BLOCKER: $SLUG is missing design-system-prompt.md" >&2
  echo "   Ask user before proceeding with build." >&2
  echo "" >&2
fi

# Assets inventory
echo "### Available Assets" >> "$OUTPUT_FILE"
if [ -d "$BASE_DIR/design/assets" ]; then
  echo '```' >> "$OUTPUT_FILE"
  ls -la "$BASE_DIR/design/assets/" 2>/dev/null >> "$OUTPUT_FILE" || echo "Empty"
  echo '```' >> "$OUTPUT_FILE"
else
  echo "No design/assets/ folder found" >> "$OUTPUT_FILE"
fi
echo "" >> "$OUTPUT_FILE"

# Check for marketing assets too
if [ -d "$BASE_DIR/marketing/images" ]; then
  echo "### Marketing Images" >> "$OUTPUT_FILE"
  echo '```' >> "$OUTPUT_FILE"
  ls -la "$BASE_DIR/marketing/images/" 2>/dev/null >> "$OUTPUT_FILE" || echo "Empty"
  echo '```' >> "$OUTPUT_FILE"
  echo "" >> "$OUTPUT_FILE"
fi

# 3. Discovery Context
echo "## Discovery Context" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"

if [ -f "$BASE_DIR/discovery/brief.md" ]; then
  echo "### Product Brief" >> "$OUTPUT_FILE"
  echo '```markdown' >> "$OUTPUT_FILE"
  cat "$BASE_DIR/discovery/brief.md" >> "$OUTPUT_FILE"
  echo '```' >> "$OUTPUT_FILE"
  echo "" >> "$OUTPUT_FILE"
fi

if [ -f "$BASE_DIR/discovery/target-market.md" ]; then
  echo "### Target Market" >> "$OUTPUT_FILE"
  echo '```markdown' >> "$OUTPUT_FILE"
  head -100 "$BASE_DIR/discovery/target-market.md" >> "$OUTPUT_FILE"
  echo '```' >> "$OUTPUT_FILE"
  echo "" >> "$OUTPUT_FILE"
fi

# 4. Technical Context (for apps)
if [ "$BUILD_TYPE" = "app" ] || [ "$BUILD_TYPE" = "both" ]; then
  echo "## Technical Context" >> "$OUTPUT_FILE"
  echo "" >> "$OUTPUT_FILE"
  
  if [ -f "$BASE_DIR/build/tech-stack.md" ]; then
    echo "### Tech Stack" >> "$OUTPUT_FILE"
    echo '```markdown' >> "$OUTPUT_FILE"
    cat "$BASE_DIR/build/tech-stack.md" >> "$OUTPUT_FILE"
    echo '```' >> "$OUTPUT_FILE"
    echo "" >> "$OUTPUT_FILE"
  fi
  
  # Check for existing app
  if [ -d "$BASE_DIR/apps/main/src" ]; then
    echo "### Existing App Structure" >> "$OUTPUT_FILE"
    echo '```' >> "$OUTPUT_FILE"
    find "$BASE_DIR/apps/main/src" -type f -name "*.tsx" | head -20 >> "$OUTPUT_FILE"
    echo '```' >> "$OUTPUT_FILE"
    echo "" >> "$OUTPUT_FILE"
  fi
fi

# 5. Landing Page Context
if [ "$BUILD_TYPE" = "landing" ] || [ "$BUILD_TYPE" = "both" ]; then
  LANDING_DIR="/home/aria/projects/${SLUG}-landing"
  if [ -d "$LANDING_DIR" ]; then
    echo "## Existing Landing Page" >> "$OUTPUT_FILE"
    echo "" >> "$OUTPUT_FILE"
    echo "Location: $LANDING_DIR" >> "$OUTPUT_FILE"
    echo '```' >> "$OUTPUT_FILE"
    ls -la "$LANDING_DIR/src/app/" 2>/dev/null | head -20 >> "$OUTPUT_FILE" || echo "No src/app"
    echo '```' >> "$OUTPUT_FILE"
    echo "" >> "$OUTPUT_FILE"
  fi
fi

# 6. Skills - EMBEDDED FULLY (not just paths)
echo "## Design Skills (FOLLOW THESE EXACTLY)" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"

# Embed frontend-design (core design principles)
if [ -f "$SKILLS_DIR/superdesign/SKILL.md" ]; then
  echo "### Frontend Design Principles" >> "$OUTPUT_FILE"
  echo "" >> "$OUTPUT_FILE"
  echo '```markdown' >> "$OUTPUT_FILE"
  cat "$SKILLS_DIR/superdesign/SKILL.md" >> "$OUTPUT_FILE"
  echo '```' >> "$OUTPUT_FILE"
  echo "" >> "$OUTPUT_FILE"
fi

# Embed frontend-design-excellence (taste protocol / quality gate)
if [ -f "$SKILLS_DIR/frontend-design-excellence/SKILL.md" ]; then
  echo "### Taste Protocol (Quality Gate)" >> "$OUTPUT_FILE"
  echo "" >> "$OUTPUT_FILE"
  echo "**Run this checklist AFTER building but BEFORE delivering:**" >> "$OUTPUT_FILE"
  echo "" >> "$OUTPUT_FILE"
  echo '```markdown' >> "$OUTPUT_FILE"
  cat "$SKILLS_DIR/frontend-design-excellence/SKILL.md" >> "$OUTPUT_FILE"
  echo '```' >> "$OUTPUT_FILE"
  echo "" >> "$OUTPUT_FILE"
fi

# Embed landing-page-builder for landing builds
if [ "$BUILD_TYPE" = "landing" ] || [ "$BUILD_TYPE" = "both" ]; then
  if [ -f "$SKILLS_DIR/landing-page-builder/SKILL.md" ]; then
    echo "### Landing Page Builder Patterns" >> "$OUTPUT_FILE"
    echo "" >> "$OUTPUT_FILE"
    echo '```markdown' >> "$OUTPUT_FILE"
    cat "$SKILLS_DIR/landing-page-builder/SKILL.md" >> "$OUTPUT_FILE"
    echo '```' >> "$OUTPUT_FILE"
    echo "" >> "$OUTPUT_FILE"
  fi
fi

# Embed web-app-builder for app builds
if [ "$BUILD_TYPE" = "app" ] || [ "$BUILD_TYPE" = "both" ]; then
  if [ -f "$SKILLS_DIR/web-app-builder/SKILL.md" ]; then
    echo "### Web App Builder Patterns" >> "$OUTPUT_FILE"
    echo "" >> "$OUTPUT_FILE"
    echo '```markdown' >> "$OUTPUT_FILE"
    cat "$SKILLS_DIR/web-app-builder/SKILL.md" >> "$OUTPUT_FILE"
    echo '```' >> "$OUTPUT_FILE"
    echo "" >> "$OUTPUT_FILE"
  fi
fi

# Embed ui-ux-pro-max if exists
if [ -f "$SKILLS_DIR/ui-ux-pro-max/SKILL.md" ]; then
  echo "### UI/UX Pro Guidelines" >> "$OUTPUT_FILE"
  echo "" >> "$OUTPUT_FILE"
  echo '```markdown' >> "$OUTPUT_FILE"
  cat "$SKILLS_DIR/ui-ux-pro-max/SKILL.md" >> "$OUTPUT_FILE"
  echo '```' >> "$OUTPUT_FILE"
  echo "" >> "$OUTPUT_FILE"
fi

# 7. Anti-patterns reminder
cat >> "$OUTPUT_FILE" << 'EOF'
## Anti-Patterns to AVOID

- ❌ Generic SaaS look (centered text stacks)
- ❌ Stock photos or placeholder images
- ❌ Corporate buzzword copy ("revolutionize", "leverage", "synergy")
- ❌ Colors not from brand palette
- ❌ Typography not matching brand guidelines
- ❌ Wrong or missing logo
- ❌ Ignoring the design-system-prompt.md direction
- ❌ Generic hero without product personality

## Checklist Before Reporting Done

- [ ] Brand colors match exactly (verify hex values)
- [ ] Typography matches brand guidelines
- [ ] Logo used correctly (right file, proper spacing)
- [ ] Copy tone matches brand voice
- [ ] Responsive design works (mobile, tablet, desktop)
- [ ] Build compiles without errors
- [ ] Deployed and accessible
- [ ] **POST-DEPLOY HOOK TRIGGERED** (MANDATORY)

## ⚠️ MANDATORY: Trigger Post-Deploy Hook

After deploy completes, you MUST run this command:

\`\`\`bash
bash ~/.openclaw/workspace/scripts/post-deploy-hook.sh "$SLUG" "$BUILD_TYPE" "Change summary 1" "Change summary 2" ...
\`\`\`

Example:
\`\`\`bash
bash ~/.openclaw/workspace/scripts/post-deploy-hook.sh "dinq" "landing" "Redesigned hero section" "Added feature cards" "Updated pricing"
\`\`\`

This triggers the approval card in Symphony chat. **DO NOT skip this step.**

EOF

echo ""
echo "✅ Context gathered to: $OUTPUT_FILE"
echo ""
echo "Include this in your spawn task:"
echo "cat $OUTPUT_FILE"
