{
  lib,
  cfg,
  pkgs,
  configDir,
  seshConfig,
  opensessionsConfigJson,
  tmux-dotbar,
  tmux-dark-notify,
  tmux-session-dots,
  tmux-agent-status,
  tmux-palette,
  ...
}:
with lib;
{
  "tmux/plugins/tmux-which-key/config.yaml".source = "${configDir}/tmux/which-key.yaml";

  "sesh/sesh.toml" = mkIf (cfg.sesh.sessions != [ ]) {
    text = seshConfig;
  };

  "opensessions/plugins/hunk.js" = mkIf cfg.opensessions.enable {
    source = "${configDir}/opensessions/plugins/hunk.js";
  };

  # Keep tmux-palette aligned with the default tmux dark theme declaratively.
  "tmux-palette/theme.json".text = ''
    ${builtins.toJSON { name = "catppuccin-mocha"; }}
  '';

  "tmux/open-opensessions-config.sh" = mkIf cfg.opensessions.enable {
    executable = true;
    text = ''
      #!${pkgs.bash}/bin/bash
      set -euo pipefail

      config_dir="''${XDG_CONFIG_HOME:-$HOME/.config}/opensessions"
      config_path="$config_dir/config.json"

      ${pkgs.coreutils}/bin/mkdir -p "$config_dir"

      if [[ ! -f "$config_path" ]]; then
        ${pkgs.coreutils}/bin/printf '%s\n' ${escapeShellArg opensessionsConfigJson} > "$config_path"
      fi

      editor="''${VISUAL:-''${EDITOR:-nvim}}"
      exec "$editor" "$config_path"
    '';
  };

  "tmux" = {
    source = "${configDir}/tmux";
    recursive = true;
  };

  # Nix-generated theme files so dark-notify can re-run dotbar on switch
  "tmux/theme-dark-generated.conf".text = ''
    source '${configDir}/tmux/theme-dark.conf'
    run-shell ${tmux-dotbar}/dotbar.tmux
    run-shell ${pkgs.tmuxPlugins.prefix-highlight}/share/tmux-plugins/prefix-highlight/prefix_highlight.tmux
  '';

  "tmux/theme-light-generated.conf".text = ''
    source '${configDir}/tmux/theme-light.conf'
    run-shell ${tmux-dotbar}/dotbar.tmux
    run-shell ${pkgs.tmuxPlugins.prefix-highlight}/share/tmux-plugins/prefix-highlight/prefix_highlight.tmux
  '';

  "tmux/extraInit".text = ''
    # This file is auto-generated by nixos, don't edit by hand!

    # Load theme config FIRST (sets dotbar color options and pane/border styling)
    # Must happen before dotbar.tmux runs to pick up @tmux-dotbar-* options
    ${concatMapStrings (path: ''
      source '${path}'
    '') cfg.rcFiles}

    # tmux-which-key: discoverable keybinding menu
    set -g @tmux-which-key-xdg-enable 1
    run-shell 'chmod -R u+w "$HOME/.local/share/tmux/plugins/tmux-which-key" 2>/dev/null; true'
    # Keep reload resilient if plugin startup exits non-zero.
    run-shell '${pkgs.tmuxPlugins.tmux-which-key}/share/tmux-plugins/tmux-which-key/plugin.sh.tmux || true'

    # Dotbar theme (must run before prefix-highlight)
    run-shell ${tmux-dotbar}/dotbar.tmux

    # Plugins (prefix-highlight can now replace #{prefix_highlight} placeholder)
    run-shell ${pkgs.tmuxPlugins.copycat}/share/tmux-plugins/copycat/copycat.tmux
    run-shell ${pkgs.tmuxPlugins.prefix-highlight}/share/tmux-plugins/prefix-highlight/prefix_highlight.tmux
    run-shell ${pkgs.tmuxPlugins.yank}/share/tmux-plugins/yank/yank.tmux

    # tmux-dark-notify: auto dark/light mode switching (macOS only)
    # Uses generated theme files that re-run dotbar on switch
    # NOTE: Can't use run-shell for main.tmux — its background daemon causes
    # run-shell to hang waiting for child processes. Start it detached instead.
    set -g @dark-notify-theme-path-light '$HOME/.config/tmux/theme-light-generated.conf'
    set -g @dark-notify-theme-path-dark '$HOME/.config/tmux/theme-dark-generated.conf'
    run-shell -b "${tmux-dark-notify}/main.tmux > /dev/null 2>&1"

    # tmux-smooth-scroll: Rust-based smooth scrolling (replaces azorng/tmux-smooth-scroll)
    # Mouse scroll disabled — causes spurious copy-mode entry in Pi panes
    set -g @smooth-scroll-mouse false
    run-shell '${pkgs.my.tmux-smooth-scroll}/bin/tmux-smooth-scroll init'

    # tmux-palette: Raycast-style tmux command palette
    bind-key -n C-Space run-shell '${tmux-palette}/bin/tmux-palette.sh'

    # tmux-smart-name: window naming + AI agent status
    run-shell ${pkgs.my.tmux-smart-name}/share/tmux-smart-name/scripts/smart-name.sh

    ${optionalString cfg.experimental.sessionDots.enable ''
      # tmux-session-dots: prepend visual session indicator if missing
      if-shell -F '#{m:*#{session_dots}*,#{status-right} #{status-left}}' 'display-message -p ""' 'set -g status-right "#{session_dots} #{status-right}"'
      run-shell ${tmux-session-dots}/session-dots.tmux
    ''}

    ${optionalString cfg.experimental.agentStatus.enable ''
      # tmux-agent-status (experimental): custom keys to avoid local binding conflicts
      set -g @agent-switcher-style "both"
      set -g @agent-status-display-method "popup"
      set -g @agent-status-key "S"
      set -g @agent-sidebar-key "b"
      set -g @agent-next-done-key "N"
      set -g @agent-wait-key "W"
      set -g @agent-park-key "U"
      set -g @agent-sidebar-width "32"
      set -g @agent-sidebar-toggle-cmd "${tmux-agent-status}/scripts/sidebar-toggle.sh"
      run-shell ${tmux-agent-status}/tmux-agent-status.tmux
    ''}

    ${optionalString cfg.opensessions.enable ''
      # opensessions: tmux sidebar + command table
      ${optionalString (cfg.opensessions.key != "") ''set -g @opensessions-key "${cfg.opensessions.key}"''}
      ${optionalString (cfg.opensessions.focusKey != "") ''set -g @opensessions-focus-key "${cfg.opensessions.focusKey}"''}
      ${optionalString (cfg.opensessions.prefixKey != "") ''set -g @opensessions-prefix-key "${cfg.opensessions.prefixKey}"''}
      ${optionalString (cfg.opensessions.prefixFocusKey != "") ''set -g @opensessions-prefix-focus-key "${cfg.opensessions.prefixFocusKey}"''}
      ${optionalString (cfg.opensessions.prefixToggleKey != "") ''set -g @opensessions-prefix-toggle-key "${cfg.opensessions.prefixToggleKey}"''}
      ${optionalString (cfg.opensessions.prefixIndexKeys != "") ''set -g @opensessions-prefix-index-keys "${cfg.opensessions.prefixIndexKeys}"''}
      set -g @opensessions-width "${toString cfg.opensessions.width}"
      set-environment -g BUN_PATH "${pkgs.bun}/bin/bun"
      set-environment -g OPENSESSIONS_HOST "${cfg.opensessions.host}"
      set-environment -g OPENSESSIONS_PORT "${toString cfg.opensessions.port}"
      set-environment -g OPENSESSIONS_PATH_PREFIX "${pkgs.curl}/bin:${pkgs.unstable.fzf}/bin:${pkgs.bun}/bin"

      run-shell "sh '$TMUX_HOME/opensessions.sh'"
    ''}
  '';
}
