#!/bin/bash
set -e
CH="/root/ce-channels"
WORK="/root/.openclaw/workspace/work"
PUB="/root/.openclaw/workspace/public"

link_dir() {
  local src="$1"
  local dest="$2"
  local label="$3"
  if [ -d "$src" ]; then
    local count=$(find "$src" -type f -not -path "*/node_modules/*" -not -path "*/.git/*" -not -path "*/.next/*" 2>/dev/null | wc -l)
    if [ "$count" -gt 0 ]; then
      ln -sfn "$src" "$dest"
      echo "    + $label ($count files)"
    fi
  fi
}

enrich() {
  local channel="$1"
  local active="$2"
  local pitch="$3"
  local public="$4"
  local extra="$5"

  CDIR="$CH/$channel"
  [ -d "$CDIR" ] || return 0

  echo "-- $channel --"

  # Active projects
  if [ -n "$active" ]; then
    for d in $(echo "$active" | tr ',' ' '); do
      name=$(basename "$d")
      link_dir "$WORK/$d" "$CDIR/project-$name" "project: $name"
    done
  fi

  # Pitches
  if [ -n "$pitch" ]; then
    for d in $(echo "$pitch" | tr ',' ' '); do
      name=$(basename "$d")
      link_dir "$WORK/$d" "$CDIR/pitch-$name" "pitch: $name"
    done
  fi

  # Public assets
  if [ -n "$public" ]; then
    for d in $(echo "$public" | tr ',' ' '); do
      link_dir "$PUB/$d" "$CDIR/assets-$d" "assets: $d"
    done
  fi

  # Extra refs
  if [ -n "$extra" ]; then
    for d in $(echo "$extra" | tr ',' ' '); do
      name=$(echo "$d" | tr '/' '-')
      link_dir "$WORK/$d" "$CDIR/ref-$name" "ref: $d"
    done
  fi

  # Discord history: copy from symlink to real file
  if [ -L "$CDIR/history.md" ]; then
    target=$(readlink "$CDIR/history.md")
    if [ -f "$target" ]; then
      cp "$target" "$CDIR/discord-history.md"
      rm "$CDIR/history.md"
      echo "    + discord-history.md (copied)"
    fi
  fi
}

# ── Projects ──
enrich "mission" "active-projects/mission" "pitches/mission-mode" "mission,mission-refs" ""
enrich "mission-2" "active-projects/mission" "pitches/mission-mode" "mission,mission-refs" ""
enrich "etoro" "active-projects/etoro" "pitches/etoro" "etoro,etoro-competition,etoro-portal,etoro-portal-public" ""
enrich "spoken-institute" "active-projects/spoken,active-projects/spoken-institute" "" "spoken-images,spoken-language-staging,spoken-language-v2,spoken-new" ""
enrich "team8" "" "pitches/team8" "team8,team8-landing,flowrx" ""
enrich "hud" "" "pitches/hud" "hud" ""
enrich "lobster" "" "" "lobster" ""
enrich "phat-foods" "active-projects/olevia" "" "all-phat-visuals,phat,phat-foods,phat-slides" ""
enrich "verifone" "" "pitches/verifone" "" ""
enrich "fioner" "" "pitches/fiona-conrad" "fiona" ""
enrich "jviewz" "" "" "jonathan" ""
enrich "autonomous-agency" "" "" "clawpilot" "internal-ce/tech/autonomous-agency,internal-ce/tech/clawpilot"
enrich "white-space" "active-projects/white-space" "pitches/white-space" "whitespace,white-space-strategy" ""

# ── Curious Endeavor ──
enrich "curious-endeavor" "active-projects/ce,active-projects/ce-website" "" "approach,ce-landing,ce-lite,ce-pricing,ce-tov,offering,products" "internal-ce/brand"
enrich "ce-social" "active-projects/ce" "" "ce-landing" "internal-ce/operations/linkedin"
enrich "figma" "" "" "figma-plugin,figma-remote-plugin,figma-story,cc-fig-plugin-download" "internal-ce/tech/figma-plugin,internal-ce/tech/figma-scripts,internal-ce/tech/cc-fig-plugin-download"
enrich "portoflio" "" "" "deck,grid" ""
enrich "visual-input" "" "" "visual-research,screenshots,sources" "internal-ce/visual-research"
enrich "skills-development" "" "" "skills,skill-demo" ""

# ── Active Work ──
enrich "ce-website" "active-projects/ce-website" "" "approach,ce-landing" ""
enrich "dev-projects" "" "" "clawpilot" "internal-ce/tech"
enrich "wix-pitch" "" "pitches/wix" "wix" ""
enrich "open-tasks" "" "" "" ""
enrich "ideas-backlog" "" "" "" ""
enrich "client-support" "" "" "" ""
enrich "emails-and-cals-scan" "" "" "" ""

