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

SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

SRC_PS1="$REPO_ROOT/ps1/ps1.sh"
DST_DIR="$HOME/.config/ps1"
DST_PS1="$DST_DIR/ps1.sh"
SEASON_FILE="$DST_DIR/season"
STYLE_FILE="$DST_DIR/style"

MARKER_START="# >>> user ps1 (managed) >>>"
MARKER_END="# <<< user ps1 (managed) <<<"

season_mode="dynamic"
season_choice=""
style_choice="aurora"
contest_layout="day-time-user"
if [[ -t 0 ]]; then
  style_pack="standard"
  echo "PS1 mode:"
  echo "  1) Single season (static)"
  echo "  2) Dynamic season (changing)"
  echo "  3) Contest pack (static)"
  read -r -p "Choose [3]: " season_mode_choice
  case "${season_mode_choice}" in
    1)
      season_mode="static"
      echo "Pick a season:"
      echo "  1) Winter"
      echo "  2) Spring"
      echo "  3) Summer"
      echo "  4) Autumn"
      read -r -p "Choose [1]: " season_pick
      case "${season_pick}" in
        2) season_choice="spring" ;;
        3) season_choice="summer" ;;
        4) season_choice="autumn" ;;
        ""|1) season_choice="winter" ;;
        *) echo "Invalid choice, using winter."; season_choice="winter" ;;
      esac

      style_pack="standard"
      ;;
    2)
      season_mode="dynamic"
      style_pack="standard"
      ;;
    ""|3)
      season_mode="static"
      style_pack="contest"
      contest_layout="day-time-user"
      echo "Contest layout order:"
      echo "  1) day-time-user"
      echo "  2) user-time"
      echo "  3) time-user"
      echo "  4) day-time"
      read -r -p "Choose [1]: " layout_pick
      case "${layout_pick}" in
        2) contest_layout="user-time" ;;
        3) contest_layout="time-user" ;;
        4) contest_layout="day-time" ;;
        ""|1) contest_layout="day-time-user" ;;
        *) echo "Invalid choice, using day-time-user."; contest_layout="day-time-user" ;;
      esac
      ;;
    *)
      echo "Invalid choice, using dynamic."
      season_mode="dynamic"
      style_pack="standard"
      ;;
  esac

  if [[ "$style_pack" == "contest" ]]; then
    echo
    echo "Style presets:"
    echo "  1) Aurora  - Powerline blocks, seasonal palette, two-line"
    echo "     Preview: [date time user] > [@host /path]"
    echo "  2) Neon    - Magenta/cyan cyber blocks, high contrast"
    echo "     Preview: [user time] > [@host /path]"
    echo "  3) Forge   - Warm copper blocks, heavy frame"
    echo "     Preview: [date time user] > [@host /path]"
    echo "  4) Circuit - Green scanline, minimal rails"
    echo "     Preview: [user time] - @host /path"
    echo "  5) Mono    - Clean monochrome, fast read"
    echo "     Preview: [date time user] @host /path"
    read -r -p "Choose style [1]: " style_pick
    case "${style_pick}" in
      2) style_choice="neon" ;;
      3) style_choice="forge" ;;
      4) style_choice="circuit" ;;
      5) style_choice="mono" ;;
      ""|1) style_choice="aurora" ;;
      *) echo "Invalid choice, using aurora."; style_choice="aurora" ;;
    esac
  else
    style_choice="aurora"
  fi
fi

mkdir -p "$DST_DIR"
cp -f "$SRC_PS1" "$DST_PS1"
chmod 0644 "$DST_PS1"

{
  printf 'PS1_SEASON_MODE=%s\n' "$season_mode"
  if [[ "$season_mode" == "static" && -n "$season_choice" ]]; then
    printf 'PS1_SEASON=%s\n' "$season_choice"
  fi
} > "$SEASON_FILE"
chmod 0644 "$SEASON_FILE"

{
  printf 'PS1_STYLE=%s\n' "$style_choice"
  printf 'PS1_STYLE_PACK=%s\n' "$style_pack"
  if [[ "$style_pack" == "contest" ]]; then
    printf 'PS1_CONTEST_LAYOUT=%s\n' "$contest_layout"
  fi
} > "$STYLE_FILE"
chmod 0644 "$STYLE_FILE"

add_source_block() {
  local file="$1"
  [[ -f "$file" ]] || touch "$file"

  # remove old block
  local tmp
  tmp="$(mktemp)"
  awk -v s="$MARKER_START" -v e="$MARKER_END" '
    $0==s {inside=1; next}
    $0==e {inside=0; next}
    !inside {print}
  ' "$file" > "$tmp"
  cat "$tmp" > "$file"
  rm -f "$tmp"

  cat >> "$file" <<EOF

$MARKER_START
# Load user PS1 (managed)
if [ -r "$DST_PS1" ]; then
  . "$DST_PS1"
fi
$MARKER_END
EOF
}

# Bash on macOS may read .bash_profile for login shells; interactive shells read .bashrc
add_source_block "$HOME/.bashrc"
add_source_block "$HOME/.bash_profile"

echo "✅ macOS user PS1 installert:"
echo "   $DST_PS1"
echo "   sourcet fra ~/.bashrc og ~/.bash_profile"
echo
echo "Tips:"
echo "  - Tving NerdFont glyphs:  export PS1_FORCE_NF=1"
echo "  - Tving fallback:         export PS1_FORCE_ASCII=1"
echo "  - Disable:               export DISABLE_GLOBAL_PS1=1  eller touch ~/.config/ps1/disable"
