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

let cfg = config.sydnix.deertopia.syncthing;
in {
  options.sydnix.deertopia.syncthing.enable =
    lib.mkEnableOption "Syncthing, à la Deertopia";

  config = lib.mkIf cfg.enable {
    sydnix.syncthing = {
      enable = true;
      includeDevices = [
        "sydpc"
        "nixos-testbed"
      ];
      directories = {
        "Music" = {
          path = "${config.nixarr.mediaDir}/library/music";
          ignorePerms = true;
          devices = [
            "sydpc"
          ];
        };
        "org" = {
          path = "/var/lib/webdav/~msyds/org";
          ignorePerms = true;
          devices = [
            "sydpc"
          ];
        };
      };
    };

    # HACK: I don't understand the idioms of Unix file permissions.
    # TODO: Consult molly for this shit TwT.
    users.users.${config.services.syncthing.user}.extraGroups = [
      "jellyfin"
      "nginx"
      "media"
      "webdav"
    ];

    sydnix.deertopia.nginx.vhosts."syncthing".vhost = {
      forceSSL = true;
      enableACME = true;
      locations."/" = {
        # TODO: This should work if guiAddress already specifies a protocol.
        proxyPass = "http://" + config.services.syncthing.guiAddress;
      };
    };
  };
}
