#!/usr/bin/env bash
# Optional: tmuxp + completions + example sessions

# 1. Install tmuxp (if not already done via pip/poetry/whatever)
# pip install --user tmuxp    # or use your preferred method

# 2. Install shtab for shell completions (highly recommended)
sudo apt update && sudo apt install -y tmuxp python3-shtab # or pip install shtab

# 3. Generate completions
# Adjust paths if your distro uses different locations

# Zsh
ZSH_COMP_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/zsh/site-functions"
# ZSH_COMP_DIR="/usr/share/zsh/vendor-completions"
mkdir -p "$ZSH_COMP_DIR"
shtab --shell=zsh tmuxp.cli.create_parser | sudo tee "$ZSH_COMP_DIR/_tmuxp" >/dev/null

# Bash
BASH_COMP_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion/completions"
# BASH_COMP_DIR="/usr/share/bash-completion/completions"
mkdir -p "$BASH_COMP_DIR"
shtab --shell=bash tmuxp.cli.create_parser | sudo tee "$BASH_COMP_DIR/tmuxp" >/dev/null

echo "Completions installed. You may need to restart your shell or run:"
echo "  source ~/.zshrc    # or source ~/.bashrc"

# 4. Optional: create example tmuxp configs in ~/.config/tmuxp/
mkdir -p ~/.config/tmuxp

conf_print_tmuxp_yaml() {
  cat <<'EOF'
session_name: yazi-plugin-dev
start_directory: ./

before_script: tmux run-shell -b 'mise run watch'

windows:
  - window_name: debug-env
    layout: main-vertical
    options:
      main-pane-width: 60%
    panes:
      - shell_command:
          - nvim ${FILE_TO_EDIT:-.}
        focus: true
      - shell_command:
          - yazi
      - shell_command:
          - "> ~/.local/state/yazi/yazi.log && tail -F ~/.local/state/yazi/yazi.log"
EOF
}
conf_print_tmuxp_yaml | tee ~/.config/tmuxp/yazi-plugin-dev.yaml
conf_print_tmuxp_yaml | tee "${PLUGIN_DIR}/.tmuxp.yaml"

# Dince we run mise we can set variables in the plugin dir
# .mise,toml rather than a tmuxp yml file.we can then call
# it with `mise run dev`
conf_print_mise_toml() {
  cat <<EOF
[env]
# This replaces the need for the 'ydev' function's exports
FILE_TO_EDIT = "main.lua"
YAZI_LOG = "debug"

[tasks.dev]
description = "Launch Yazi development environment"
run = "tmuxp load yazi-plugin-dev"

[tasks.watch]
description = "Watch for changes and reload Yazi automatically"
run = "ls *.lua | entr -n yazi --remote reload"
EOF
}
conf_print_mise_toml | tee "${PLUGIN_DIR}/.mise.toml"

conf_print_nvim_yazi-reload() {
  cat <<EOF
-- Quick reload for Yazi plugins during development
vim.keymap.set("n", "<leader>yr", function()
  vim.fn.system("yazi --remote reload")
  print("Yazi reloaded!")
end, { desc = "Yazi: Remote Reload" })
EOF
}
# conf_print_nvim_yazi-reload | tee ~/.config/nvim/after/plugin/yazi-reload.lua

cat >~/.config/tmuxp/docker.yaml <<'EOF'
session_name: docker
start_directory: ./
windows:
- window_name: dockerfiles
  layout: tiled
  panes:
  - vim Dockerfile
  - bash
  - bash
- window_name: compose
  panes:
  - docker compose up
EOF

cat >~/.config/tmuxp/dev.yaml <<'EOF'
session_name: dev
start_directory: ~/projects/myapp
windows:
- window_name: code
  layout: main-horizontal
  options:
    main-pane-height: 60%
  panes:
  - vim
  - bash
- window_name: server
  panes:
  - npm run dev    # or cargo run, poetry run, etc.
- window_name: logs
  panes:
  - tail -f logs/app.log
EOF

echo "Example tmuxp sessions created:"
echo "  ~/.config/tmuxp/docker.yaml"
echo "  ~/.config/tmuxp/dev.yaml"
echo ""
echo "Usage examples:"
echo "  tmuxp load docker          # from any dir"
echo "  tmuxp load .               # if .tmuxp.yaml exists in project"
echo "  tmuxp ls                   # list available configs"
