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

let
  nomarchyLib = import ../lib { inherit lib; };
  userPackagesFile = "${config.home.homeDirectory}/.config/home-manager/user-packages.json";
  userPackages = if builtins.pathExists userPackagesFile then
    let
      pkgNames = builtins.fromJSON (builtins.readFile userPackagesFile);
      # Filter to only packages that exist in pkgs to prevent build failures
      validPkgs = builtins.filter (name: builtins.hasAttr name pkgs) pkgNames;
    in builtins.map (name: pkgs.${name}) validPkgs
  else [];
in
{
  imports = [
    inputs.nix-colors.homeManagerModules.default
    inputs.walker.homeManagerModules.default
    ../core/home
    ../themes/engine/stylix-compat.nix
    ../themes/engine/loader.nix
    ../themes/engine/files.nix
    ../themes/engine/scripts.nix
    ../themes/engine/stylix.nix
    ../themes/engine/switcher.nix
    ./apps/alacritty/default.nix
    ./apps/btop/default.nix
    ./apps/chromium/default.nix
    ./apps/elephant/default.nix
    ./apps/ghostty/default.nix
    ./apps/kitty/default.nix
    ./apps/lazygit/default.nix
    ./apps/opencode/default.nix
    ./apps/tmux/default.nix
    ./apps/vscode.nix
    ./apps/walker.nix
    ./apps/swayosd.nix
    ./desktop/nightlight.nix
    ./desktop/idle.nix
    ./desktop/hyprland/default.nix
    ./desktop/waybar/default.nix
    ./scripts/default.nix
    ./scripts/battery-monitor.nix
  ];


  colorScheme = lib.mkDefault (nomarchyLib.getColorScheme config.nomarchy.theme);

  nixpkgs.config.allowUnfree = true;

  # Enable neovim program module (required for stylix integration)
  programs.neovim.enable = lib.mkDefault true;

  home.packages = lib.mkDefault (with pkgs; [
    # Core applications
    firefox
    xfce.thunar

    # Media
    imv              # Image viewer
    mpv              # Video player

    # Hyprland ecosystem
    swww             # Wallpaper daemon
    mako             # Notification daemon
    hyprlock         # Lock screen

    # Screenshot and clipboard
    wl-clipboard
    grim
    slurp

    # Hardware control
    brightnessctl
    playerctl
    pamixer

    # Utilities
    jq
    xmlstarlet
    mise
    gum              # TUI components for scripts
    xdg-terminal-exec
    swaybg
    rofi-wayland

    # Theming
    yaru-theme
    everforest-gtk-variant
    bibata-cursors
  ]);
}
