#!/bin/bash
# migrate-pages.sh — Restructure SX page files so file paths match URL structure
# All moves use git mv. Directories created with mkdir -p.
# If source doesn't exist, skip with warning. If target exists, skip.
set -euo pipefail

cd /root/rose-ash

# Base directory for all moves
B="sx/sx"

# First, stage all current changes in sx/sx/ so that the one-per-file split
# files become tracked by git (they are currently untracked).
echo "=== Staging current sx/sx/ changes ==="
git add sx/sx/
echo "  Done. $(git diff --cached --name-only | wc -l) files staged."

MOVED=0
SKIPPED=0
WARNINGS=0

safe_mv() {
  local src="$B/$1"
  local dst="$B/$2"
  if [ ! -f "$src" ]; then
    echo "  WARN: source missing: $1"
    WARNINGS=$((WARNINGS + 1))
    return
  fi
  if [ -f "$dst" ]; then
    echo "  SKIP: target exists: $2"
    SKIPPED=$((SKIPPED + 1))
    return
  fi
  local dir
  dir=$(dirname "$dst")
  mkdir -p "$dir"
  git mv "$src" "$dst"
  echo "  $1 -> $2"
  MOVED=$((MOVED + 1))
}

echo ""
echo "=== HOME ==="
safe_mv "docs-content/home-content.sx" "home/index.sx"

echo ""
echo "=== GEOGRAPHY: core pages ==="
safe_mv "geography/capabilities.sx" "geography/capabilities/index.sx"
safe_mv "geography/modules.sx" "geography/modules/index.sx"
safe_mv "geography/eval-rules.sx" "geography/eval-rules/index.sx"

echo ""
echo "=== GEOGRAPHY: CEK ==="
safe_mv "geography/cek/cek-content.sx" "geography/cek/index.sx"
safe_mv "geography/cek/cek-demo-content.sx" "geography/cek/demo/index.sx"
safe_mv "geography/cek/cek-freeze-content.sx" "geography/cek/freeze/index.sx"
safe_mv "geography/cek/cek-content-address-content.sx" "geography/cek/content/index.sx"

# CEK islands (demo-* files)
for f in $B/geography/cek/demo-*.sx; do
  [ -f "$f" ] || continue
  base=$(basename "$f")
  safe_mv "geography/cek/$base" "geography/cek/_islands/$base"
done
safe_mv "geography/cek/content-address-demo.sx" "geography/cek/_islands/content-address-demo.sx"
safe_mv "geography/cek/freeze-demo.sx" "geography/cek/_islands/freeze-demo.sx"

echo ""
echo "=== GEOGRAPHY: Reactive Islands index ==="
safe_mv "reactive-islands/index/reactive-islands-index-content.sx" "geography/reactive/index.sx"
for f in $B/reactive-islands/index/demo-*.sx; do
  [ -f "$f" ] || continue
  base=$(basename "$f")
  safe_mv "reactive-islands/index/$base" "geography/reactive/_islands/$base"
done

echo ""
echo "=== GEOGRAPHY: Reactive Islands demo (examples) ==="
safe_mv "reactive-islands/demo/reactive-islands-demo-content.sx" "geography/reactive/examples/index.sx"
for f in $B/reactive-islands/demo/example-*.sx; do
  [ -f "$f" ] || continue
  base=$(basename "$f")
  slug="${base#example-}"
  slug="${slug%.sx}"
  safe_mv "reactive-islands/demo/$base" "geography/reactive/examples/$slug/index.sx"
done

echo ""
echo "=== GEOGRAPHY: Marshes ==="
safe_mv "reactive-islands/marshes/reactive-islands-marshes-content.sx" "geography/marshes/index.sx"
for f in $B/reactive-islands/marshes/demo-*.sx; do
  [ -f "$f" ] || continue
  base=$(basename "$f")
  safe_mv "reactive-islands/marshes/$base" "geography/marshes/_islands/$base"
done
for f in $B/reactive-islands/marshes/example-*.sx; do
  [ -f "$f" ] || continue
  base=$(basename "$f")
  slug="${base#example-}"
  slug="${slug%.sx}"
  safe_mv "reactive-islands/marshes/$base" "geography/marshes/$slug/index.sx"
done

echo ""
echo "=== GEOGRAPHY: Reactive lib files ==="
safe_mv "reactive-islands/event-bridge.sx" "geography/reactive/_lib/event-bridge.sx"
safe_mv "reactive-islands/named-stores.sx" "geography/reactive/_lib/named-stores.sx"
safe_mv "reactive-islands/phase2.sx" "geography/reactive/_lib/phase2.sx"
safe_mv "reactive-islands/plan.sx" "geography/reactive/_lib/plan.sx"
safe_mv "reactive-islands/test-runner.sx" "geography/reactive/_lib/test-runner.sx"
safe_mv "reactive-islands/test-temperature.sx" "geography/reactive/_lib/test-temperature.sx"
safe_mv "reactive-islands/runner-placeholder/test-runner-placeholder.sx" "geography/reactive/_lib/test-runner-placeholder.sx"

