{ lib, config, ... }:
with lib;
let
  cfg = config.modules.accounts.dav;
  vdirsyncerConf = {
        enable = true;
        auth = "basic";
        userNameCommand = [
          "cat"
          "${config.sops.secrets."webdav/username".path}"
        ];
  };
in
{
  options.modules.accounts.dav = {
    enable = mkEnableOption "enable personal Caldav / CardDav";
  };
  config = mkIf cfg.enable {
    
    ## Manage secrets used in this module
    sops = {
      secrets = {
        "webdav/url/caldav" = {
          sopsFile = ../../../../secrets/accounts.yaml;
        };
        "webdav/url/carddav" = {
          sopsFile = ../../../../secrets/accounts.yaml;
        };
        "webdav/username" = {
          sopsFile = ../../../../secrets/accounts.yaml;
        };
        "webdav/password" = {
          sopsFile = ../../../../secrets/accounts.yaml;
        };
      };
    };

    accounts.calendar.basePath = ".local/share/calendars";
    accounts.calendar.accounts.personal_calendars = {
      name = "personal_calendar";
      remote.type = "caldav"; 
      remote.passwordCommand = [
        "cat"
        "${config.sops.secrets."webdav/password".path}"
      ];
      vdirsyncer = vdirsyncerConf // {
        metadata = [
          "color"
          "displayname"
        ];
        itemTypes = [
          "VTODO"
          "VEVENT"
        ];
        urlCommand = [
          "cat"
          "${config.sops.secrets."webdav/url/caldav".path}"
        ];
        collections = [
          "from a"
          "from b"
        ];
      };
      khal = {
        enable = true;
        type = "discover";
        color = "auto";
      };
    };

    accounts.contact.basePath = ".local/share/contacts";
    accounts.contact.accounts.personal_contacts = {
      remote.type = "carddav"; 
      remote.passwordCommand = [
        "cat"
        "${config.sops.secrets."webdav/password".path}"
      ];
      local.type = "filesystem";
      local.fileExt = ".vcf";
      vdirsyncer = vdirsyncerConf // {

        metadata = [
          "displayname"
        ];
        urlCommand = [
          "cat" 
          "${config.sops.secrets."webdav/url/carddav".path}"
        ];
      };
      khal = {
        enable = true;
        color = "#26A269";
      };
      khard = {
        enable = true;
      };
    };
    programs.vdirsyncer.enable = true;
    services.vdirsyncer = {
      enable = true;
      frequency = "*:0/15";
    };
    programs.khal = {
      enable = true;
      locale = {
        dateformat = "%Y.%m.%d";
        datetimeformat = "%Y.%m.%d %H:%M";
        longdateformat = "%d %B %Y";
        longdatetimeformat = "%d %B %Y %H:%M";
        timeformat = "%H:%M";
      };
      settings = {
        default = {
          highlight_event_days = true;
        };
        view = {
          frame = "color";
          blank_line_before_day = true;
        };
      };
    };
    programs.khard = {
      enable = true;
      settings = {
        general = {
          default_action = "list";
          editor = ["nvim" "-i" "NONE"];
          merge_editor = [ "nvim" "-d" ];
        };
      };
    };
  };
}
