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

with lib;

let
  cfg = config.home.roles.base;
in
{
  options.home.roles.base = {
    enable = mkEnableOption "Enable base CLI tools and essential programs";
  };

  config = mkIf cfg.enable {
    home.packages = with pkgs; [
      fd
      glances
      gzip
      htop
      killall
      less
      ncdu
      shellcheck
      tmux
      tree
      watch
    ];

    # Automatic garbage collection for user profile (home-manager generations).
    # This complements system-level gc which only cleans system generations.
    # - Linux: Uses --delete-older-than to keep 10-day rollback window
    # - Darwin: Overridden to use -d in base-darwin role to avoid launchd bug
    #   (https://github.com/nix-community/home-manager/issues/7211)
    nix.gc = {
      automatic = true;
      randomizedDelaySec = mkIf pkgs.stdenv.isLinux "14m";
      options = lib.mkDefault "--delete-older-than 10d";
    };

    # Essential programs everyone needs
    programs.bash = {
      enable = true;
      initExtra = ''
        codex() {
          local key
          key="$(rbw get openai-api-key-codex)"
          OPENAI_API_KEY="$key" command codex "$@"
        }
      '';
    };

    programs.home-manager.enable = true;
    programs.command-not-found.enable = true;

    programs.git = {
      enable = true;
      settings = {
        user.name = "John Ogle";
        user.email = "john@ogle.fyi";
        safe.directory = "/etc/nixos";
      };
    };

    programs.jq.enable = true;

    programs.neovim = {
      enable = true;
      viAlias = true;
      vimAlias = true;
    };

    programs.ssh = {
      enable = true;
      enableDefaultConfig = false;
      matchBlocks = {
        "*" = {
          addKeysToAgent = "yes";
        };
        "nucdeb1" = {
          hostname = "nucdeb1.oglehome";
          user = "root";
        };
      };
    };

    programs.rbw = {
      enable = true;
      settings = {
        email = "john@johnogle.info";
        base_url = "https://bitwarden.johnogle.info";
        pinentry = pkgs.pinentry-qt;
      };
    };

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