{ config, pkgs, ... }: {
  services.logind.settings.Login = {
    IdleAction = "suspend";
    IdleActionSec = "5min";
    HandleLidSwitch = "suspend";
    HandleLidSwitchDocked = "ignore";
  };

  systemd.services.ssh-suspend-inhibitor = {
    description = "Prevent suspend when SSH sessions are active";
    wantedBy = [ "multi-user.target" ];
    
    serviceConfig = {
      Type = "simple";
      Restart = "always";
      ExecStart = pkgs.writeShellScript "ssh-inhibitor" ''
        while true; do
          # Vérifie s'il y a des sessions SSH actives
          if ${pkgs.procps}/bin/pgrep -x sshd >/dev/null && \
             [ $(${pkgs.procps}/bin/pgrep -P $(${pkgs.procps}/bin/pgrep -x sshd) | wc -l) -gt 0 ]; then
            # Il y a des sessions SSH, on crée un inhibitor s'il n'existe pas
            if [ ! -f /tmp/ssh-inhibitor.lock ]; then
              ${pkgs.systemd}/bin/systemd-inhibit --what=idle:sleep \
                --who="SSH Session" \
                --why="SSH session active" \
                --mode=block \
                sleep infinity &
              echo $! > /tmp/ssh-inhibitor.lock
            fi
          else
            # Pas de sessions SSH, on retire l'inhibitor
            if [ -f /tmp/ssh-inhibitor.lock ]; then
              kill $(cat /tmp/ssh-inhibitor.lock) 2>/dev/null || true
              rm /tmp/ssh-inhibitor.lock
            fi
          fi
          sleep 10
        done
      '';
    };
  };
}
