{ lib, config, ... }:
let
  deviceList = builtins.attrNames config.common.syncthing.devices;
in
{

  options = {
    common.syncthing = {

      enable = lib.mkEnableOption "enable syncthing";

      devices = lib.mkOption {
        type = lib.types.attrsOf (lib.types.submodule {

          options.id = lib.mkOption { type = lib.types.str; };
          options.autoAcceptFolders = lib.mkOption {
            type = lib.types.bool;
            default = true;
          };

        });
      };

      basePath = lib.mkOption {
        type = lib.types.str;
        default = "/mnt/data";
      };

    };
  };

  config.home-manager.users.willifan.services.syncthing = lib.mkIf config.common.syncthing.enable {
    enable = lib.mkDefault true;
    overrideDevices = lib.mkDefault true;
    overrideFolders = lib.mkDefault true;
    guiAddress = "0.0.0.0:8384";
    passwordFile = config.sops.secrets."syncthing/password".path;
    settings = {
      gui.user = "willifan";
      devices = config.common.syncthing.devices;
      folders = lib.mkDefault {
        "Documents" = {
          id = "jtl6g-qjmwo";
          path = config.common.syncthing.basePath + "/Documents";
          devices = deviceList;
        };
        "Enpass" = lib.mkDefault {
          id = "ciksm-xsw4m";
          path = config.common.syncthing.basePath + "/Enpass";
          devices = deviceList;
        };
        "Pictures" = lib.mkDefault {
          id = "po4qj-q9t0t";
          path = config.common.syncthing.basePath + "/Pictures";
          devices = deviceList;
        };
        "Videos" = lib.mkDefault {
          id = "4wqf5-xasng";
          path = config.common.syncthing.basePath + "/Videos";
          devices = deviceList;
        };
        "Notes" = lib.mkDefault {
          id = "oc61n-iewgj";
          path = config.common.syncthing.basePath + "/Notes";
          devices = deviceList;
        };
        "Music" = lib.mkDefault {
          id = "xxh8a-3y2tq";
          path = config.common.syncthing.basePath + "/Music";
          devices = deviceList;
        };
      };
    };
  };
}