load("@rules_erlang//:eunit2.bzl", "eunit")
load("@rules_erlang//:xref2.bzl", "xref")
load("@rules_erlang//:dialyze.bzl", "dialyze", "plt")
load(
    "//:rabbitmq.bzl",
    "BROKER_VERSION_REQUIREMENTS_ANY",
    "RABBITMQ_DIALYZER_OPTS",
    "assert_suites",
    "broker_for_integration_suites",
    "rabbitmq_app",
    "rabbitmq_integration_suite",
    "rabbitmq_suite",
)
load(
    ":app.bzl",
    "all_beam_files",
    "all_srcs",
    "all_test_beam_files",
    "test_suite_beam_files",
)

APP_ENV = """[
	    {servers,               undefined},
	    {user_bind_pattern,     none},
	    {user_dn_pattern,       "${username}"},
	    {dn_lookup_attribute,   none},
	    {dn_lookup_base,        none},
	    {group_lookup_base,     none},
	    {dn_lookup_bind,        as_user},
	    {other_bind,            as_user},
	    {anon_auth,             false},
	    {vhost_access_query,    {constant, true}},
	    {resource_access_query, {constant, true}},
	    {topic_access_query,    {constant, true}},
	    {tag_queries,           [{administrator, {constant, false}}]},
	    {use_ssl,               false},
	    {use_starttls,          false},
	    {ssl_options,           []},
	    {port,                  389},
	    {timeout,               infinity},
	    {log,                   false},
	    {pool_size,             64},
	    {idle_timeout,          300000}
	  ]"""

APP_NAME = "rabbitmq_auth_backend_ldap"

APP_DESCRIPTION = "RabbitMQ LDAP Authentication Backend"

APP_MODULE = "rabbit_auth_backend_ldap_app"

all_beam_files(name = "all_beam_files")

all_test_beam_files(name = "all_test_beam_files")

all_srcs(name = "all_srcs")

test_suite_beam_files(name = "test_suite_beam_files")

# gazelle:erlang_app_extra_app eldap
# gazelle:erlang_app_extra_app public_key

rabbitmq_app(
    name = "erlang_app",
    srcs = [":all_srcs"],
    hdrs = [":public_hdrs"],
    app_description = APP_DESCRIPTION,
    app_env = APP_ENV,
    app_extra_keys = BROKER_VERSION_REQUIREMENTS_ANY,
    app_module = APP_MODULE,
    app_name = APP_NAME,
    beam_files = [":beam_files"],
    extra_apps = [
        "eldap",
        "public_key",
    ],
    license_files = [":license_files"],
    priv = [":priv"],
    deps = [
        "//deps/rabbit:erlang_app",
        "//deps/rabbit_common:erlang_app",
    ],
)

xref(
    name = "xref",
    target = ":erlang_app",
)

plt(
    name = "deps_plt",
    for_target = ":erlang_app",
    ignore_warnings = True,
    plt = "//:base_plt",
)

dialyze(
    name = "dialyze",
    dialyzer_opts = RABBITMQ_DIALYZER_OPTS,
    plt = ":deps_plt",
    target = ":erlang_app",
)

eunit(
    name = "eunit",
    compiled_suites = [":test_rabbit_ldap_seed_beam"],
    target = ":test_erlang_app",
)

broker_for_integration_suites()

rabbitmq_integration_suite(
    name = "config_schema_SUITE",
)

rabbitmq_integration_suite(
    name = "system_SUITE",
    size = "medium",
    additional_beam = [
        "test/rabbit_ldap_seed.beam",
    ],
    data = [
        "example/global.ldif",
        "example/memberof_init.ldif",
        "example/refint_1.ldif",
        "example/refint_2.ldif",
    ],
    tags = [
        "ldap",
    ],
)

rabbitmq_suite(
    name = "unit_SUITE",
    size = "small",
)

assert_suites()

alias(
    name = "rabbitmq_auth_backend_ldap",
    actual = ":erlang_app",
    visibility = ["//visibility:public"],
)
