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

APP_NAME = "rabbitmq_stream"

APP_DESCRIPTION = "RabbitMQ Stream"

APP_MODULE = "rabbit_stream"

APP_ENV = """[
	{tcp_listeners, [5552]},
	{num_tcp_acceptors, 10},
	{tcp_listen_options, [{backlog,   128},
                          {nodelay,   true}]},
	{ssl_listeners, []},
	{num_ssl_acceptors, 10},
	{ssl_listen_options, []},
	{initial_credits, 50000},
	{credits_required_for_unblocking, 12500},
	{frame_max, 1048576},
	{heartbeat, 60},
	{advertised_host, undefined},
	{advertised_port, undefined}
]"""

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 ssl

# gazelle:erlang_app_dep_exclude rabbit_common

rabbitmq_app(
    name = "erlang_app",
    srcs = [":all_srcs"],
    hdrs = [":public_hdrs"],
    app_description = APP_DESCRIPTION,
    app_env = APP_ENV,
    app_module = APP_MODULE,
    app_name = APP_NAME,
    beam_files = [":beam_files"],
    extra_apps = ["ssl"],
    license_files = [":license_files"],
    priv = [":priv"],
    deps = [
        "//deps/rabbit:erlang_app",
        "//deps/rabbitmq_stream_common:erlang_app",
        "@osiris//:erlang_app",
        "@ranch//:erlang_app",
    ],
)

xref(
    name = "xref",
    additional_libs = [
        "//deps/rabbitmq_cli:erlang_app",  # keep
    ],
    target = ":erlang_app",
)

plt(
    name = "deps_plt",
    for_target = ":erlang_app",
    ignore_warnings = True,
    libs = ["@rules_elixir//elixir"],  # keep
    plt = "//:base_plt",
    deps = ["//deps/rabbitmq_cli:erlang_app"],  # keep
)

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

eunit(
    name = "eunit",
    target = ":test_erlang_app",
)

broker_for_integration_suites(
)

rabbitmq_integration_suite(
    name = "commands_SUITE",
    additional_beam = [
        ":rabbit_stream_SUITE_beam_files",
    ],
    data = glob(["test/rabbit_stream_SUITE_data/**/*"]),
    flaky = True,
    deps = [
        "//deps/rabbitmq_stream_common:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "config_schema_SUITE",
)

rabbitmq_integration_suite(
    name = "rabbit_stream_utils_SUITE",
)

rabbitmq_integration_suite(
    name = "rabbit_stream_manager_SUITE",
)

rabbitmq_integration_suite(
    name = "rabbit_stream_reader_SUITE",
    deps = [
        "//deps/rabbitmq_stream_common:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "rabbit_stream_SUITE",
    shard_count = 3,
    deps = [
        "//deps/rabbit:erlang_app",
        "//deps/rabbitmq_stream_common:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "protocol_interop_SUITE",
    size = "medium",
    runtime_deps = [
        "//deps/amqp10_client:erlang_app",
    ],
)

assert_suites()

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