Warning: Identity file /home/jpporta/.ssh/id_rsa not accessible: No such file or directory.
Warning: Identity file /home/jpporta/.ssh/id_rsa not accessible: No such file or directory.
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.custom.ntfy-notify;

  # Escape the whole command string for the YAML config. Since ntfy reads this
  # from client.yml directly (not from systemd), we pass notify-send arguments
  # as a shell command string — ntfy will spawn a shell to run it.
  mkSubscription = topic: priority:
    lib.generators.toYaml { } [{
      topic = "${cfg.server}/${topic}";
      command = ''notify-send -a "Kanban" -u "${priority}" "Kanban Update" "''${m}"'';
    }];
in
{
  options.custom.ntfy-notify = {
    enable = lib.mkEnableOption "ntfy desktop notification subscriber";

    server = lib.mkOption {
      type = lib.types.str;
      default = "ntfy.sh";
      description = "ntfy server base URL (e.g. ntfy.sh or self-hosted).";
    };

    subscriptions = lib.mkOption {
      type = lib.types.listOf (lib.types.submodule {
        options = {
          topic = lib.mkOption {
            type = lib.types.str;
            description = "ntfy topic to subscribe to.";
          };
          priority = lib.mkOption {
            type = lib.types.enum [ "low" "normal" "critical" ];
            default = "normal";
            description = "Urgency level passed to notify-send -u.";
          };
        };
      });
      default = [
        {
          topic = "joao-kanban";
          priority = "critical";
        }
      ];
      description = ''
        List of ntfy topics to subscribe to. Each topic gets a notify-send
        command that feeds into the desktop notification daemon (SwayNC).
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    home.packages = [ pkgs.ntfy-sh ];

    # Client config with subscription → notify-send mappings.
    # ntfy subscribe (without args) reads subscriptions from here.
    xdg.configFile."ntfy/client.yml" = {
      text = ''
        # Generated by ndots ntfy-notify module.
        # Each incoming message spawns a notify-send → your notification daemon.
        subscribe:
      '' + lib.concatMapStringsSep "\n" (sub:
        "  - topic: ${cfg.server}/${sub.topic}\n"
        + "    command: >-\n"
        + ''      notify-send -a "Kanban" -u "${sub.priority}" "Kanban Update" "$m"''
      ) cfg.subscriptions;
    };

    systemd.user.services.ntfy-notify = {
      Unit = {
        Description = "ntfy push notification subscriber (kanban → notify-send)";
        After = [ "graphical-session.target" ];
        PartOf = [ "graphical-session.target" ];
      };
      Service = {
        Type = "simple";
        ExecStart = ''
          ${pkgs.ntfy-sh}/bin/ntfy subscribe
        '';
        Restart = "on-failure";
        RestartSec = 10;
      };
      Install.WantedBy = [ "graphical-session.target" ];
    };
  };
}
