# Shared Home Manager base config for all desktop users.
# Provides common programs, services, session variables, shell setup,
# fonts, and dotfile management. Host home.nix files import this and
# override/extend as needed.
{ config, lib, pkgs, osConfig, ... }:
let
  cfg = config.baseHome;
in
{
  options.baseHome = {
    enable = lib.mkEnableOption "shared home-manager base config";
    userName = lib.mkOption {
      type = lib.types.str;
      description = "Username for home directory and services";
    };
    fullName = lib.mkOption {
      type = lib.types.str;
      description = "Full display name for git etc";
    };
    email = lib.mkOption {
      type = lib.types.str;
      description = "Email for git etc";
    };
    stateVersion = lib.mkOption {
      type = lib.types.str;
      default = "23.11";
      description = "Home Manager state version";
    };
    hostDir = lib.mkOption {
      type = lib.types.str;
      description = "Host directory name for dotfile symlinks (e.g. 'frame12')";
    };
  };

  imports = [
    ./programs.nix
    ./git-autosync.nix
    ./noctalia.nix
    ../wm/niri-home.nix
  ];

  config = lib.mkIf cfg.enable {
    home.username = cfg.userName;
    home.homeDirectory = "/home/${cfg.userName}";
    programs.home-manager.enable = true;
    home.stateVersion = cfg.stateVersion;

    sharedPrograms.enable = true;
    fonts.fontconfig.enable = true;

    # Stylix auto-theming
    stylix = {
      autoEnable = lib.mkDefault true;
      targets.firefox = {
        profileNames = lib.mkDefault [ "default" ];
        colorTheme.enable = lib.mkDefault true;
        firefoxGnomeTheme.enable = lib.mkDefault true;
      };
    };

    # Enable niri home config
    nirihome.enable = lib.mkDefault true;

    # Common session variables
    home.sessionVariables = {
      EDITOR = lib.mkDefault "hx";
      NIXOS_OZONE_WL = "1";
    };

    # Gnome keyring for SSH and secrets
    services.gnome-keyring.enable = true;
    services.gnome-keyring.components = [ "secrets" ]; # ssh handled by gcr-ssh-agent (gcr 4.x)

    # Bluetooth headphone controls
    services.mpris-proxy.enable = true;

    # Common packages (Qt theming, fonts, unix tools)
    home.packages = with pkgs; [
      # Qt theming for Stylix
      libsForQt5.qtstyleplugin-kvantum
      libsForQt5.qt5ct
      # Common unix tools
      lsd
      htop
      unzip
    ];

    # Git base config — hosts extend with user-specific settings
    programs.git = {
      enable = true;
      settings = {
        user.name = cfg.fullName;
        user.email = cfg.email;
        init.defaultBranch = "main";
        pull.ff = "only";
        merge.conflictStyle = "zdiff3";
        push.autoSetupRemote = "true";
      };
    };

    programs.bash.enable = true;

    # Zsh base setup
    programs.zsh = {
      enable = true;
      oh-my-zsh = {
        enable = true;
        plugins = [ "git" ];
        theme = lib.mkDefault "half-life";
      };
      initContent = lib.mkDefault ''
        # History
        HISTSIZE=10000
        SAVEHIST=10000
        setopt SHARE_HISTORY
        setopt APPEND_HISTORY

        # integrate ssh-agent from gnome keyring
        export SSH_AUTH_SOCK=/run/user/$UID/gcr/ssh
      '';
      shellAliases = {
        ls = "lsd";
        l = "lsd --almost-all --long";
        llm = "lsd --timesort --long";
        lS = "lsd --oneline --classic";
        lt = "lsd --tree --depth=2";
        cat = "bat --paging=never";
        rm = "rm -i";
      };
      syntaxHighlighting.enable = true;
    };

    # Shared dotfiles — all hosts use the same pattern
    home.file = lib.mkMerge [
      (
        let
          # Relative to the host's home.nix (hosts/<host>/modules/home-manager/)
          # After migration, shared dotfiles at ../../../../shared/dotfiles
          # But since this module lives in shared/, use a relative path from here
          sharedDotfilesPath = ../../dotfiles;
        in
        if builtins.pathExists sharedDotfilesPath then
          builtins.listToAttrs (
            map (name: {
              name = "${config.xdg.configHome}/${name}";
              value = {
                source = lib.mkDefault (sharedDotfilesPath + "/${name}");
              };
            }) (builtins.attrNames (builtins.readDir sharedDotfilesPath))
          )
        else
          { }
      )
    ];

    # Niri config symlink — each host has its own niri dotfiles
    xdg.configFile = {
      "niri".source = lib.mkDefault (
        config.lib.file.mkOutOfStoreSymlink
          "${osConfig.deskCfg.flakePath}/hosts/${cfg.hostDir}/linked-dotfiles/niri"
      );
    };
  };
}
