{
  config,
  lib,
  ...
}: let
  cfg = config.programs.dank-material-shell;
in {
  imports = [
    ./dms-rename.nix
  ];

  options.programs.dank-material-shell = {
    niri = {
      enableKeybinds = lib.mkEnableOption "DankMaterialShell niri keybinds";
      enableSpawn = lib.mkEnableOption "DankMaterialShell niri spawn-at-startup";
      includes = {
        enable =
          (lib.mkEnableOption "includes for niri-flake")
          // {
            default = true;
          };
        override = lib.mkOption {
          type = lib.types.bool;
          description = ''
            Whether DMS settings will be prioritized over settings defined in niri-flake or not
          '';
          default = true;
          example = false;
        };
        originalFileName = lib.mkOption {
          type = lib.types.str;
          description = ''
            A new name for the config file generated by niri-flake
          '';
          default = "hm";
          example = "niri-flake";
        };
        filesToInclude = lib.mkOption {
          type = lib.types.listOf lib.types.str;
          description = ''
            A list of dms-generated files to include
          '';
          default = [
            "alttab"
            "binds"
            "colors"
            "cursor"
            "layout"
            "outputs"
            "windowrules"
            "wpblur"
          ];
          example = [
            "outputs"
            "wpblur"
          ];
        };
      };
    };
  };

  config = lib.mkIf cfg.enable {
    warnings = (
      lib.optional (cfg.niri.enableKeybinds && cfg.niri.includes.enable) ''
        It is not recommended to use both `enableKeybinds` and `includes.enable` at the same time.
      ''
    );

    # HACK: niri-flake does not support config includes yet, but we can "fix" that
    # TODO: replace with proper config includes after https://github.com/sodiboo/niri-flake/pull/1548 merge
    xdg.configFile = lib.mkIf cfg.niri.includes.enable (
      let
        cfg' = cfg.niri.includes;

        withOriginalConfig = dmsFiles:
          if cfg'.override
          then [cfg'.originalFileName] ++ dmsFiles
          else dmsFiles ++ [cfg'.originalFileName];

        fixes = map (fix: "\n${fix}") (
          lib.optional (cfg'.enable && config.programs.niri.settings.layout.border.enable)
          # kdl
          ''
            // Border fix
            // See https://yalter.github.io/niri/Configuration%3A-Include.html#border-special-case for details
            layout { border { on; }; }
          ''
        );
      in {
        niri-config.target = lib.mkForce "niri/${cfg'.originalFileName}.kdl";
        niri-config-dms = {
          target = "niri/config.kdl";
          text = lib.pipe cfg'.filesToInclude [
            (map (filename: "dms/${filename}"))
            withOriginalConfig
            (map (filename: "include optional=true \"${filename}.kdl\""))
            (files: files ++ fixes)
            (builtins.concatStringsSep "\n")
          ];
        };
      }
    );

    programs.niri.settings = lib.mkMerge [
      (lib.mkIf cfg.niri.enableKeybinds {
        binds = with config.lib.niri.actions; let
          dms-ipc = spawn "dms" "ipc";
        in
          {
            "Mod+Space" = {
              action = dms-ipc "spotlight" "toggle";
              hotkey-overlay.title = "Toggle Application Launcher";
            };
            "Mod+N" = {
              action = dms-ipc "notifications" "toggle";
              hotkey-overlay.title = "Toggle Notification Center";
            };
            "Mod+Comma" = {
              action = dms-ipc "settings" "toggle";
              hotkey-overlay.title = "Toggle Settings";
            };
            "Mod+P" = {
              action = dms-ipc "notepad" "toggle";
              hotkey-overlay.title = "Toggle Notepad";
            };
            "Super+Alt+L" = {
              action = dms-ipc "lock" "lock";
              hotkey-overlay.title = "Toggle Lock Screen";
            };
            "Mod+X" = {
              action = dms-ipc "powermenu" "toggle";
              hotkey-overlay.title = "Toggle Power Menu";
            };
            "XF86AudioRaiseVolume" = {
              allow-when-locked = true;
              action = dms-ipc "audio" "increment" "3";
            };
            "XF86AudioLowerVolume" = {
              allow-when-locked = true;
              action = dms-ipc "audio" "decrement" "3";
            };
            "XF86AudioMute" = {
              allow-when-locked = true;
              action = dms-ipc "audio" "mute";
            };
            "XF86AudioMicMute" = {
              allow-when-locked = true;
              action = dms-ipc "audio" "micmute";
            };
            "XF86MonBrightnessUp" = {
              allow-when-locked = true;
              action = dms-ipc "brightness" "increment" "5" "";
            };
            "XF86MonBrightnessDown" = {
              allow-when-locked = true;
              action = dms-ipc "brightness" "decrement" "5" "";
            };
            "Mod+Alt+N" = {
              allow-when-locked = true;
              action = dms-ipc "night" "toggle";
              hotkey-overlay.title = "Toggle Night Mode";
            };
            "Mod+V" = {
              action = dms-ipc "clipboard" "toggle";
              hotkey-overlay.title = "Toggle Clipboard Manager";
            };
          }
          // lib.attrsets.optionalAttrs cfg.enableSystemMonitoring {
            "Mod+M" = {
              action = dms-ipc "processlist" "toggle";
              hotkey-overlay.title = "Toggle Process List";
            };
          };
      })

      (lib.mkIf cfg.niri.enableSpawn {
        spawn-at-startup = [
          {
            command = [
              "dms"
              "run"
            ];
          }
        ];
      })
    ];
  };
}
