{
  config,
  pkgs,
  lib,
  namespace,
  ...
}:
with lib;
let
  name = "sunshine";
  cfg = config.${namespace}.services.${name};

  module = lib.${namespace}.mkModule {
    inherit config name;
    description = "Sunshine, a self-hosted game stream host for Moonlight";
    options = { };
    moduleConfig = {
      services.sunshine = {
        enable = true;
        openFirewall = cfg.openFirewall;
        autoStart = true;
        capSysAdmin = true;
        applications.apps = with pkgs; [
          {
            name = "steam";
            prep-cmd = [
              # {
              #   do = "${pkgs.cosmic-randr}/bin/cosmic-randr mode HDMI-A-3 1280 800";
              #   undo = "${pkgs.cosmic-randr}/bin/cosmic-randr mode HDMI-A-3 1920 1080";
              # }
              # {
              #   # do = ''"${pkgs.util-linux}/bin/setsid ${pkgs.steam}/bin/steam steam://open/bigpicture"'';
              #   do = "${pkgs.steam-run}/bin/steam-run ${pkgs.steam}/bin/steam";
              #   undo = ''"${pkgs.util-linux}/bin/setsid ${pkgs.steam}/bin/steam steam://close/bigpicture"'';
              # }
            ];
            # detached = [
            #   "${pkgs.steam}/bin/steam"# steam://open/bigpicture"
            # ];
            exclude-global-prep-cmd = "false";
            auto-detach = "true";
            wait-all = true;
            exit-timeout = 5;
            image-path = "steam.png";
          }
          {
            name = "800p Desktop";
            prep-cmd = [
              {
                do = "${pkgs.cosmic-randr}/bin/cosmic-randr mode HDMI-A-3 1280 800";
                undo = "${pkgs.cosmic-randr}/bin/cosmic-randr mode HDMI-A-3 1920 1080";
              }
            ];
            exclude-global-prep-cmd = "false";
            auto-detach = "true";
          }
        ];
        settings = {
          port = 47989;
        };
      };
      # systemd.user.services.sunshine = {
      #   description = "Self-hosted game stream host for Moonlight";

      #   wantedBy = [ "graphical-session.target" ];
      #   partOf = [ "graphical-session.target" ];
      #   wants = [ "graphical-session.target" ];
      #   after = [ "graphical-session.target" ];

      #   startLimitIntervalSec = 500;
      #   startLimitBurst = 5;

      #   environment.PATH = lib.mkForce null; # don't use default PATH, needed for tray icon menu links to work

      #   serviceConfig = {
      #     # only add configFile if an application or a setting other than the default port is set to allow configuration from web UI
      #     ExecStart = lib.mkForce "${config.security.wrapperDir}/sunshine port:47989";
      #     Restart = "on-failure";
      #     RestartSec = "5s";
      #   };
      # };
    };
  };
in
{
  imports = [ module ];
}
