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

with lib;
let
  cfg = config.my.services.mirador;
  toml = pkgs.formats.toml { };
in
{
  options.my.services.mirador = {
    enable = mkEnableOption "mirador";
    package = mkPackageOption pkgs "mirador" { };
    settings = mkOption {
      inherit (toml) type;
      default = { };
      apply = toml.generate "config.toml";
    };
  };


  config = mkIf cfg.enable {
    home-manager.users.moritz = {
      xdg.configFile."mirador/config.toml".source = cfg.settings;
      home.packages = [ cfg.package ];
      systemd.user.services.mirador = {
        Unit = {
          Description = "CLI to watch mailbox changes";
        };
        Service = {
          # Lower CPU and I/O priority
          Nice = 19;
          IOSchedulingClass = "best-effort";
          IOSchedulingPriority = 7;
          IOWeight = 100;

          ExecStart = ''
            ${lib.getExe cfg.package} watch
          '';
        };
      };
    };
    age.secrets.email = {
      file = ../../secrets/email.age;
      owner = "1000";
    };
  };
}
