{ pkgs, lib, config, ... }:
let
  cfg = config.mods.server.audio;
  arr.read.port = config.mods.server.local.ports.readarr;
  arr.prowl.port = config.mods.server.local.ports.prowlarr;
  arr.flare.port = config.mods.server.local.ports.flaresolverr;
  arr.torrent.port = config.mods.server.local.ports.torrent;
  port = config.mods.server.nginx.ports.audio;
in with lib; {
  options.mods.server = {
    audio = {
      enable = mkEnableOption {
        default = false;
        description = "enables audiobook servers";
      };
    };
  };

  config = mkIf cfg.enable {
    services.readarr = {
      enable = true;
      openFirewall = true;
      settings.server.port = arr.read.port;
    };

    services.prowlarr = {
      enable = true;
      openFirewall = true;
      settings.server.port = arr.prowl.port;
    };

    services.flaresolverr = {
      enable = true;
      openFirewall = true;
      port = arr.flare.port;
    };

    services.transmission = {
      enable = true;
      openFirewall = true;
      openRPCPort = true;
      port = arr.torrent.port;
      settings = {
        download-dir = "/storage1/download";
        rpc-port = arr.torrent.port;
        rpc-bind-address = "0.0.0.0";
      };
    };

    services.audiobookshelf = {
      inherit port;
      enable = true;
      openFirewall = true;
      host = "0.0.0.0";
    };
  };
}
