%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%
%% Copyright (c) 2007-2026 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
%%

%% region

{mapping, "cluster_formation.aws.region", "rabbit.cluster_formation.peer_discovery_aws.aws_ec2_region", [
    {datatype, string}
]}.

{translation, "rabbit.cluster_formation.peer_discovery_aws.aws_ec2_region",
fun(Conf) ->
    case cuttlefish:conf_get("cluster_formation.aws.region", Conf, undefined) of
        undefined -> cuttlefish:unset();
        Value     -> Value
    end
end}.

%% use_private_ip

{mapping, "cluster_formation.aws.use_private_ip", "rabbit.cluster_formation.peer_discovery_aws.aws_use_private_ip", [
    {datatype, boolean}
]}.

{translation, "rabbit.cluster_formation.peer_discovery_aws.aws_use_private_ip",
fun(Conf) ->
    case cuttlefish:conf_get("cluster_formation.aws.use_private_ip", Conf, undefined) of
        undefined -> cuttlefish:unset();
        Value     -> Value
    end
end}.

%% access_key_id

{mapping, "cluster_formation.aws.access_key_id", "rabbit.cluster_formation.peer_discovery_aws.aws_access_key", [
    {datatype, string}
]}.

{translation, "rabbit.cluster_formation.peer_discovery_aws.aws_access_key",
fun(Conf) ->
    case cuttlefish:conf_get("cluster_formation.aws.access_key_id", Conf, undefined) of
        undefined -> cuttlefish:unset();
        Value     -> Value
    end
end}.

%% secret_key

{mapping, "cluster_formation.aws.secret_key", "rabbit.cluster_formation.peer_discovery_aws.aws_secret_key", [
    {datatype, [tagged_string, string]}
]}.

{translation, "rabbit.cluster_formation.peer_discovery_aws.aws_secret_key",
fun(Conf) ->
    case cuttlefish:conf_get("cluster_formation.aws.secret_key", Conf, undefined) of
        undefined -> cuttlefish:unset();
        Value     -> Value
    end
end}.

%% use_autoscaling_group

{mapping, "cluster_formation.aws.use_autoscaling_group", "rabbit.cluster_formation.peer_discovery_aws.aws_autoscaling", [
    {datatype, boolean}
]}.

{translation, "rabbit.cluster_formation.peer_discovery_aws.aws_autoscaling",
fun(Conf) ->
    case cuttlefish:conf_get("cluster_formation.aws.use_autoscaling_group", Conf, undefined) of
        undefined -> cuttlefish:unset();
        Value     -> Value
    end
end}.

%% ec2_tags

{mapping, "cluster_formation.aws.instance_tags", "rabbit.cluster_formation.peer_discovery_aws.aws_ec2_tags", [
    {datatype, {enum, [none]}}
]}.

{mapping, "cluster_formation.aws.instance_tags.$name", "rabbit.cluster_formation.peer_discovery_aws.aws_ec2_tags", [
    {datatype, string}
]}.

{translation, "rabbit.cluster_formation.peer_discovery_aws.aws_ec2_tags",
fun(Conf) ->
    case cuttlefish:conf_get("cluster_formation.aws.instance_tags", Conf, undefined) of
        none -> [];
        _ ->
            Settings = cuttlefish_variable:filter_by_prefix("cluster_formation.aws.instance_tags", Conf),
            [ {lists:last(K), V} || {K, V} <- Settings ]
    end
end}.

%% hostname_path

{mapping, "cluster_formation.aws.hostname_path", "rabbit.cluster_formation.peer_discovery_aws.aws_hostname_path", [
    {datatype, string,
    {default, ["privateDnsName"]}}
]}.
{translation, "rabbit.cluster_formation.peer_discovery_aws.aws_hostname_path",
fun(Conf) ->
    case cuttlefish:conf_get("cluster_formation.aws.hostname_path", Conf, undefined) of
        undefined -> cuttlefish:unset();
        Value     -> rabbit_peer_discovery_util:as_list(Value)
    end
end}.

%% ec2_instance_states

{mapping, "cluster_formation.aws.ec2_instance_states.$state", "rabbit.cluster_formation.peer_discovery_aws.aws_ec2_instance_states", [
    {datatype, string}
]}.

{translation, "rabbit.cluster_formation.peer_discovery_aws.aws_ec2_instance_states",
fun(Conf) ->
    case cuttlefish_variable:filter_by_prefix("cluster_formation.aws.ec2_instance_states", Conf) of
        [] -> ["running", "pending"];
        Settings -> [V || {_K, V} <- Settings]
    end
end}.

%% hostname_paths (multiple paths support)

{mapping, "cluster_formation.aws.hostname_path.$number", "rabbit.cluster_formation.peer_discovery_aws.aws_hostname_paths", [
    {datatype, string}
]}.

{translation, "rabbit.cluster_formation.peer_discovery_aws.aws_hostname_paths",
fun(Conf) ->
    case cuttlefish_variable:filter_by_prefix("cluster_formation.aws.hostname_path", Conf) of
        [] ->
            cuttlefish:unset();
        L ->
            %% Extract and sort numbered paths (hostname_path.1, hostname_path.2, etc.)
            L1 = lists:map(
                   fun ({["cluster_formation", "aws", "hostname_path", N], V}) ->
                           case string:to_integer(N) of
                               {I, ""} ->
                                   {I, V};
                               _ ->
                                   cuttlefish:invalid(io_lib:format("Cannot convert ~p to an integer", [N]))
                           end;
                       (Other) ->
                           cuttlefish:invalid(io_lib:format("~p is invalid", [Other]))
                   end, L),
            [rabbit_peer_discovery_util:as_list(V) || {_, V} <- lists:sort(L1)]
    end
end}.
