#!/usr/bin/env bash
set -euo pipefail

MOSAIC_HOME="${MOSAIC_HOME:-$HOME/.config/mosaic}"
backup_stamp="$(date +%Y%m%d%H%M%S)"

copy_file_managed() {
  local src="$1"
  local dst="$2"

  mkdir -p "$(dirname "$dst")"

  if [[ -L "$dst" ]]; then
    rm -f "$dst"
  fi

  if [[ -f "$dst" ]]; then
    if cmp -s "$src" "$dst"; then
      return
    fi
    mv "$dst" "${dst}.mosaic-bak-${backup_stamp}"
  fi

  cp "$src" "$dst"
}

remove_legacy_path() {
  local p="$1"

  if [[ -L "$p" ]]; then
    rm -f "$p"
    return
  fi

  if [[ -d "$p" ]]; then
    find "$p" -depth -type l -delete 2>/dev/null || true
    find "$p" -depth -type d -empty -delete 2>/dev/null || true
    return
  fi

  # Remove stale symlinked files if present.
  if [[ -e "$p" && -L "$p" ]]; then
    rm -f "$p"
  fi
}

# Remove compatibility symlink surfaces for migrated content.
legacy_paths=(
  "$HOME/.claude/agent-guides"
  "$HOME/.claude/scripts/git"
  "$HOME/.claude/scripts/codex"
  "$HOME/.claude/scripts/bootstrap"
  "$HOME/.claude/scripts/cicd"
  "$HOME/.claude/scripts/portainer"
  "$HOME/.claude/scripts/debug-hook.sh"
  "$HOME/.claude/scripts/qa-hook-handler.sh"
  "$HOME/.claude/scripts/qa-hook-stdin.sh"
  "$HOME/.claude/scripts/qa-hook-wrapper.sh"
  "$HOME/.claude/scripts/qa-queue-monitor.sh"
  "$HOME/.claude/scripts/remediation-hook-handler.sh"
  "$HOME/.claude/templates"
  "$HOME/.claude/presets/domains"
  "$HOME/.claude/presets/tech-stacks"
  "$HOME/.claude/presets/workflows"
  "$HOME/.claude/presets/jarvis-loop.json"
)

for p in "${legacy_paths[@]}"; do
  remove_legacy_path "$p"
done

# Claude-specific runtime files (settings, hooks — NOT CLAUDE.md which is now a thin pointer)
for runtime_file in \
  CLAUDE.md \
  settings.json \
  hooks-config.json \
  context7-integration.md; do
  src="$MOSAIC_HOME/runtime/claude/$runtime_file"
  [[ -f "$src" ]] || continue
  copy_file_managed "$src" "$HOME/.claude/$runtime_file"
done

# OpenCode runtime adapter (thin pointer to AGENTS.md)
opencode_adapter="$MOSAIC_HOME/runtime/opencode/AGENTS.md"
if [[ -f "$opencode_adapter" ]]; then
  copy_file_managed "$opencode_adapter" "$HOME/.config/opencode/AGENTS.md"
fi

# Codex runtime adapter (thin pointer to AGENTS.md)
codex_adapter="$MOSAIC_HOME/runtime/codex/instructions.md"
if [[ -f "$codex_adapter" ]]; then
  mkdir -p "$HOME/.codex"
  copy_file_managed "$codex_adapter" "$HOME/.codex/instructions.md"
fi

if [[ -x "$MOSAIC_HOME/bin/mosaic-ensure-sequential-thinking" ]]; then
  "$MOSAIC_HOME/bin/mosaic-ensure-sequential-thinking"
fi

echo "[mosaic-link] Runtime assets synced (non-symlink mode)"
echo "[mosaic-link] Canonical source: $MOSAIC_HOME"
