{ inputs, config, pkgs, ... }:
let
  scripts = {
    disk_check = {
      when = "*-*-* *:00:00";
      script = toString (pkgs.writeShellScript "script" ''
        REPORT_EMAIL=email@thomasave.be
        ZPOOL_STATUS=$(zpool status -x)
        if [ "$ZPOOL_STATUS" = "all pools are healthy" ] || [ "$ZPOOL_STATUS" = "no pools available" ]
        then
            printf 0 > /var/db/zpool.status
        else
            if [ "$(cat /var/db/zpool.status)" -eq 0 ]
            then
                zpool status | mail -s "ZPOOL NOT HEALTHY" $REPORT_EMAIL
                printf 1 > /var/db/zpool.status
            fi
        fi
      '');
    };
    restic_etienne = {
      when = "*-*-* 01:00:00";
      script = toString (pkgs.writeShellScript "script" ''
        /Backup/Restic/Etienne/run.sh
        ${pkgs.curl}/bin/curl https://uptime.thomasave.be/api/push/zGPrOi0NsJ
      '');
    };
    restic_magda_and_etienne = {
      when = "*-*-* 03:00:00";
      script = toString (pkgs.writeShellScript "script" ''
        /Backup/Restic/Magda\ and\ Etienne/run.sh
        ${pkgs.curl}/bin/curl https://uptime.thomasave.be/api/push/yvBuDgduDd
      '');
    };
    restic_backblaze = {
      when = "*-*-* 02:00:00";
      script = toString (pkgs.writeShellScript "script" ''
        /Backup/Restic/BackBlaze/run.sh
        ${pkgs.curl}/bin/curl https://uptime.thomasave.be/api/push/LVruVYTwTn
      '');
    };
    mail_sync = {
      when = "*-*-* 04:00:00";
      script = toString (pkgs.writeShellScript "script" ''
        cd /home/server/Containers/Mails && docker compose up sync-etienne sync-magda
        ${pkgs.curl}/bin/curl https://uptime.thomasave.be/api/push/eEIPVGyrAx
      '');
    };
  };
in
{
  home.username = "server";
  home.homeDirectory = "/home/server";
  nix = {
    package = pkgs.nix;
    settings.use-xdg-base-directories = true;
  };

  xdg.enable = true;
  imports = [
    (import ./utils/common.nix { inherit inputs config pkgs; })
    (import ./utils/services.nix { inherit pkgs; scripts = scripts; })
    ./ssh
  ];

  programs.ssh.matchBlocks."*".identityFile = "/home/server/.secrets/SSH/Vault/id_ed25519";

  home.sessionVariables = {
    NIX_PATH = "${config.xdg.stateHome}/nix/profiles/channels/";
    LANG = "en_US.UTF-8";
    XDG_RUNTIME_DIR = "/run/user/$(id -u)";
  };

  programs.zsh.initExtra = pkgs.lib.mkForce ''
      LOCAL_SEARCH_DIRS=(~/.dotfiles/ ~/Storage/Thomas/ ~/Storage/Etienne/ ~/Storage/Magda\ and\ Etienne/ ~/Containers/)
      REMOTE_SEARCH_DIRS=()
  '';
}
