{ config, pkgs, lib, hyprlandSettings, ... }:
let
  settings = import ./settings.nix { inherit pkgs; };

  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"
  '';
in
{
  imports = [
    ./environment.nix
  ];

  environment.systemPackages = [ bing-wallpaper pkgs.jq ];

  home-manager.users."${settings.user}" = (import ./home.nix {inherit lib pkgs hyprlandSettings;});

  services = {
    displayManager = {
      sddm = {
        enable = true;
        package = pkgs.kdePackages.sddm;
        extraPackages = [ settings.sddm.package ];
        theme = settings.sddm.themeName;
        wayland.enable = true;
        settings = {
          Theme = {
            ThemeDir = "${settings.sddm.package}/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 = true;

    blueman.enable = true;
  };

  programs = {
    hyprland = {
      enable = true;
      xwayland.enable = true;
      portalPackage = 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
          '';
          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";
          };
        };
      };
    };
    extraConfig = ''
        DefaultTimeoutStopSec=10s
    '';
  };

  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 = [
      pkgs.xdg-desktop-portal-hyprland
      pkgs.xdg-desktop-portal-gnome
      pkgs.xdg-desktop-portal-gtk
    ];
  };

  nixpkgs.overlays = [
    (self: super: {
      waybar = super.waybar.overrideAttrs (oldAttrs: {
      mesonFlags = oldAttrs.mesonFlags ++ [ "-Dexperimental=true" ];
      });
    })
  ];
}
