# Syncthing and Radicale configuration
{ config, pkgs, lib, ... }:
{

  environment.systemPackages = with pkgs; [
    syncthing
    radicale
  ];

  # Enable syncthingthing
  services.syncthing = {
    enable = true;
    openDefaultPorts = true;
    guiAddress = "0.0.0.0:8384";
    dataDir = "/vault/syncthing";
    declarative = {
      devices = {
        monolith = { id = "QGDGEZQ-INE7XDY-DNX2QI4-QI7ANQJ-57REEO2-FUMH545-FZS5RYU-ULF7HA2"; };
        roamer = { id = "DS3PJH3-J6SNMHM-XUJTDLO-DHGJL5U-J3RUMAG-4OSJWIK-VSJSDVJ-PIHZ2QP"; };
      };
      folders = {
       "Documents" = { devices = [ "monolith" "roamer" ]; id = "wusdj-bfjkr"; };
       "Notes" = { devices = [ "monolith" "roamer" ]; id = "2aqt7-vpprc"; };
       "Music" = { devices = [ "monolith" "roamer" ]; id = "kafhz-bfmzm"; };
       "Photos" = { devices = [ "monolith" "roamer" ]; id = "mjibc-ustcg"; };
       "Security" = { devices = [ "monolith" "roamer" ]; id = "z4lpn-pmm3v"; };
       "Projects" = { devices = [ "monolith" ]; id = "cjhmu-avy9v"; };

        Documents.type = "receiveonly";
        Notes.type = "receiveonly";
        Music.type = "receiveonly";
        Photos.type = "receiveonly";
        Security.type = "receiveonly";
        Projects.type = "receiveonly";

        Documents.path = "/vault/syncthing/Documents";
        Notes.path = "/vault/syncthing/Notes";
        Music.path = "/vault/syncthing/Music";
        Photos.path = "/vault/syncthing/Photos";
        Security.path = "/vault/syncthing/Security";
        Projects.path = "/vault/syncthing/Projects";
      };
    };
  };

  # Enable Radicale
  services.radicale = {
    enable = true;
    config = ''
      [server]
      hosts = 127.0.0.1:5232
      max_connections = 20
      max_content_length = 100000000
      timeout = 30

      [auth]
      type = htpasswd
      htpasswd_filename = /var/lib/radicale/users
      htpasswd_encryption = plain
      delay = 1
    '';
  };

}