{
  config,
  lib,
  namespace,
  pkgs,
  ...
}:
let
  cfg = config.${namespace}.programs.waybar;

  waybar-audio = pkgs.writeScriptBin "waybar-audio" ''
    #!/usr/bin/env bash

    # Extract sink entries: "ID  Description"
    choices=$(pw-dump | jq -r '
      .. | objects
      | select(.["media.class"] == "Audio/Sink")
      | "\(.["object.id"]) \(.["node.description"])"
    '
    )

    # Show wofi menu
    selected=$(printf "%s\n" "$choices" | wofi --dmenu --prompt "Audio Output")

    # Extract ID
    id=$(echo "$selected" | awk '{print $1}')

    # Switch to the selected device
    if [ -n "$id" ]; then
        wpctl set-default "$id"
    fi
  '';
in
{
  imports = [ ../options.nix ];

  config = lib.mkIf cfg.enable {
    home.packages = [ waybar-audio ];
  };
}
