# Useful variable definitions
export LANG=en_US.UTF-8
export EDITOR='nvim'
export PATH=$HOME/bin:$HOME/.local/bin:/usr/local/bin:$PATH
export ZSH="$HOME/.oh-my-zsh"

# Personal Directories
export CONFIG_DIR="$HOME/Config"
export NVIM_DIR="$CONFIG_DIR/Neovim"
export MANAGED_CONFIG_DIR="$CONFIG_DIR/Main/configs"

ZSH_THEME="spaceship"
CASE_SENSITIVE="true"
plugins=(git)

zstyle ':omz:update' mode reminder
source $ZSH/oh-my-zsh.sh

# Aliases
alias vim='nvim'
alias backup='rsync -ahv --info=progress2 --no-i-r --partial'

# Spaceship Settings
SPACESHIP_PROMPT_ORDER=(
  time          # Time stamps
  dir           # Current directory
  git           # Git branch
  exec_time     # Execution time
  rust
  line_sep      # Line break
  jobs          # Background jobs indicator
  user          # Username section
  host          # Hostname section
  exit_code     # Exit code section
  char          # Prompt character
)

# User and Hostname Config
SPACESHIP_HOST_SHOW="always"
SPACESHIP_HOST_PREFIX="@"
SPACESHIP_USER_PREFIX=""
SPACESHIP_USER_SUFFIX=""
SPACESHIP_USER_SHOW="always"

# Date and Time Config
SPACESHIP_TIME_SHOW=true
SPACESHIP_TIME_FORMAT='%D{%H:%M:%S.%.}'

# Unknown
SPACESHIP_DIR_TRUNC=0
SPACESHIP_DIR_TRUNC_REPO=false

# Git
SPACESHIP_GIT_ORDER=(git_branch git_commit git_status)
SPACESHIP_GIT_COMMIT_SHOW=true
SPACESHIP_GIT_COMMIT_PREFIX=" at "

# Rust Spaceship Config
SPACESHIP_RUST_SHOW=true

# Source Cargo Directory
source $HOME/.cargo/env

# Launch or attach to a Tmux session by default if not already in TMUX
# if command -v tmux &> /dev/null && [ -n "$PS1" ] && [[ ! "$TERM" =~ screen ]] && [[ ! "$TERM" =~ tmux ]] && [ -z "$TMUX" ]; then
#     # Try to attach to an existing tmux session; if none, create a new one
#     tmux attach || exec tmux
# fi

# Function to move and manage config directories as needed
manage() {
    if [ $# -ne 1 ]; then
        echo "Usage: manage <config file or dir>"
        return 1
    fi

    local src="$(realpath -s "$1")" # Don't resolve symlinks here. We need to check first.
    local dst="$(realpath "$MANAGED_CONFIG_DIR")"
    local src_name="$(basename "$src")"
    local dst_path="$dst/$src_name"

    # Ensure the given path is not a symlink
    if [ -L "$src" ]; then
        echo "$src is a symlink. Not managing."
        return 1
    fi

    # Not a symlink. Re-resolve with actual path in case symlinks exist higher up the chain.
    src="$(realpath $src)"

    # Ensure the given path is not already managed
    echo "Checking if $src is a child of $dst"
    case "$src" in
        "$dst"|"$dst"/*)
        echo "Specified directory path is already being managed."
        return 1
        ;;
    esac

    mv -n "$src" "$dst_path" || {
        echo "Failed to move $src to $dst"
        return 1
    }

    ln -s "$dst_path" "$src" || {
        echo "Failed to create symlink $src -> $dst"
        return 1
    }

    local CWD="$(pwd)"

    # Determine if we should commit this newly managed config.
    if [ read -q "choice?Would you like to commit this directory? [n]: " ]; then
        cd "$dst_path"
        git add .
        git commit -m "Begin managing $src_name config"
        cd "$CWD"
    else
        echo
        echo "Not commiting config to repo."
    fi

    # Determine if user would like to move to the config dir
    if [ read -q "choice?Would you like to move to the config directory? [n]: " ]; then
        if [ -d "$dst_path" ]; then
            cd "$dst_path"
        else
            cd "$dst"
        fi
    else
        echo "Not leaving current directory."
    fi

    return 0
}

# Add zoxide
eval "$(zoxide init zsh)"

# MACOS
# zoxide setup
#eval "$(zoxide init zsh)"
#alias cd="z"
#
#alias download="yt-dlp -f bestvideo+bestaudio --merge-output-format mp4 --embed-thumbnail --embed-subs --write-auto-sub --sub-lang \"en.*\" --embed-chapters --embed-metadata --embed-info-json -o '%(timestamp>%Y%m%d - %H%M%S)s - %(id)s - %(uploader)s - %(title)s.%(ext)s'"
#
## Keep both x86_64 and arm64 Homebrew
#if [ "$(arch)" = "arm64" ]; then
#    eval "$(/opt/homebrew/bin/brew shellenv)"
#else
#    eval "$(/usr/local/bin/brew shellenv)"
#fi
