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

  bing-wallpaper = pkgs.writeScriptBin "bing-wallpaper" ''
    # Directory to store wallpapers
    IMG_PATH="/run/wallpaper.jpg"

    # Download if not already downloaded
    URL=$(curl -s "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1" | \
      jq -r '.images[0].url')
    FULL_URL="https://www.bing.com$URL"
    curl -s -o "$IMG_PATH" "$FULL_URL"
    echo "Downloaded $FULL_URL to $IMG_PATH successfully"
  '';

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

  config = lib.mkIf cfg.enable {
    environment.systemPackages = [
      bing-wallpaper
      pkgs.jq
    ];

    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";
      };

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

      dbus.enable = true;

      ddccontrol.enable = false;

      blueman.enable = true;
    };

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

      nm-applet.enable = true;
    };

    systemd = {
      services = {
        preload-bing-wallpaper = {
          enable = true;
          wants = [ "network-online.target" ];
          after = [ "network-online.target" ];
          before = [ "display-manager.service" ];
          requiredBy = [
            "plymouth-quit-wait.service"
            "display-manager.service"
          ];
          wantedBy = [ "display-manager.service" ];
          path = [
            pkgs.bash
            pkgs.jq
            pkgs.curl
            bing-wallpaper
          ];
          script = ''
            bing-wallpaper
          '';
          serviceConfig = {
            Type = "oneshot";
          };
        };
      };
      user = {
        services = {
          reload-bing-wallpaper = {
            enable = true;
            path = [
              pkgs.bash
              pkgs.jq
              pkgs.curl
              pkgs.hyprland
              bing-wallpaper
            ];
            script = ''
              bing-wallpaper
              ${pkgs.hyprland}/bin/hyprctl hyprpaper reload ,/run/wallpaper.jpg
            '';
            serviceConfig = {
              Type = "oneshot";
            };
          };
        };
        # Create a timer to run the service periodically
        timers = {
          reload-bing-wallpaper = {
            description = "Timer for reload-bing-wallpaper";
            wantedBy = [ "timers.target" ];

            # Timer configuration
            timerConfig = {
              OnCalendar = "daily"; # Check every day
              Persistent = true; # Run immediately if last run was missed
              Unit = "reload-bing-wallpaper.service";
            };
          };
        };
      };
    };

    security = {
      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
      ];
    };
  };
}
