{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.home.roles.desktop;
in
{
  options.home.roles.desktop = {
    enable = mkEnableOption "Enable desktop GUI applications and utilities";
  };

  config = mkIf cfg.enable {
    home.packages = with pkgs; [
      # Desktop applications
      bitwarden-desktop
      dunst
      keepassxc
      unstable.ghostty
      
      # Desktop utilities
      feh # Image viewer and wallpaper setter for X11
      rofi # Application launcher for X11
      solaar # Logitech management software
      waybar
      wofi # Application launcher for Wayland
      xdg-utils # XDG utilities for opening files/URLs with default applications
      
      # System utilities with GUI components
      (snapcast.override { pulseaudioSupport = true; })
      
      # KDE tiling window management
      kdePackages.krohnkite  # Dynamic tiling extension for KWin 6
      
      # KDE PIM applications for email, calendar, and contacts
      kdePackages.kmail
      kdePackages.kmail-account-wizard
      kdePackages.kmailtransport
      kdePackages.korganizer
      kdePackages.kaddressbook
      kdePackages.kontact
      
      # KDE System components needed for proper integration
      kdePackages.kded
      kdePackages.systemsettings
      kdePackages.kmenuedit
      
      # Desktop menu support
      kdePackages.plasma-desktop # Contains applications.menu
      
      # KDE Online Accounts support
      kdePackages.kaccounts-integration
      kdePackages.kaccounts-providers
      kdePackages.signond
      
      # KDE Mapping
      kdePackages.marble  # Virtual globe and world atlas
      
      # KDE Productivity
      kdePackages.kate        # Advanced text editor with syntax highlighting
      kdePackages.okular      # Universal document viewer (PDF, ePub, etc.)
      kdePackages.spectacle   # Screenshot capture utility
      kdePackages.filelight   # Visual disk usage analyzer
      
      # KDE Multimedia
      kdePackages.gwenview    # Image viewer and basic editor
      kdePackages.elisa       # Music player
      
      # KDE System Utilities  
      kdePackages.ark         # Archive manager (zip, tar, 7z, etc.)
      kdePackages.yakuake     # Drop-down terminal emulator
    ];

    programs.firefox = {
      enable = true;
    };

    programs.spotify-player.enable = true;

    services.gnome-keyring = {
      enable = true;
    };

    # rbw vault unlock on login and resume from suspend
    systemd.user.services.rbw-unlock-on-login = {
      Unit = {
        Description = "Unlock rbw vault at login";
        After = [ "graphical-session.target" ];
      };
      Service = {
        Type = "oneshot";
        ExecStart = "${pkgs.rbw}/bin/rbw unlock";
        Environment = "RBW_AGENT=${pkgs.rbw}/bin/rbw-agent";
        # KillMode = "process" prevents systemd from killing the rbw-agent daemon
        # when this oneshot service completes. The agent is spawned by rbw unlock
        # and needs to persist after the service exits.
        KillMode = "process";
      };
      Install = {
        WantedBy = [ "graphical-session.target" ];
      };
    };

    systemd.user.services.rbw-unlock-on-resume = {
      Unit = {
        Description = "Unlock rbw vault after resume from suspend";
        After = [ "suspend.target" ];
      };
      Service = {
        Type = "oneshot";
        ExecStart = "${pkgs.rbw}/bin/rbw unlock";
        Environment = "RBW_AGENT=${pkgs.rbw}/bin/rbw-agent";
        # KillMode = "process" prevents systemd from killing the rbw-agent daemon
        # when this oneshot service completes. The agent is spawned by rbw unlock
        # and needs to persist after the service exits.
        KillMode = "process";
      };
      Install = {
        WantedBy = [ "suspend.target" ];
      };
    };

    # KDE environment variables for proper integration
    home.sessionVariables = {
      QT_QPA_PLATFORMTHEME = "kde";
      KDE_SESSION_VERSION = "6";
    };

    xdg = {
      enable = true;
      
      # Ensure desktop files are made available for discovery
      desktopEntries = {};  # This creates the desktop files directory structure
      
      mimeApps = {
        enable = true;
        associations.added = {
          # Ensure associations are properly registered
          "text/html" = "firefox.desktop";
          "x-scheme-handler/http" = "firefox.desktop";
          "x-scheme-handler/https" = "firefox.desktop";
        };
        defaultApplications = {
          # Web browsers
          "text/html" = "firefox.desktop";
          "x-scheme-handler/http" = "firefox.desktop";
          "x-scheme-handler/https" = "firefox.desktop";
          "x-scheme-handler/about" = "firefox.desktop";
          "x-scheme-handler/unknown" = "firefox.desktop";
          
          # Documents
          "application/pdf" = "okular.desktop";
          "text/plain" = "kate.desktop";
          "text/x-tex" = "kate.desktop";
          "text/x-c" = "kate.desktop";
          "text/x-python" = "kate.desktop";
          "application/x-shellscript" = "kate.desktop";
          
          # Images
          "image/png" = "gwenview.desktop";
          "image/jpeg" = "gwenview.desktop";
          "image/jpg" = "gwenview.desktop";
          "image/gif" = "gwenview.desktop";
          "image/bmp" = "gwenview.desktop";
          "image/tiff" = "gwenview.desktop";
          "image/webp" = "gwenview.desktop";
          
          # Archives
          "application/zip" = "ark.desktop";
          "application/x-tar" = "ark.desktop";
          "application/x-compressed-tar" = "ark.desktop";
          "application/x-7z-compressed" = "ark.desktop";
          "application/x-rar" = "ark.desktop";
          
          # Audio
          "audio/mpeg" = "elisa.desktop";
          "audio/mp4" = "elisa.desktop";
          "audio/flac" = "elisa.desktop";
          "audio/ogg" = "elisa.desktop";
          "audio/wav" = "elisa.desktop";
          
          # Email
          "message/rfc822" = "kmail.desktop";
          "x-scheme-handler/mailto" = "kmail.desktop";
          
          # Calendar
          "text/calendar" = "korganizer.desktop";
          "application/x-vnd.akonadi.calendar.event" = "korganizer.desktop";
        };
      };
    };

    # Fix for KDE applications.menu file issue on Plasma 6
    # KDE still looks for applications.menu but Plasma 6 renamed it to plasma-applications.menu
    xdg.configFile."menus/applications.menu".source = "${pkgs.kdePackages.plasma-workspace}/etc/xdg/menus/plasma-applications.menu";

    # Note: modules must be imported at top-level home config
  };
}
