{
  config,
  lib,
  namespace,
  ...
}:
with lib;
let
  inherit (lib.${namespace}) mkOpt mkReverseProxyOpt;
  cfg = config.${namespace}.services.calibre;
  # dataDir = "/library";
  # hostAddress = "10.0.1.3";
  # calibreUserId = config.users.users.nix-apps.uid;
  # calibreGroupId = config.users.groups.jallen-nas.gid;

  # calibreConfig =
  #   { lib, ... }:
  #   {
  #     services.calibre-server = {
  #       enable = true;
  #       openFirewall = true;
  #       libraries = [
  #         dataDir
  #       ];
  #     };

  #     services.calibre-web = {
  #       enable = true;
  #       openFirewall = true;
  #       listen = {
  #         ip = "0.0.0.0";
  #         port = cfg.port;
  #       };
  #       options = {
  #         enableBookUploading = true;
  #         enableBookConversion = true;
  #         calibreLibrary = dataDir;
  #       };
  #     };

  # users.users.calibre = {
  #   isSystemUser = true;
  #   uid = lib.mkForce calibreUserId;
  #   group = "calibre";
  # };

  # users.groups = {
  #   calibre-web = {
  #     gid = lib.mkForce calibreGroupId;
  #   };
  # };

  # networking = {
  #   firewall = {
  #     enable = true;
  #     allowedTCPPorts = [ cfg.port ];
  #   };
  #   # Use systemd-resolved inside the container
  #   # Workaround for bug https://github.com/NixOS/nixpkgs/issues/162686
  #   useHostResolvConf = lib.mkForce false;
  # };

  # services.resolved.enable = true;
  # system.stateVersion = "23.11";
  # };

  # bindMounts = {
  #   ${dataDir} = {
  #     hostPath = cfg.dataDir;
  #     isReadOnly = false;
  #   };
  #   "/var/lib/calibre-web" = {
  #     hostPath = cfg.webDir;
  #     isReadOnly = false;
  #   };
  # };

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

  # calibreContainer =
  #   (lib.${namespace}.mkContainer {
  #     name = "calibre";
  #     localAddress = cfg.localAddress;
  #     port = cfg.port;
  #     bindMounts = bindMounts;
  #     config = calibreConfig;
  #   })
  #     { inherit lib; };

  # fullConfig = {
  #   ${namespace}.services.traefik = lib.mkIf cfg.reverseProxy.enable {
  #     reverseProxies = [ reverseProxyConfig ];
  #   };
  # }
  # // calibreContainer;
in
{
  options.${namespace}.services.calibre = {
    enable = mkEnableOption "calibre service";

    port = mkOpt types.int 8080 "Port for calibre to be hosted on";

    webPort = mkOpt types.int 8083 "Port for calibre web to be hosted on";

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

    dataDir = mkOpt types.str "/media/nas/main/books" "Path to the data dir";

    webDir = mkOpt types.str "/media/nas/main/nix-app-data/calibre-web" "Path to the web data dir";

    reverseProxy = mkReverseProxyOpt;
  };

  config = mkIf cfg.enable {
    ${namespace}.services.traefik = lib.mkIf cfg.reverseProxy.enable {
      reverseProxies = [ reverseProxyConfig ];
    };

    services = {
      calibre-server = {
        enable = false;
        openFirewall = true;
        port = cfg.port;
        libraries = [
          cfg.dataDir
        ];
      };

      calibre-web = {
        enable = true;
        openFirewall = true;
        dataDir = cfg.webDir;
        listen = {
          ip = "0.0.0.0";
          port = cfg.webPort;
        };
        options = {
          enableBookUploading = true;
          enableBookConversion = true;
          calibreLibrary = cfg.dataDir;
        };
      };
    };
  };
}
