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

{
  # Home Manager configuration for Darwin work laptop
  # Corporate-friendly setup with essential development tools

  home.username = lib.mkForce "johno";
  home.homeDirectory = lib.mkForce "/Users/johno";
  home.stateVersion = "24.05";

  # System packages
  home.packages = with pkgs; [
    google-cloud-sdk
  ];

  # Note: ghostty installed via Homebrew (managed outside of nix)

  # Override Darwin-incompatible settings from base role
  programs.rbw.settings.pinentry = lib.mkForce pkgs.pinentry_mac;

  # Disable Home Manager from managing shell RC files
  # topsoil/compost will manage these files instead
  programs.bash.enable = lib.mkForce false;
  programs.zsh.enable = lib.mkForce false;

  # Create a local nix integration file that topsoil-managed configs can source
  home.file.".nix-integration.sh" = {
    text = ''
      # Source Home Manager session variables (nix paths, environment, etc.)
      if [ -e /etc/profiles/per-user/johno/etc/profile.d/hm-session-vars.sh ]; then
        . /etc/profiles/per-user/johno/etc/profile.d/hm-session-vars.sh
      fi

      # Setup bash completions from nix profiles
      if [[ ! -v BASH_COMPLETION_VERSINFO ]] && [ -n "$NIX_PROFILES" ]; then
        for profile in $NIX_PROFILES; do
          if [ -f "$profile/etc/profile.d/bash_completion.sh" ]; then
            . "$profile/etc/profile.d/bash_completion.sh"
            break
          fi
        done
      fi

      # command-not-found handler
      command_not_found_handle() {
        local p=/nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite
        if [ -n "$NIX_PROFILES" ]; then
          for profile in $NIX_PROFILES; do
            if [ -x "$profile/bin/command-not-found" ] && [ -f "$p" ]; then
              "$profile/bin/command-not-found" "$@"
              return $?
            fi
          done
        fi
        echo "$1: command not found" >&2
        return 127
      }
    '';
  };

  home.file.".nix-integration.zsh" = {
    text = ''
      # Source Home Manager session variables (nix paths, environment, etc.)
      if [ -e /etc/profiles/per-user/johno/etc/profile.d/hm-session-vars.sh ]; then
        . /etc/profiles/per-user/johno/etc/profile.d/hm-session-vars.sh
      fi

      # Setup zsh completions from nix profiles
      typeset -U path cdpath fpath manpath
      for profile in ''${(z)NIX_PROFILES}; do
        fpath+=($profile/share/zsh/site-functions $profile/share/zsh/$ZSH_VERSION/functions $profile/share/zsh/vendor-completions)
      done
      autoload -U compinit && compinit

      # command-not-found handler
      command_not_found_handler() {
        local p=/nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite
        if [ -n "$NIX_PROFILES" ]; then
          for profile in ''${(z)NIX_PROFILES}; do
            if [ -x "$profile/bin/command-not-found" ] && [ -f "$p" ]; then
              "$profile/bin/command-not-found" "$@"
              return $?
            fi
          done
        fi
        echo "$1: command not found" >&2
        return 127
      }
    '';
  };

  # Keep SSH and Git disabled to avoid conflicts with work environment
  programs.ssh.enable = lib.mkForce false;
  programs.git.enable = lib.mkForce false;
  programs.rbw.enable = lib.mkForce false;

  home.shell.enableShellIntegration = true;

  home.roles = {
    base.enable = true;
    development = {
      enable = true;
      allowArbitraryClaudeCodeModelSelection = true;
    };
    tmux.enable = true;
    emacs.enable = true;
    aerospace = {
      enable = true;
      leader = "cmd";
      ctrlShortcuts.enable = true;
      sketchybar.enable = true;
      # Optional: Add per-machine userSettings overrides
      # userSettings = {
      #   mode.main.binding."${leader}-custom" = "custom-command";
      # };
    };
  };

  imports = [
    ./roles
    ./roles/base-darwin
  ];
}
