{
  config,
  lib,
  namespace,
  ...
}:
with lib;
let
  cfg = config.${namespace}.services.code-server;
in
{
  imports = [ ./options.nix ];

  config = mkIf cfg.enable {
    # Configure the standard NixOS code-server service
    services.code-server = {
      enable = true;
      port = cfg.port;
      user = cfg.user;
      group = cfg.group;
      host = cfg.host;
      auth = cfg.auth;
      disableTelemetry = cfg.disableTelemetry;
      disableUpdateCheck = cfg.disableUpdateCheck;
      extraEnvironment = cfg.extraEnvironment;
    }
    // optionalAttrs (cfg.hashedPassword != null) {
      hashedPassword = cfg.hashedPassword;
    };

    # Open firewall for code-server if enabled
    networking.firewall = mkIf cfg.openFirewall {
      allowedTCPPorts = [ cfg.port ];
      allowedUDPPorts = [ cfg.port ];
    };
  };
}
