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

with lib;
let
  cfg = config.my.profiles.pimalaya;
in

{
  options.my.profiles.pimalaya.enable = mkEnableOption "Pimalaya email stuff";

  config = mkIf cfg.enable {
    my = {
      services.mirador = {
        enable = true;
        package = inputs.mirador.packages.${pkgs.system}.default.overrideAttrs (old: {
          nativeBuildInputs = old.nativeBuildInputs ++ [ pkgs.installShellFiles ];
          postInstall = old.postInstall + ''
            installShellCompletion --cmd mirador \
              --bash <($out/bin/mirador completion bash) \
              --fish <($out/bin/mirador completion fish) \
              --zsh <($out/bin/mirador completion zsh)
          '';
        });
        settings.accounts.personal = {
          default = true;
          folder = "INBOX";
          backend.type = "imap";
          backend.host = "mail.your-server.de";
          backend.port = 993;
          backend.encryption = "tls";
          backend.login = "mail@moritzboeh.me";
          backend.auth.type = "password";
          backend.auth.command = "cat /run/agenix/email";
          on-message-added.cmd = "systemctl start --user neverest.service";
          on-message-added.notify.summary = "📫 New message from {sender}";
          on-message-added.notify.body = "{subject}";
        };
      };
      services.neverest = {
        enable = true;
        package = inputs.neverest.packages.${pkgs.system}.default.overrideAttrs (old: {
          nativeBuildInputs = old.nativeBuildInputs ++ [ pkgs.installShellFiles ];
          postInstall = old.postInstall + ''
            installShellCompletion --cmd neverest \
              --bash <($out/bin/neverest completion bash) \
              --fish <($out/bin/neverest completion fish) \
              --zsh <($out/bin/neverest completion zsh)
          '';
        });
        settings.accounts.personal = {
          default = true;
          left.backend.type = "imap";
          left.backend.host = "mail.your-server.de";
          left.backend.port = 993;
          left.backend.encryption = "tls";
          left.backend.login = "mail@moritzboeh.me";
          left.backend.auth.type = "password";
          left.backend.auth.command = "cat /run/agenix/email";
          right.backend.type = "maildir";
          right.backend.root-dir = "/home/moritz/Documents/Mail/personal";
          right.backend.maildirpp = false;
          right.folder.aliases.inbox = "INBOX";
          right.folder.aliases.sent = "Sent";
          right.folder.aliases.drafts = "Drafts";
          right.folder.aliases.trash = "Trash";
        };
      };
      programs.himalaya = {
        enable = true;
        package = inputs.himalaya.packages.${pkgs.system}.default.overrideAttrs (old: {
          nativeBuildInputs = old.nativeBuildInputs ++ [ pkgs.installShellFiles ];
          postInstall = old.postInstall + ''
            installShellCompletion --cmd himalaya \
              --bash <($out/bin/himalaya completion bash) \
              --fish <($out/bin/himalaya completion fish) \
              --zsh <($out/bin/himalaya completion zsh)
          '';
        });
        settings.accounts.personal = {
          default = true;
          email = "mail@moritzboeh.me";
          display-name = "Moritz Böhme";
          downloads-dir = "/home/moritz/Downloads";
          backend.type = "maildir";
          backend.root-dir = "/home/moritz/Documents/Mail/personal";
          backend.maildirpp = false;
          folder.aliases.inbox = "INBOX";
          folder.aliases.sent = "Sent";
          folder.aliases.drafts = "Drafts";
          folder.aliases.trash = "Trash";
          message.send.backend.type = "smtp";
          message.send.backend.host = "mail.your-server.de";
          message.send.backend.port = 465;
          message.send.backend.login = "mail@moritzboeh.me";
          message.send.backend.encryption.type = "tls";
          message.send.backend.auth.type = "password";
          message.send.backend.auth.command = "cat /run/agenix/email";
        };
      };
    };
  };
}
