{ config, lib, pkgs, ... }:
with lib;
let
  cfg = config.programs.ntfy;
  format = pkgs.formats.yaml { };
  configFile = format.generate "ntfy.yml" cfg.settings;
in
{
  options.programs.ntfy = {
    enable = mkEnableOption "ntfy";

    package = mkOption {
      default = pkgs.ntfy;
      defaultText = "pkgs.ntfy";
      type = types.package;
    };

    settings = mkOption {
      default = {};
      type = types.submodule {
        freeformType = format.type;
      };
    };
  };

  config = mkIf cfg.enable {
    home.packages = [ cfg.package ];

    xdg.configFile."ntfy/ntfy.yml" = mkIf (cfg.settings != { }) { source = configFile; };
  };
}