set -s extended-keys on
set -as terminal-features 'xterm*:extkeys'
set -ga terminal-overrides ",xterm*:Tc"
set -sg escape-time 0
set -g history-limit 50000

# Set Fish as default shell (OS-aware path)
if-shell "uname | grep -q Darwin" \
    "set-option -g default-shell /opt/homebrew/bin/fish; set-option -g default-command /opt/homebrew/bin/fish" \
    "set-option -g default-shell /usr/bin/fish; set-option -g default-command /usr/bin/fish"

unbind C-Enter

# --- Windows start at 1, auto-renumber ---
set -g base-index 1
setw -g pane-base-index 1
set -g renumber-windows on

# --- Mouse ---
set -g mouse on

# --- Pane navigation (prefix + hjkl) ---
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# --- Pane resizing (prefix + C-hjkl, repeatable) ---
bind -r C-h resize-pane -L 4
bind -r C-j resize-pane -D 2
bind -r C-k resize-pane -U 2
bind -r C-l resize-pane -R 4

# --- Window navigation ---
bind C-p previous-window
bind C-n next-window

set -g repeat-time 1000

# --- Splits open in same directory ---
bind v split-window -h -c "#{pane_current_path}"
bind x split-window -v -c "#{pane_current_path}"
unbind '"'
unbind %

# --- i3-style pane moving (prefix + H/J/K/L swaps panes) ---
# Try directional swap first; if no pane in that direction, break out and rejoin
bind H run-shell 'tmux swap-pane -s "{left-of}" 2>/dev/null; true'
bind J run-shell 'tmux swap-pane -s "{down-of}" 2>/dev/null; true'
bind K run-shell 'tmux swap-pane -s "{up-of}" 2>/dev/null; true'
bind L run-shell 'tmux swap-pane -s "{right-of}" 2>/dev/null; true'

# --- Window moving (prefix + < / > shifts window position) ---
bind -r < swap-window -t -1\; select-window -t -1
bind -r > swap-window -t +1\; select-window -t +1

# --- Break pane out / join pane back ---
# prefix + ! = break current pane into its own window (built-in)
# prefix + @ = pick a window and pull it back as a pane
bind @ command-prompt -p "join pane from window:" "join-pane -s '%%'"

# --- New window in same directory ---
bind c new-window -c "#{pane_current_path}"

# --- SSH into new window ---
bind S command-prompt -p "ssh:" "new-window -n '%1' 'ssh %1'"

# --- Reload config ---
bind r source-file ~/.tmux.conf \; display "Config reloaded"

# --- Status bar ---
set -g status-right ""
set -g status-style 'bg=#f5bde6 fg=#24273a'
set -g pane-active-border-style "bg=default fg=#f5bde6"

# --- Copy/paste (system clipboard) ---
set -g set-clipboard on
setw -g mode-keys vi
bind -T copy-mode-vi v send -X begin-selection
bind -T copy-mode-vi y send -X copy-pipe-and-cancel "wl-copy 2>/dev/null || xclip -selection clipboard 2>/dev/null || xsel --clipboard --input"
bind -T copy-mode-vi MouseDragEnd1Pane send -X copy-pipe-and-cancel "wl-copy 2>/dev/null || xclip -selection clipboard 2>/dev/null || xsel --clipboard --input"

# --- Plugins ---
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'
set -g @resurrect-strategy-nvim 'session'

run '~/.tmux/plugins/tpm/tpm'
