# Graphical desktop layer: GUI apps, Wayland session env, and cursor theme.
# Imported only on hosts that run Sway (MBP, T400, Mac Pro); never pulled onto
# the headless WSL host. Login (and the Sway session launch) is handled by the
# greetd/ReGreet greeter -- see ../modules/sway.nix -- so there is no tty1
# autostart.
{
  pkgs,
  config,
  inputs,
  identity,
  ...
}:
{
  imports = [
    ./sway.nix
  ];

  home.packages = [
    pkgs.element-desktop
    pkgs.legcord
    pkgs.nemo # file manager (launched via Mod+e, see ./sway.nix)
    #pkgs.plex-desktop
    #pkgs.plexamp
  ];

  home.sessionVariables = {
    MOZ_USE_XINPUT2 = "1";
    XDG_CURRENT_DESKTOP = "sway";
  };

  # Default apps for the desktop (writes ~/.config/mimeapps.list). Firefox owns
  # the web; nemo owns directories/file URIs; images, PDFs and plain text open
  # in Firefox too -- no dedicated GUI viewer/editor is installed and vim is
  # terminal-only (no usable GUI .desktop for double-click handoff). Kept
  # minimal -- only the handlers actually present on these hosts.
  xdg.mimeApps = {
    enable = true;
    defaultApplications = {
      "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";
      "inode/directory" = "nemo.desktop";
      "image/png" = "firefox.desktop";
      "image/jpeg" = "firefox.desktop";
      "image/gif" = "firefox.desktop";
      "image/webp" = "firefox.desktop";
      "image/svg+xml" = "firefox.desktop";
      "application/pdf" = "firefox.desktop";
      "text/plain" = "firefox.desktop";
    };
  };

  # Theme GTK apps (nemo, etc.) to match the Catppuccin Mocha desktop. Under
  # Sway there is no XSettings daemon, so GTK reads these from the generated
  # ~/.config/gtk-{3,4}.0/settings.ini directly. The Mocha theme is dark by
  # design, so no separate prefer-dark hint is needed.
  gtk = {
    enable = true;
    # Theme GTK4 apps too (for any added later). GTK4 ignores gtk-theme-name,
    # but home-manager turns this into an `@import` of the theme's
    # gtk-4.0/gtk.css into ~/.config/gtk-4.0/gtk.css -- which even libadwaita
    # honours, since that file overrides the named colours it uses
    # (window_bg_color, accent_bg_color, ...). Set explicitly (same value as the
    # legacy default) so it also silences the stateVersion<26.05 warning.
    gtk4.theme = config.gtk.theme;
    theme = {
      name = "catppuccin-mocha-blue-standard";
      package = pkgs.catppuccin-gtk.override {
        accents = [ "blue" ];
        variant = "mocha";
      };
    };
    iconTheme = {
      name = "Adwaita";
      package = pkgs.adwaita-icon-theme;
    };
  };

  home.pointerCursor = {
    gtk.enable = true;
    x11 = {
      enable = true;
      defaultCursor = "Adwaita";
    };
    package = pkgs.adwaita-icon-theme;
    name = "Adwaita";
    size = 24;
  };

  # Firefox is themed at the browser level (it does not follow the GTK theme).
  # The system installs the binary (programs.firefox in ../modules/users.nix); here
  # home-manager owns only the profile, hence package = null. Apply the
  # Catppuccin Mocha theme add-on (only the mauve accent is packaged upstream;
  # the rest of the desktop uses blue) and make content + UI dark.
  programs.firefox = {
    enable = true;
    package = null;
    # Keep the legacy profile location (~/.mozilla/firefox) -- that is where the
    # system Firefox actually looks; pin it explicitly to silence the
    # stateVersion<26.05 default-change warning (the new XDG path depends on
    # Firefox's own profile support).
    configPath = ".mozilla/firefox";
    profiles.${identity.username} = {
      id = 0;
      isDefault = true;
      extensions = {
        force = true;
        packages = [
          inputs.firefox-addons.packages.${pkgs.stdenv.hostPlatform.system}.catppuccin-mocha-mauve
        ];
      };
      settings = {
        # Enable bundled add-ons automatically so the theme applies on first run.
        "extensions.autoDisableScopes" = 0;
        # Dark chrome + dark page content.
        "ui.systemUsesDarkTheme" = 1;
        "layout.css.prefers-color-scheme.content-override" = 0;
      };
    };
  };
}
