{
  config,
  lib,
  namespace,
  ...
}:
with lib;
let
  inherit (lib.${namespace}) mkOpt;
  cfg = config.${namespace}.services.jellyseerr;
in
{
  options.${namespace}.services.jellyseerr = {
    enable = mkEnableOption "enable jellyseerr";

    port = mkOpt types.int 5055 "jellyseerr port";

    dataDir = mkOpt types.str "" "data dir";
  };

  config = mkIf cfg.enable {
    # Enable jellyseerr service
    services.jellyseerr = {
      enable = true;
      port = cfg.port;
      openFirewall = true;
      configDir = cfg.dataDir;
    };

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

    users.users.jellyseerr = {
      isSystemUser = true;
      group = "jellyseerr";
      home = cfg.dataDir;
    };
    users.groups.jellyseerr = { };
  };
}
