{
  config,
  lib,
  namespace,
  ...
}:
with lib;
let
  inherit (lib.${namespace}) mkOpt mkReverseProxyOpt;
  cfg = config.${namespace}.services.uptime-kuma;

  uptime-kumaConfig = {
    services.uptime-kuma = {
      enable = true;
      appriseSupport = true;
      settings = {
        HOST = "0.0.0.0";
        PORT = "${toString cfg.port}";
        # DATA_DIR = lib.mkForce cfg.dataDir;
      };
    };

    # systemd.services = {
    #   uptime-kuma = {
    #     serviceConfig = {
    #       WorkingDirectory = lib.mkForce cfg.dataDir;
    #       StateDirectory = lib.mkForce null; # cfg.dataDir;
    #       StateDirectoryMode = lib.mkForce 700;
    #       DynamicUser = lib.mkForce false;
    #       ProtectSystem = lib.mkForce false;
    #     };
    #   };
    # };

    # users.users.uptime-kuma = {
    #   isSystemUser = true;
    #   group = "uptime-kuma";
    #   home = cfg.dataDir;
    # };
    # users.groups.uptime-kuma = {};
  };

  # Create reverse proxy configuration using mkReverseProxy
  reverseProxyConfig = lib.${namespace}.mkReverseProxy {
    name = "uptime-kuma";
    subdomain = cfg.reverseProxy.subdomain;
    url = "http://${cfg.localAddress}:${toString cfg.port}";
    middlewares = cfg.reverseProxy.middlewares;
  };

  fullConfig = {
    "${namespace}".services.traefik = lib.mkIf cfg.reverseProxy.enable {
      reverseProxies = [ reverseProxyConfig ];
    };
  }
  // uptime-kumaConfig;
in
{
  options.${namespace}.services.uptime-kuma = {
    enable = mkEnableOption "uptime-kuma service";

    port = mkOpt types.int 4000 "Port for uptime-kuma to be hosted on";

    localAddress = mkOpt types.str "127.0.0.1" "local address of the service";

    dataDir = mkOpt types.str "" "Path to the data dir";

    reverseProxy = mkReverseProxyOpt;
  };

  config = mkIf cfg.enable fullConfig;
}
