#!/usr/bin/env bash

# Bootstrap a fresh project from this template.
# - Updates README heading with the new project name.
# - Creates an initial commit if none exists.
# - Optionally rewires git remotes.
# - Touches package.json if you want to set the name field (only when present).

set -euo pipefail

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
repo_root="$(cd "${script_dir}/.." && pwd)"

echo "🚀 Template bootstrap"

read -r -p "Project name (e.g. \"Atlas Console\"): " project_name
if [[ -z "${project_name}" ]]; then
  echo "Project name cannot be empty. Aborting."
  exit 1
fi

default_slug="$(echo "${project_name}" | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | tr -cd '[:alnum:]-')"
read -r -p "Project slug [${default_slug}]: " project_slug
project_slug="${project_slug:-$default_slug}"

# Update README heading if it still contains the template title.
readme="${repo_root}/README.md"
if grep -q "^Development Project Template" "${readme}"; then
  echo "Updating README title..."
  tmp_readme="$(mktemp)"
  {
    echo "${project_name}"
    echo "${project_name//?/=}"
    tail -n +3 "${readme}"
  } > "${tmp_readme}"
  mv "${tmp_readme}" "${readme}"
else
  echo "README already customised; skipping title update."
fi

# Update package.json name if present.
package_json="${repo_root}/package.json"
if [[ -f "${package_json}" ]]; then
  if command -v jq >/dev/null 2>&1; then
    echo "Updating package.json name → ${project_slug}"
    tmp_package="$(mktemp)"
    jq --arg name "${project_slug}" '.name = $name' "${package_json}" > "${tmp_package}"
    mv "${tmp_package}" "${package_json}"
  else
    echo "jq is not installed; skipping package.json update. Install jq and rerun if needed."
  fi
else
  echo "No package.json found; skipping package rename."
fi

# Offer to update git origin remote.
if git -C "${repo_root}" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
  current_remote="$(git -C "${repo_root}" remote get-url origin 2>/dev/null || true)"
  echo "Current git remote: ${current_remote:-<none>}"
  read -r -p "Update git remote? (y/N): " change_remote
  if [[ "${change_remote,,}" == "y" ]]; then
    read -r -p "New remote URL: " remote_url
    if git -C "${repo_root}" remote | grep -q "^origin$"; then
      git -C "${repo_root}" remote set-url origin "${remote_url}"
    else
      git -C "${repo_root}" remote add origin "${remote_url}"
    fi
    echo "Origin remote updated."
  else
    echo "Skipping remote update."
  fi
fi

# Stamp a .project-name file so scripts/tools can read the canonical name.
echo "${project_name}" > "${repo_root}/.project-name"

cat <<EOT

✅ Bootstrap complete!
- README updated (if the template title was untouched).
- package.json name set to ${project_slug} (if package.json exists).
- Project name written to .project-name.

Next steps:
1. Review docs/bootstrapping.md and keep working through the checklist.
2. Remove or adapt example tests under __tests__/.
3. Replace template copy and assets with your project's branding.

Happy building!
EOT
