{ config, pkgs, ... }:

let
  lib = pkgs.lib;
  qs = "${pkgs.quickshell}/bin/qs";

  # Niri built-in actions
  inherit (config.lib.niri.actions)
    spawn
    close-window
    maximize-column
    screenshot-window
    focus-column-left
    focus-column-right
    focus-workspace-up
    focus-workspace-down
    quit
    ;

  # Helper to call QuickShell
  qsCall =
    args:
    spawn (
      [
        qs
        "-c"
        "DankMaterialShell"
        "ipc"
        "call"
      ]
      ++ args
    );

  # Generate workspace focus binds (Mod+1 to Mod+9)
  wsBinds = builtins.listToAttrs (
    builtins.genList
      (i: {
        name = "Mod+${toString (i + 1)}";
        value.action.focus-workspace = i + 1;
      }) 9
  );

in
{
  programs.niri.settings.binds = lib.mkMerge [
    # --- Static Keybindings ---
    {
      # --- Media Keys ---

      # --- Audio / Brightness ---
      "XF86AudioMute" = {
        allow-when-locked = true;
        action = qsCall [
          "audio"
          "mute"
        ];
      };
      "XF86AudioMicMute" = {
        allow-when-locked = true;
        action = qsCall [
          "audio"
          "micmute"
        ];
      };
      "XF86AudioRaiseVolume" = {
        allow-when-locked = true;
        action = qsCall [
          "audio"
          "increment"
          "5"
        ];
      };
      "XF86AudioLowerVolume" = {
        allow-when-locked = true;
        action = qsCall [
          "audio"
          "decrement"
          "5"
        ];
      };
      "XF86MonBrightnessUp" = {
        allow-when-locked = true;
        action = qsCall [
          "brightness"
          "increment"
          "5"
          "amdgpu_bl1"
        ];
      };
      "XF86MonBrightnessDown" = {
        allow-when-locked = true;
        action = qsCall [
          "brightness"
          "decrement"
          "5"
          "amdgpu_bl1"
        ];
      };

      # --- System Actions ---
      "Ctrl+Alt+L".action = qsCall [
        "lock"
        "lock"
      ];
      "Mod+V".action = qsCall [
        "clipboard"
        "toggle"
      ];
      "Mod+U".action = qsCall [
        "settings"
        "toggle"
      ];
      "Mod+M".action = qsCall [
        "processlist"
        "toggle"
      ];

      # --- Screenshots ---
      "Print".action.screenshot-screen = {
        write-to-disk = true;
      };
      "Mod+Shift+Alt+S".action = screenshot-window;
      "Mod+Shift+S".action.screenshot = {
        show-pointer = false;
      };

      # --- Launchers ---
      "Mod+D".action = spawn (lib.getExe pkgs.fuzzel);
      "Mod+Return".action = spawn (lib.getExe pkgs.alacritty);

      # --- Window / Layout ---
      "Mod+W".action = close-window;
      "Mod+F".action = maximize-column;

      # --- Navigation ---
      "Mod+Left".action = focus-column-left;
      "Mod+Right".action = focus-column-right;
      "Mod+Down".action = focus-workspace-down;
      "Mod+Up".action = focus-workspace-up;

      "Mod+Shift+E".action = quit;
    }

    # --- Dynamic Workspace Binds ---
    wsBinds
  ];
}
