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

let
  extensions_to_install =
    final:
    with final.vscode-extensions;
    let
      common = [
        bbenoist.nix
        ms-azuretools.vscode-docker
        ms-vscode-remote.remote-ssh
        esbenp.prettier-vscode
      ];
      work_specific = [
        # ms-python.vscode-pylance
      ]
      ++ (pkgs.vscode-utils.extensionsFromVscodeMarketplace [
        {
          name = "remote-server";
          publisher = "ms-vscode";
          version = "1.5.3";
          sha256 = "MSayIBwvSgIHg6gTrtUotHznvo5kTiveN8iSrehllW0=";
        }
        {
          name = "vscode-python-envs";
          publisher = "ms-python";
          version = "1.12.0";
          sha256 = "8dCnGBuxv+8QwP0FrUZIKFxllVOR2z+wIhyyJgxsRns=";
        }
        {
          name = "vscode-pylance";
          publisher = "ms-python";
          version = "2025.10.1";
          sha256 = "7NLq5huykRAhhEIHjtXLKDBFwQ5yiJKFLG64Tjh0yaM=";
        }
        {
          name = "debugpy";
          publisher = "ms-python";
          version = "2025.16.0";
          sha256 = "QPIDUzzwIfzyicsCwqU2u6hVSjCJMInHhHMBqvL8tYs=";
        }
        {
          name = "python";
          publisher = "ms-python";
          version = "2025.18.0";
          sha256 = "6auzfS1DmOuaqV2rDHSCC9BlrcON4xUrdJZ0N0wYCs4=";
        }
      ]);
      personal_specific = [
        ms-python.python
        ms-python.debugpy
        ms-vscode.cpptools # C/C++ support and debugging
        ms-vscode.cpptools-extension-pack
      ];
    in
    if config.me.vscode.enable_work_profile then
      common ++ work_specific
    else
      common ++ personal_specific;
in
{
  imports = [ ];

  options.me = {
    vscode.enable = lib.mkOption {
      type = lib.types.bool;
      default = false;
      example = true;
      description = "Whether we want to install vscode.";
    };

    vscode.enable_work_profile = lib.mkOption {
      type = lib.types.bool;
      default = false;
      example = true;
      description = "Whether we want to install the remote tunnels vscode extension.";
    };
  };

  config = lib.mkIf (config.me.vscode.enable && config.me.graphical) {
    allowedUnfree = [
      "vscode"
      "vscode-x86_64-unknown-linux-gnu-with-extensions"
      "vscode-with-extensions"
      "vscode-extension-ms-vscode-remote-remote-ssh"
      "vscode-extension-MS-python-vscode-pylance"
      "vscode-extension-ms-vscode-cpptools"
    ];

    environment.systemPackages = with pkgs; [
      vscode-with-extensions
    ];

    me.install.user.talexander.file = {
      ".config/Code/User/settings.json" = {
        source = ./files/settings.json;
      };
      ".config/Code/User/keybindings.json" = {
        source = ./files/keybindings.json;
      };
    };

    environment.persistence."/persist" = lib.mkIf (config.me.mountPersistence) {
      hideMounts = true;
      users.talexander = {
        directories = [
          {
            directory = ".config/Code/User/globalStorage";
            user = "talexander";
            group = "talexander";
            mode = "0755";
          }
          {
            directory = ".config/Code/User/workspaceStorage";
            user = "talexander";
            group = "talexander";
            mode = "0755";
          }
        ];
      };
    };

    environment.persistence."/state" = lib.mkIf (config.me.mountPersistence) {
      hideMounts = true;
      users.talexander = {
        directories = [
          {
            directory = ".config/Code/CachedProfilesData";
            user = "talexander";
            group = "talexander";
            mode = "0755";
          }
          {
            directory = ".config/Code/CachedConfigurations";
            user = "talexander";
            group = "talexander";
            mode = "0755";
          }
          {
            directory = ".config/Code/Cache";
            user = "talexander";
            group = "talexander";
            mode = "0755";
          }
          {
            directory = ".config/Code/CachedData";
            user = "talexander";
            group = "talexander";
            mode = "0755";
          }
        ];
      };
    };

    nixpkgs.overlays = [
      (final: prev: {
        vscode-with-extensions = (
          prev.vscode-with-extensions.override {
            vscodeExtensions = (extensions_to_install final);
          }
        );
      })
      (final: prev: {
        vscode-with-extensions = pkgs.symlinkJoin {
          name = prev.vscode-with-extensions.name;
          paths = [ prev.vscode-with-extensions ];
          buildInputs = [ pkgs.makeWrapper ];
          postBuild = ''
            wrapProgram $out/bin/code --add-flags --password-store=gnome-libsecret
          '';
        };
      })
    ];
  };
}
