{
  lib,
  pkgs,
  config,
  ...
}: let
  cfg = config.roles.email;
  terminal = config.programs.terminal;
  termcmd = "${terminal}/bin/${terminal.pname}";
in {
  options.roles = with lib; {
    email = {
      enable = mkEnableOption "email settings";
      email = mkOption {
        description = "email address";
      };
      terminal = mkOption {
        type = types.bool;
        default = true;
      };
    };
  };

  config = lib.mkIf cfg.enable {
    programs.scripts = [
      {
        name = "email";
        text = ''
          ${termcmd} -e aerc
        '';
        install = false;
        hotkeys = [{key = "E";}];
      }
    ];

    programs.git = {
      userName = config.home.userFullname;
      userEmail = cfg.email;
    };

    accounts.email.accounts.${cfg.email} = lib.mkIf cfg.terminal {
      notmuch.enable = true;
      neomutt.enable = true;
      aerc.enable = true;
      mbsync.enable = true;
      mbsync.create = "both";
      userName = cfg.email;
      realName = config.home.userFullname;
      address = cfg.email;
      primary = true;
      passwordCommand = "${pkgs.rbw}/bin/rbw get privateemail";
      smtp = {
        host = "mail.privateemail.com";
        port = 465;
        tls.enable = true;
      };
      imap = {
        host = "mail.privateemail.com";
        port = 993;
        tls.enable = true;
      };
    };

    programs.notmuch.enable = cfg.terminal;
    programs.neomutt = {
      enable = cfg.terminal;
      vimKeys = true;
      sort = "reverse-date";
      sidebar.enable = true;
    };
    programs.mbsync.enable = cfg.terminal;
    services.mbsync.enable = cfg.terminal;

    programs.aerc = {
      enable = cfg.terminal;
      extraBinds = ''
        # Binds are of the form <key sequence> = <command to run>
        # To use '=' in a key sequence, substitute it with "Eq": "<Ctrl+Eq>"
        # If you wish to bind #, you can wrap the key sequence in quotes: "#" = quit
        <C-p> = :prev-tab<Enter>
        <C-n> = :next-tab<Enter>
        <C-t> = :term<Enter>

        [messages]
        Q = :quit<Enter>

        T = :toggle-threads<Enter>

        j = :next<Enter>
        <Down> = :next<Enter>
        <C-d> = :next 50%<Enter>
        <C-f> = :next 100%<Enter>
        <PgDn> = :next -s 100%<Enter>

        k = :prev<Enter>
        <Up> = :prev<Enter>
        <C-u> = :prev 50%<Enter>
        <C-b> = :prev<Enter>
        <PgUp> = :prev -s 100%<Enter>
        gg = :select 0<Enter>
        G = :select -1<Enter>

        J = :next-folder<Enter>
        K = :prev-folder<Enter>

        v = :mark -t<Enter>
        V = :mark -v<Enter>

        f = :flag<Enter>
        F = :unflag<Enter>

        t = :toggle-threads<Enter>

        <Enter> = :view<Enter>
        D = :mv Trash<Enter>

        C = :compose<enter>

        Rr = :reply<Enter>
        Rq = :reply -q<Enter>
        rr = :reply -a<Enter>
        rq = :reply -aq<Enter>
        rt = :unflag<Enter>:reply -a -Tthanks<Enter>
        Rt = :unflag<Enter>:reply -qa -Tquoted_thanks<Enter>

        a = :mv Archive<Enter>

        c = :cf<space>
        $ = :term<space>
        ! = :term<space>
        | = :pipe<space>

        ga = :flag<Enter>:pipe -mb git am -3<Enter>
        gp = :term git push<Enter>
        gl = :term git log<Enter>

        / = :search<space>
        \ = :filter<space>
        n = :next-result<Enter>
        N = :prev-result<Enter>

        o = :term mbsync -a<Enter>
        q = :quit<Enter>

        [view]
        / = :toggle-key-passthrough<Enter>/
        q = :close<Enter>
        | = :pipe -m<space>
        S = :save<space>
        H = :toggle-headers<Enter>
        D = :mv Trash<Enter>
        <C-k> = :prev-part<Enter>
        <C-j> = :next-part<Enter>
        J = :next<Enter>
        K = :prev<Enter>

        f = :forward<Enter>
        Rr = :reply<Enter>
        Rq = :reply -q<Enter>
        rr = :reply -a<Enter>
        rq = :reply -aq<Enter>
        rt = :reply -Tthanks<Enter>

        ga = :pipe -b git am -3<Enter>
        gp = :term git push<Enter>
        gl = :term git log<Enter>

        [view::passthrough]
        $noinherit = true
        $ex = <C-x>
        <Esc> = :toggle-key-passthrough<Enter>

        [compose]
        $ex = <C-x>
        <C-k> = :prev-field<Enter>
        <C-j> = :next-field<Enter>
        <tab> = :next-field<Enter>

        [compose::editor]
        # Keybindings used when the embedded terminal is selected in the compose view
        $noinherit = true
        $ex = <C-x>
        <C-t> = :term<Enter>
        <C-k> = :prev-field<Enter>
        <C-j> = :next-field<Enter>
        <C-p> = :prev-tab<Enter>
        <C-n> = :next-tab<Enter>

        [compose::review]
        # Keybindings used when reviewing a message to be sent
        y = :send<Enter>
        n = :abort<Enter>
        e = :edit<Enter>
        a = :attach<space>
        c = :encrypt<Enter>
        s = :sign<Enter>
        V = :header -f X-Sourcehut-Patchset-Update NEEDS_REVISION<Enter>
        A = :header -f X-Sourcehut-Patchset-Update APPLIED<Enter>
        R = :header -f X-Sourcehut-Patchset-Update REJECTED<Enter>

        [terminal]
        $noinherit = true
        $ex = <C-x>

        <C-t> = :term<Enter>
        <C-p> = :prev-tab<Enter>
        <C-n> = :next-tab<Enter>
      '';

      extraConfig = {
        general = {
          pgp-provider = "gpg";
          unsafe-accounts-conf = true;
        };
        ui = {};
        viewer = {};
        filters = {
          "text/plain" = "colorize";
          "text/calendar" = "calendar";
          "message/delivery-status" = "colorize";
          "message/rfc822" = "colorize";
          "image/*" = "img2sixel";
          "text/html" = "html | colorize";
        };
      };
    };
  };
}
