load("@rules_erlang//:eunit2.bzl", "eunit")
load("@rules_erlang//:xref2.bzl", "xref")
load("@rules_erlang//:dialyze.bzl", "dialyze", "plt")
load("//:rabbitmq_home.bzl", "rabbitmq_home")
load("//:rabbitmq_run.bzl", "rabbitmq_run")
load(
    "//:rabbitmq.bzl",
    "BROKER_VERSION_REQUIREMENTS_ANY",
    "RABBITMQ_DIALYZER_OPTS",
    "assert_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_NAME = "rabbitmq_shovel"

APP_DESCRIPTION = "Data Shovel for RabbitMQ"

APP_MODULE = "rabbit_shovel"

APP_ENV = """[
	    {defaults, [
	        {prefetch_count,     1000},
	        {ack_mode,           on_confirm},
	        {publish_fields,     []},
	        {publish_properties, []},
	        {reconnect_delay,    5}
	      ]}
	  ]"""

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 crypto

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 = ["crypto"],
    license_files = [":license_files"],
    priv = [":priv"],
    deps = [
        "//deps/amqp10_client:erlang_app",
        "//deps/amqp_client:erlang_app",
        "//deps/rabbit:erlang_app",
        "//deps/rabbit_common: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 = ["//deps/rabbitmq_cli: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",
    compiled_suites = [":test_shovel_test_utils_beam"],
    target = ":test_erlang_app",
)

rabbitmq_home(
    name = "broker-for-tests-home",
    plugins = [
        "//deps/rabbit:erlang_app",
        ":erlang_app",
    ],
)

rabbitmq_run(
    name = "rabbitmq-for-tests-run",
    home = ":broker-for-tests-home",
)

rabbitmq_integration_suite(
    name = "amqp10_dynamic_SUITE",
    additional_beam = [
        "test/shovel_test_utils.beam",
    ],
    flaky = True,
)

rabbitmq_suite(
    name = "amqp10_shovel_SUITE",
    size = "small",
    deps = [
        "//deps/amqp10_common:erlang_app",
        "@meck//:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "amqp10_SUITE",
)

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

rabbitmq_integration_suite(
    name = "configuration_SUITE",
)

rabbitmq_integration_suite(
    name = "delete_shovel_command_SUITE",
    additional_beam = [
        "test/shovel_test_utils.beam",
    ],
)

rabbitmq_integration_suite(
    name = "dynamic_SUITE",
    additional_beam = [
        "test/shovel_test_utils.beam",
    ],
    flaky = True,
)

rabbitmq_suite(
    name = "parameters_SUITE",
    size = "medium",
    deps = [
        "//deps/rabbit_common:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "rolling_upgrade_SUITE",
    additional_beam = [
        "test/shovel_test_utils.beam",
    ],
    # FIXME: As of this writing, there is a bug in Khepri that makes this
    # testsuite unstable.
    flaky = True,
    deps = [
        "@khepri//:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "shovel_status_command_SUITE",
    additional_beam = [
        "test/shovel_test_utils.beam",
    ],
)

assert_suites()

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