# ── HQ ──
enrich "ce-finance" "" "" "ce-pricing" "internal-ce/operations"
enrich "improvement" "" "" "strategy" "internal-ce/methodology"
enrich "contacts-from-site" "active-projects/ce-leadgen,active-projects/ce-sales-pipeline" "" "" ""
enrich "admin" "" "" "" "internal-ce/operations"
enrich "brain-maintenance" "" "" "" ""
enrich "2025-tax" "" "" "" "tax-2025"

# ── Methodology ──
enrich "radar" "" "" "radar-screenshots" "internal-ce/methodology"
enrich "taste-development" "" "" "taste" ""
enrich "visual-methodology" "" "" "visual-research,visual-qa,vqa" "internal-ce/visual-research,internal-ce/methodology"
enrich "skills-review" "" "" "skills,skill-demo" ""
enrich "methodology" "" "" "strategy" "internal-ce/methodology/methodology"
enrich "logo-maker-machine" "" "" "" ""
enrich "skills-improvement-karpathy" "" "" "" ""

# ── Workspace ──
enrich "general" "" "" "" ""
enrich "tasks" "" "" "" ""
enrich "strategy-document" "" "" "strategy" ""
enrich "claude-for-figma" "" "" "figma-plugin" "internal-ce/tech/claude-figma-plugin,internal-ce/tech/figma-plugin"
enrich "image-creation" "" "" "images" ""

# ── Productize ──
enrich "product-definition" "" "" "products" "internal-ce/operations/products"
enrich "fiona-protocol-v2-test" "" "pitches/fiona-conrad" "fiona" ""

# ── Home Fronteira ──
enrich "marquez" "" "pitches/marquez-131" "" ""
enrich "office" "" "" "" ""
enrich "club-7" "" "" "" ""
enrich "basket" "" "" "" ""

# ── Standalone ──
enrich "management" "" "" "comms" "internal-ce/operations"
enrich "brandwatch" "active-projects/brandwatch" "pitches/brandwatch" "brandwatch,brandwatch-pitch" ""
enrich "lukas-kitt" "" "" "" ""
enrich "amir" "" "" "" ""
enrich "brand-pipeline-spec" "active-projects/ce-leadgen" "" "" ""
enrich "competitive-research" "active-projects/ce-competitive-research" "" "competition" ""
enrich "spoken-language" "active-projects/spoken" "" "spoken-language-staging,spoken-language-v2" ""

# ── Now upgrade every CLAUDE.md with contents listing ──
echo ""
echo "== Upgrading CLAUDE.md files =="
for dir in "$CH"/*/; do
  name=$(basename "$dir")
  [ -f "$dir/CLAUDE.md" ] || continue

  # Build contents section
  contents=""
  for item in "$dir"/*; do
    bname=$(basename "$item")
    [ "$bname" = "CLAUDE.md" ] && continue
    if [ -L "$item" ]; then
      target=$(readlink "$item")
      fcount=$(find "$target" -type f -not -path "*/node_modules/*" -not -path "*/.git/*" -not -path "*/.next/*" 2>/dev/null | wc -l | tr -d ' ')
      contents="$contents\n- **$bname/** → \`$target\` ($fcount files)"
    elif [ -f "$item" ]; then
      sz=$(wc -c < "$item" | tr -d ' ')
      contents="$contents\n- **$bname** ($sz bytes)"
    fi
  done

  if [ -n "$contents" ]; then
    # Append contents to CLAUDE.md if not already there
    if ! grep -q "## Contents" "$dir/CLAUDE.md" 2>/dev/null; then
      printf "\n## Contents\n$contents\n" >> "$dir/CLAUDE.md"
      echo "  $name: updated CLAUDE.md"
    fi
  fi
done

echo ""
echo "=== DONE ==="
echo ""

# Final summary
total_links=0
total_files=0
for dir in "$CH"/*/; do
  name=$(basename "$dir")
  links=$(find "$dir" -maxdepth 1 -type l 2>/dev/null | wc -l | tr -d ' ')
  files=$(find "$dir" -maxdepth 1 -type f 2>/dev/null | wc -l | tr -d ' ')
  total_links=$((total_links + links))
  total_files=$((total_files + files))
  echo "$name: $files files, $links linked project dirs"
done
echo ""
echo "TOTAL: $total_files files, $total_links linked project dirs across $(ls -d $CH/*/ | wc -l | tr -d ' ') channels"
