%%-*- mode: erlang -*-

{mapping, "exhook.request_failed_action", "emqx_exhook.request_failed_action", [
  {default, "deny"},
  {datatype, {enum, [ignore, deny]}}
]}.

{mapping, "exhook.request_timeout", "emqx_exhook.request_timeout", [
  {default, "5s"},
  {datatype, {duration, ms}}
]}.

{mapping, "exhook.auto_reconnect", "emqx_exhook.auto_reconnect", [
  {default, "60s"},
  {datatype, string}
]}.

{translation, "emqx_exhook.auto_reconnect", fun(Conf) ->
  case cuttlefish:conf_get("exhook.auto_reconnect", Conf) of
      "false" -> false;
      Dur ->
          case cuttlefish_duration:parse(Dur, ms) of
              Ms when is_integer(Ms) -> Ms;
              {error, Reason} -> error(Reason)
          end
  end
end}.

{mapping, "exhook.server.$name.url", "emqx_exhook.servers", [
  {datatype, string}
]}.

{mapping, "exhook.server.$name.ssl.cacertfile", "emqx_exhook.servers", [
  {datatype, string}
]}.

{mapping, "exhook.server.$name.ssl.certfile", "emqx_exhook.servers", [
  {datatype, string}
]}.

{mapping, "exhook.server.$name.ssl.keyfile", "emqx_exhook.servers", [
  {datatype, string}
]}.

{translation, "emqx_exhook.servers", fun(Conf) ->
  Filter  = fun(Opts) -> [{K, V} || {K, V} <- Opts, V =/= undefined] end,
  ServerOptions = fun(Prefix) ->
                      case http_uri:parse(cuttlefish:conf_get(Prefix ++ ".url", Conf)) of
                          {ok, {http, _, Host, Port, _, _}} ->
                              [{scheme, http}, {host, Host}, {port, Port}];
                          {ok, {https, _, Host, Port, _, _}} ->
                              [{scheme, https}, {host, Host}, {port, Port},
                               {ssl_options,
                                 Filter([{ssl, true},
                                         {certfile, cuttlefish:conf_get(Prefix ++ ".ssl.certfile", Conf, undefined)},
                                         {keyfile, cuttlefish:conf_get(Prefix ++ ".ssl.keyfile", Conf, undefined)},
                                         {cacertfile, cuttlefish:conf_get(Prefix ++ ".ssl.cacertfile", Conf, undefined)}
                                        ])}];
                          _ -> error(invalid_server_options)
                      end
                  end,
  [{list_to_atom(Name), ServerOptions("exhook.server." ++ Name)}
   || {["exhook", "server", Name, "url"], _} <- cuttlefish_variable:filter_by_prefix("exhook.server", Conf)]
end}.