# demo-cyst and demo-reactive-expressions → _lib
for f in $B/reactive-islands/demo-cyst/*.sx; do
  [ -f "$f" ] || continue
  base=$(basename "$f")
  safe_mv "reactive-islands/demo-cyst/$base" "geography/reactive/_lib/$base"
done
for f in $B/reactive-islands/demo-reactive-expressions/*.sx; do
  [ -f "$f" ] || continue
  base=$(basename "$f")
  safe_mv "reactive-islands/demo-reactive-expressions/$base" "geography/reactive/_lib/$base"
done

echo ""
echo "=== GEOGRAPHY: Scopes ==="
safe_mv "scopes/scopes-content.sx" "geography/scopes/index.sx"
for f in $B/scopes/demo-*.sx; do
  [ -f "$f" ] || continue
  base=$(basename "$f")
  safe_mv "scopes/$base" "geography/scopes/_islands/$base"
done
safe_mv "scopes/scopes-demo-example.sx" "geography/scopes/_islands/scopes-demo-example.sx"

echo ""
echo "=== GEOGRAPHY: Provide ==="
safe_mv "provide/provide-content.sx" "geography/provide/index.sx"
for f in $B/provide/demo-*.sx; do
  [ -f "$f" ] || continue
  base=$(basename "$f")
  safe_mv "provide/$base" "geography/provide/_islands/$base"
done
safe_mv "provide/provide-demo-example.sx" "geography/provide/_islands/provide-demo-example.sx"

echo ""
echo "=== GEOGRAPHY: Spreads ==="
safe_mv "spreads/spreads-content.sx" "geography/spreads/index.sx"
for f in $B/spreads/demo-*.sx; do
  [ -f "$f" ] || continue
  base=$(basename "$f")
  safe_mv "spreads/$base" "geography/spreads/_islands/$base"
done

echo ""
echo "=== GEOGRAPHY: Reactive Runtime ==="
safe_mv "reactive-runtime/overview-content.sx" "geography/reactive-runtime/index.sx"

# Other content files → {slug}/index.sx  (strip -content suffix)
for f in $B/reactive-runtime/*-content.sx; do
  [ -f "$f" ] || continue
  base=$(basename "$f")
  [ "$base" = "overview-content.sx" ] && continue
  slug="${base%-content.sx}"
  safe_mv "reactive-runtime/$base" "geography/reactive-runtime/$slug/index.sx"
done

# demo-* → _islands
for f in $B/reactive-runtime/demo-*.sx; do
  [ -f "$f" ] || continue
  base=$(basename "$f")
  safe_mv "reactive-runtime/$base" "geography/reactive-runtime/_islands/$base"
done

echo ""
echo "=== GEOGRAPHY: Hypermedia Reference ==="
safe_mv "reference/attrs-content.sx" "geography/hypermedia/reference/attributes/index.sx"
safe_mv "reference/headers-content.sx" "geography/hypermedia/reference/headers/index.sx"
safe_mv "reference/events-content.sx" "geography/hypermedia/reference/events/index.sx"
safe_mv "reference/js-api-content.sx" "geography/hypermedia/reference/js-api/index.sx"

# Reference shared files
safe_mv "reference/attr-detail-content.sx" "geography/hypermedia/reference/_shared/attr-detail-content.sx"
safe_mv "reference/attr-not-found.sx" "geography/hypermedia/reference/_shared/attr-not-found.sx"
safe_mv "reference/header-detail-content.sx" "geography/hypermedia/reference/_shared/header-detail-content.sx"
safe_mv "reference/event-detail-content.sx" "geography/hypermedia/reference/_shared/event-detail-content.sx"

# Reference index
safe_mv "examples/reference-index-content.sx" "geography/hypermedia/reference/index.sx"

# Example page shared
safe_mv "examples/page-content.sx" "geography/hypermedia/example/_shared/page-content.sx"

# Examples-content → example/{slug}/index.sx
for f in $B/examples-content/example-*.sx; do
  [ -f "$f" ] || continue
  base=$(basename "$f")
  slug="${base#example-}"
  slug="${slug%.sx}"
  safe_mv "examples-content/$base" "geography/hypermedia/example/$slug/index.sx"
done

echo ""
echo "=== GEOGRAPHY: Isomorphism ==="
safe_mv "analyzer/bundle-analyzer-content.sx" "geography/isomorphism/bundle-analyzer/index.sx"
# Analyzer islands (non-content files)
for f in $B/analyzer/*.sx; do
  [ -f "$f" ] || continue
  base=$(basename "$f")
  [ "$base" = "bundle-analyzer-content.sx" ] && continue
  safe_mv "analyzer/$base" "geography/isomorphism/bundle-analyzer/_islands/$base"
done

safe_mv "routing-analyzer/content.sx" "geography/isomorphism/routing-analyzer/index.sx"
safe_mv "routing-analyzer/routing-row.sx" "geography/isomorphism/routing-analyzer/_islands/routing-row.sx"

safe_mv "async-io-demo.sx" "geography/isomorphism/async-io/index.sx"

safe_mv "affinity-demo/content.sx" "geography/isomorphism/affinity/index.sx"
for f in $B/affinity-demo/aff-*.sx; do
  [ -f "$f" ] || continue
  base=$(basename "$f")
  safe_mv "affinity-demo/$base" "geography/isomorphism/affinity/_islands/$base"
done

safe_mv "optimistic-demo.sx" "geography/isomorphism/optimistic/index.sx"
safe_mv "offline-demo.sx" "geography/isomorphism/offline/index.sx"

echo ""
echo "=== LANGUAGE: Docs ==="
safe_mv "docs-content/docs-introduction-content.sx" "language/doc/introduction/index.sx"
safe_mv "docs-content/docs-getting-started-content.sx" "language/doc/getting-started/index.sx"
safe_mv "docs-content/docs-components-content.sx" "language/doc/components/index.sx"
safe_mv "docs-content/docs-evaluator-content.sx" "language/doc/evaluator/index.sx"
safe_mv "docs-content/docs-primitives-content.sx" "language/doc/primitives/index.sx"
safe_mv "docs-content/docs-special-forms-content.sx" "language/doc/special-forms/index.sx"
safe_mv "docs-content/docs-server-rendering-content.sx" "language/doc/server-rendering/index.sx"

echo ""
echo "=== LANGUAGE: Spec ==="
safe_mv "specs/architecture-content.sx" "language/spec/index.sx"
safe_mv "specs/overview-content.sx" "language/spec/_shared/overview-content.sx"
safe_mv "specs/detail-content.sx" "language/spec/_shared/detail-content.sx"
safe_mv "specs/not-found.sx" "language/spec/_shared/not-found.sx"

echo ""
echo "=== LANGUAGE: Bootstrappers ==="
safe_mv "specs/bootstrappers-index-content.sx" "language/bootstrapper/index.sx"
safe_mv "specs/bootstrapper-js-content.sx" "language/bootstrapper/javascript/index.sx"
safe_mv "specs/bootstrapper-py-content.sx" "language/bootstrapper/python/index.sx"
safe_mv "specs/bootstrapper-self-hosting-content.sx" "language/bootstrapper/self-hosting/index.sx"
safe_mv "specs/bootstrapper-self-hosting-js-content.sx" "language/bootstrapper/self-hosting-js/index.sx"

echo ""
echo "=== LANGUAGE: Page helpers ==="
safe_mv "page-helpers-demo/content.sx" "language/bootstrapper/page-helpers/index.sx"
for f in $B/page-helpers-demo/demo-*.sx; do
  [ -f "$f" ] || continue
  base=$(basename "$f")
  safe_mv "page-helpers-demo/$base" "language/bootstrapper/page-helpers/_islands/$base"
done

echo ""
echo "=== LANGUAGE: Testing ==="
safe_mv "testing/overview-content.sx" "language/test/index.sx"
safe_mv "testing/runners-content.sx" "language/test/runners/index.sx"
safe_mv "testing/spec-content.sx" "language/test/_shared/spec-content.sx"

echo ""
echo "=== APPLICATIONS: HTMX ==="
safe_mv "htmx/demo-content.sx" "applications/htmx/index.sx"
safe_mv "htmx/_test/demo-content.sx" "applications/htmx/_test/demo-content.sx"

echo ""
echo "=== APPLICATIONS: Hyperscript ==="
safe_mv "hyperscript/playground-content.sx" "applications/hyperscript/index.sx"
safe_mv "hyperscript/htmx-content.sx" "applications/hyperscript/htmx/index.sx"
safe_mv "hyperscript/compile-result.sx" "applications/hyperscript/_islands/compile-result.sx"
safe_mv "hyperscript/example.sx" "applications/hyperscript/_islands/example.sx"
safe_mv "hyperscript/live-demo.sx" "applications/hyperscript/_islands/live-demo.sx"
safe_mv "hyperscript/playground.sx" "applications/hyperscript/_islands/playground.sx"
safe_mv "hyperscript/translation.sx" "applications/hyperscript/_islands/translation.sx"

echo ""
echo "=== APPLICATIONS: CSSX ==="
safe_mv "cssx/overview-content.sx" "applications/cssx/index.sx"
for f in $B/cssx/*-content.sx; do
  [ -f "$f" ] || continue
  base=$(basename "$f")
  [ "$base" = "overview-content.sx" ] && continue
  slug="${base%-content.sx}"
  safe_mv "cssx/$base" "applications/cssx/$slug/index.sx"
done

echo ""
echo "=== APPLICATIONS: Protocols ==="
safe_mv "protocols/wire-format-content.sx" "applications/protocol/index.sx"
for f in $B/protocols/*-content.sx; do
  [ -f "$f" ] || continue
  base=$(basename "$f")
  [ "$base" = "wire-format-content.sx" ] && continue
  slug="${base%-content.sx}"
  safe_mv "protocols/$base" "applications/protocol/$slug/index.sx"
done

echo ""
echo "=== APPLICATIONS: Standalone pages ==="
safe_mv "sx-urls.sx" "applications/sx-urls/index.sx"
safe_mv "native-browser.sx" "applications/native-browser/index.sx"
safe_mv "sxtp.sx" "applications/sxtp/index.sx"

echo ""
echo "=== APPLICATIONS: GraphQL ==="
safe_mv "graphql/demo-content.sx" "applications/graphql/index.sx"
safe_mv "graphql/parse-island.sx" "applications/graphql/_islands/parse-island.sx"

echo ""
echo "=== APPLICATIONS: Pretext ==="
safe_mv "pretext-demo/content.sx" "applications/pretext/index.sx"
safe_mv "pretext-demo/live.sx" "applications/pretext/_islands/live.sx"
safe_mv "pretext-demo/render-paragraph.sx" "applications/pretext/_islands/render-paragraph.sx"

echo ""
echo "=== ETC: Essays ==="
safe_mv "essays/index.sx" "etc/essay/index.sx"

# Philosophy essays → etc/philosophy/
safe_mv "essays/philosophy-index.sx" "etc/philosophy/index.sx"
safe_mv "essays/sx-manifesto.sx" "etc/philosophy/sx-manifesto/index.sx"
safe_mv "essays/godel-escher-bach.sx" "etc/philosophy/godel-escher-bach/index.sx"
safe_mv "essays/sx-and-wittgenstein.sx" "etc/philosophy/wittgenstein/index.sx"
safe_mv "essays/sx-and-dennett.sx" "etc/philosophy/dennett/index.sx"
safe_mv "essays/s-existentialism.sx" "etc/philosophy/existentialism/index.sx"
safe_mv "essays/platonic-sx.sx" "etc/philosophy/platonic-sx/index.sx"

# Regular essays → etc/essay/{name}/index.sx
PHILOSOPHY_ESSAYS="sx-manifesto godel-escher-bach sx-and-wittgenstein sx-and-dennett s-existentialism platonic-sx"
for f in $B/essays/*.sx; do
  [ -f "$f" ] || continue
  base=$(basename "$f")
  name="${base%.sx}"
  [ "$name" = "index" ] && continue
  [ "$name" = "philosophy-index" ] && continue
  skip=false
  for p in $PHILOSOPHY_ESSAYS; do
    if [ "$name" = "$p" ]; then
      skip=true
      break
    fi
  done
  $skip && continue
  safe_mv "essays/$base" "etc/essay/$name/index.sx"
done

echo ""
echo "=== ETC: Plans ==="
safe_mv "plans/index.sx" "etc/plan/index.sx"

# Theorem prover (subdirectory)
safe_mv "plans/theorem-prover/plan-theorem-prover-content.sx" "etc/plan/theorem-prover/index.sx"
safe_mv "plans/theorem-prover/prove-phase1-row.sx" "etc/plan/theorem-prover/_islands/prove-phase1-row.sx"
safe_mv "plans/theorem-prover/prove-phase2-row.sx" "etc/plan/theorem-prover/_islands/prove-phase2-row.sx"

# Regular plan files → etc/plan/{name}/index.sx
for f in $B/plans/*.sx; do
  [ -f "$f" ] || continue
  base=$(basename "$f")
  name="${base%.sx}"
  [ "$name" = "index" ] && continue
  safe_mv "plans/$base" "etc/plan/$name/index.sx"
done

echo ""
echo "=== TOOLS ==="
safe_mv "sx-tools/overview-content.sx" "tools/sx-tools/index.sx"
safe_mv "sx-tools-demos.sx" "tools/sx-tools/_lib/sx-tools-demos.sx"
safe_mv "sx-tools-editor.sx" "tools/sx-tools/_lib/sx-tools-editor.sx"
safe_mv "services-tools.sx" "tools/services/index.sx"
safe_mv "playground/content.sx" "tools/playground/index.sx"
safe_mv "playground/repl.sx" "tools/playground/_islands/repl.sx"

echo ""
echo "======================================="
echo "Migration complete!"
echo "  Moved:    $MOVED"
echo "  Skipped:  $SKIPPED"
echo "  Warnings: $WARNINGS"
echo "======================================="
