{ lib, pkgs, config, ... }:
with lib;
let
  cfg = config.mae.nvim;
  lspLangConfigType = types.submodule {
    options = {
      enable = mkEnableOption "Enable named lsp lang config";
      script = mkOption {
        type = types.lines;
        default = "{}";
      };
    };
  };
  mkLspLangConfig = name: cfg: ''
    require'lspconfig'["${name}"].setup(require'coq'.lsp_ensure_capabilities(${cfg.script}))
  '';
  mkLspLangConfigs = cfgs: lib.strings.concatStringsSep "\n" ((lib.attrsets.mapAttrsToList mkLspLangConfig cfgs));
in
{
  options.mae.nvim.lsp = {
    enable = mkEnableOption "Enable lsp support in nvim with nvim-lspconfig and cmp-nvim";
    servers = mkOption {
      type = types.attrsOf lspLangConfigType;
      default = [ ];
    };
  };

  config = mkIf cfg.lsp.enable {
    programs.neovim = {
      plugins = with pkgs.vimPlugins; [
        {
          plugin = coq_nvim;
          config = ''
            vim.g.coq_settings = { 
              xdg = true,
              auto_start = true
              }
          '';
          type = "lua";
        }
        {
          plugin = nvim-lspconfig;
          config = mkLspLangConfigs cfg.lsp.servers;
          type = "lua";
        }
      ];
    };
  };
}