#!/usr/bin/env bash


# 1. Get active tmux sessions
sessions=$(tmux list-sessions -F "#S" 2>/dev/null)

# 2. Get directories (Optimized find)
# Adjust -maxdepth to 3 or 4 if you want deeper subfolders
# dirs=$(fd . ~ ~/personal ~/personal/Projects ~/.config --min-depth 1 --max-depth 3 --type d)
dirs=$(find ~ ~/personal ~/personal/Projects ~/.config \
    -mindepth 1 -maxdepth 2 \
    \( -name ".git" -o -name "node_modules" -o -name ".cache" -o -name "Library" \) -prune \
    -o -type d -print 2>/dev/null)

# 3. Combine them and pipe to fzf
# We use printf to ensure they appear as a clean list

selected=$(printf "$sessions\n$dirs" | fzf \
    --layout=reverse \
    --border=rounded \
    --margin=1 \
    --padding=1 \
    --height=100% \
    --prompt="󱇗 " \
    --color="bg+:#292e42,bg:#1b1d2b,spinner:#bb9af7,hl:#7aa2f7" \
    --color="fg:#c0caf5,header:#7dcfff,info:#bb9af7,pointer:#7dcfff" \
    --color="marker:#9ece6a,fg+:#c0caf5,prompt:#bb9af7,hl+:#7aa2f7" \
    --preview '[[ -d {} ]] && ls -p --color=always {} | head -10 || tmux list-windows -t {} 2>/dev/null' \
    --preview-window="top:35%:border-bottom")
# selected=$(printf "$sessions\n$dirs" | fzf \
#     --height=100% --layout=reverse --border \
#     --prompt="󱇗 Switch to: " --pointer="󰁔" \
#     --color="bg+:#292e42,bg:#1b1d2b,spinner:#bb9af7,hl:#7aa2f7" \
#     --color="fg:#c0caf5,header:#7dcfff,info:#bb9af7,pointer:#7dcfff" \
#     --color="marker:#9ece6a,fg+:#c0caf5,prompt:#bb9af7,hl+:#7aa2f7" \
#     --preview '[[ -d {} ]] && ls -p --color=always {} || tmux list-windows -t {} 2>/dev/null' \
#     --preview-window=right:60%)
#
session_name=$(basename "$selected" | tr . _)

if ! tmux has-session -t "$session_name" 2>/dev/null; then
    tmux new-session -d -s "$session_name" -c "$selected"
fi

if [[ -z $TMUX ]]; then
    exec tmux attach -t "$session_name"
else
    tmux switch-client -t "$session_name"
fi

