# Visual Studio Code (Microsoft build, unfree). Default extensions + settings shared by all
# hosts; per-host extras (peacock, comment-anchors, project-manager) go via
# `chiasson.home.editors.vscode.extensions`.
{ self, ... }: {
  flake.homeManagerModules.wisdomEditorsVscode =
    { config, lib, pkgs, ... }:
    let
      root = config.chiasson.home;
      cfg = config.chiasson.home.editors.vscode;

      defaultExtensions = with pkgs.vscode-extensions; [
        jnoortheen.nix-ide
        yzhang.markdown-all-in-one
        esbenp.prettier-vscode
      ];

      coreSettings = {
        "nix.enableLanguageServer" = true;
        "nix.serverPath" = "nixd";
        "nix.serverSettings" = {
          nixd = {
            formatting = {
              command = "nixfmt";
            };
          };
        };
        "diffEditor.ignoreTrimWhitespace" = false;
        "chat.disableAIFeatures" = true;
      };
    in
    {
      options.chiasson.home.editors.vscode = {
        enable = lib.mkEnableOption ''
          Visual Studio Code (Microsoft build; needs `nixpkgs.config.allowUnfree`). Core
          extensions (nix-ide, markdown-all-in-one, prettier) + nixd/nixfmt settings are included.
        '' // {
          default = false;
        };

        extensions = lib.mkOption {
          type = lib.types.listOf lib.types.package;
          default = [ ];
          description = ''
            Extra extensions not wanted on every host (e.g. peacock, comment-anchors,
            project-manager). Per-host via `chiasson.users.extraModules`.
          '';
        };
      };

      config = lib.mkIf (root.enable && cfg.enable) {
        home.packages = [ pkgs.vscode ];

        programs.vscode = {
          enable = true;
          package = pkgs.vscode;
          extensions = defaultExtensions ++ cfg.extensions;
          userSettings = coreSettings;
        };
      };
    };
}
