{ pkgs, config, lib, ... }:

let
  service = "qbittorrent";
  cfg = config.homelab.${service};
  sec = config.sops.secrets;
  homelab = config.homelab;
in
{
  options.homelab.${service} = {
    enable = lib.mkEnableOption "enables ${service}";

    # set port options
    port = lib.mkOption {
      type = lib.types.int;
      default = 7103;
      description = "set port for ${service} (default: ${toString cfg.port}";
    };
#    torrenting_port = lib.mkOption {
#      type = lib.types.int;
#      default = ;
#      description = "set port for ${service} (default: ${toString cfg.port}";
#    };
    url = lib.mkOption {
      type = lib.types.str;
      default = "qbit.${homelab.base_domain}";
      description = "set domain for ${service}";
    };
    data_dir = lib.mkOption {
      type = lib.types.str;
      default = "/var/lib/${service}";
      description = "set data directory for ${service}";
    };
    ids = lib.mkOption {
      type = lib.types.int;
      default = cfg.port;
      description = "set uid and pid of ${service} user (matches port by default)";
    };
    vpn_inf = lib.mkOption {
      type = lib.types.str;
      default = "enp89s0.69";
      description = "set the interface qbittorrent will be bound to (used to route through vpn)";
    };
    backup = lib.mkOption {
      type = lib.types.bool;
      default = true;
      description = "enable backups for ${service}";
    };
    motd = lib.mkOption {
      type = lib.types.nullOr lib.types.str;
      default = service;
    };
  };

  config = lib.mkIf cfg.enable {

    # ensure media dirs existence
    systemd.tmpfiles.rules = [
      "d /holocron/media/downloads 2775 qbittorrent media -"
  ];

    # declare ${service} user
    users.users.${service} = {
      description = "${service} server user";
      uid = lib.mkForce cfg.ids;
      isSystemUser = true;
      home = cfg.data_dir;
      createHome = true;
      group = "media";
    };

    # enable the qbittorrent service
    services.${service} = {
      enable = true;
      openFirewall = true;
      user = service;
      group = lib.mkForce "media";  # override for permissions
      profileDir = cfg.data_dir;
      webuiPort = cfg.port;
#      torrentingPort = cfg.torrenting_port;
    };

    # override umask to make permissions work out
    systemd.services.${service} = {
      serviceConfig = {
        UMask = lib.mkForce "0007";
      };
    };

    # bind to network interface but allow local access to webui
    networking.firewall.extraCommands = ''
      iptables -F QBIT 2>/dev/null || true
      iptables -X QBIT 2>/dev/null || true
      iptables -N QBIT
      iptables -A OUTPUT -m owner --uid-owner ${toString cfg.ids} -j QBIT
      iptables -A QBIT -o ${cfg.vpn_inf} -j ACCEPT
      iptables -A QBIT -p udp --dport 53 -o ${cfg.vpn_inf} -j ACCEPT
      iptables -A QBIT -p tcp --dport 53 -o ${cfg.vpn_inf} -j ACCEPT
      iptables -A QBIT -o lo -j ACCEPT
      iptables -A QBIT -p tcp -d 127.0.0.1 --dport ${toString cfg.port} -j ACCEPT
      iptables -A QBIT -p tcp -o enp89s0 -d 10.0.0.0/8 --dport ${toString cfg.port} -j ACCEPT
      iptables -A QBIT -j DROP
    '';

# boilerplate for if you ever want to try to get this working again
# ------------------------------------------------------------------------------
#      # add systemd service to VPN network namespace
#      vpnConfinement = {
#        enable = true;
#        vpnNamespace = "wgmex";
#      };
# ------------------------------------------------------------------------------

#    # open firewall
#    networking.firewall.allowedTCPPorts = [ cfg.port ];

    # add to caddy for reverse proxy
    services.caddy.virtualHosts."${cfg.url}" = {
      extraConfig = ''
        tls internal
        reverse_proxy 127.0.0.1:${toString cfg.port}
      '';
    };

    # add to glance
    homelab.glance.links.mediastack = [{
      title = service;
      url = "https://${cfg.url}";
      error-url = "http://${homelab.host_ip}:${toString cfg.port}";
      check-url = "http://${homelab.host_ip}:${toString cfg.port}";
      icon = "di:${service}"; }];

    # add to backups
    homelab.backups.baks = {
      ${service} = { paths = [ cfg.data_dir ]; };
    };
  };
}
