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

with lib;

let

  cfg = config.graylog.stream;

in {

  options.graylog.stream = mkOption {
    default = { };
    type = with types;
      attrsOf (submodule ({ name, ... }: {
        options = {
          title = mkOption {
            default = name;
            type = with types; str;
          };
          index_set_id = mkOption { type = with types; str; };
          disabled = mkOption {
            default = false;
            type = with types; bool;
          };
          matching_type = mkOption {
            default = "AND";
            type = with types; str;
          };
          pipelines = mkOption {
            default = [ ];
            type = with types; listOf str;
          };
        };
      }));
  };

  config = mkIf (cfg != { }) {
    resource.graylog_stream = mapAttrs (name: value: {
      inherit (value) title index_set_id disabled matching_type;
    }) cfg;

    resource.graylog_pipeline_connection = mapAttrs (name: pipelineConfig: {
      stream_id = "\${graylog_stream.${name}.id}";
      pipeline_ids = pipelineConfig.pipelines;
    }) cfg;
  };
}