{
  config,
  pkgs,
  lib,
  namespace,
  ...
}:
let
  cfg = config.${namespace}.desktop.hyprland;

  # Select the appropriate wallpaper script based on the configuration

  sddmThemeName = "sddm-astronaut-theme";
  sddmThemePkg = pkgs.sddm-astronaut.override {
    embeddedTheme = "astronaut";
    themeConfig = {
      Background = "/run/wallpaper.jpg";
    };
  };
in
{
  imports = [ ./options.nix ];

  config = lib.mkIf cfg.enable {
    # Configure Plymouth to use the same wallpaper
    boot.plymouth = {
      enable = true;
      extraConfig = ''
        ShowDelay=0
        DeviceTimeout=5
        DeviceScale=1
      '';
    };

    services = {
      displayManager = {
        sddm = {
          enable = true;
          package = pkgs.kdePackages.sddm;
          extraPackages = [ sddmThemePkg ];
          theme = sddmThemeName;
          wayland.enable = true;
          settings = {
            Theme = {
              ThemeDir = "${sddmThemePkg}/share/sddm/themes";
            };
          };
        };
        # Disable gdm
        gdm.enable = lib.mkForce false;
        defaultSession = "hyprland-uwsm";
      };

      # disable other desktops
      desktopManager = {
        plasma6.enable = lib.mkForce false;
        gnome.enable = lib.mkForce false;
      };

      gnome.gnome-keyring.enable = true;

      dbus.enable = true;
      ddccontrol.enable = false;
      blueman.enable = true;
    };

    programs = {
      hyprland = {
        enable = true;
        xwayland.enable = true;
        withUWSM = true;
        portalPackage = lib.mkDefault pkgs.xdg-desktop-portal-hyprland;
      };

      nm-applet.enable = false;
    };

    security = {
      # Enable gnome keyring for password storage
      pam.services.sddm.enableGnomeKeyring = true;
      polkit.enable = true;

      # configure sudo
      sudo.extraRules = [
        {
          commands = [
            {
              command = "/run/current-system/sw/bin/waybar-weather";
              options = [ "NOPASSWD" ];
            }
            {
              command = "/run/current-system/sw/bin/waybar-updates";
              options = [ "NOPASSWD" ];
            }
          ];
          groups = [ "wheel" ];
        }
      ];
    };

    xdg.portal = {
      enable = true;
      wlr.enable = true;
      xdgOpenUsePortal = true;
      extraPortals = lib.mkDefault [
        pkgs.xdg-desktop-portal-hyprland
        pkgs.xdg-desktop-portal-gnome
        pkgs.xdg-desktop-portal-gtk
      ];
    };
  };
}
