load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
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",
    "rabbitmq_app",
    "rabbitmq_suite",
)
load(
    ":app.bzl",
    "all_beam_files",
    "all_srcs",
    "all_test_beam_files",
    "test_suite_beam_files",
)

py_binary(
    name = "codegen",
    srcs = [
        "codegen.py",
    ],
    imports = ["../../deps/rabbitmq_codegen"],
    deps = [
        "//deps/rabbitmq_codegen:amqp_codegen",
    ],
)

genrule(
    name = "rabbit_framing",
    srcs = [
        "//deps/rabbitmq_codegen:amqp-rabbitmq-0.9.1.json",
        "//deps/rabbitmq_codegen:credit_extension.json",
        "//deps/rabbitmq_codegen:amqp-rabbitmq-0.8.json",
    ],
    outs = ["gen/rabbit_framing.hrl"],
    cmd = "$(location :codegen) --ignore-conflicts header $(SRCS) $@",
    tags = ["manual"],
    tools = [":codegen"],
)

genrule(
    name = "rabbit_framing_amqp_0_9_1",
    srcs = [
        "//deps/rabbitmq_codegen:amqp-rabbitmq-0.9.1.json",
        "//deps/rabbitmq_codegen:credit_extension.json",
    ],
    outs = ["gen/rabbit_framing_amqp_0_9_1.erl"],
    cmd = "$(location :codegen) body $(SRCS) $@",
    tags = ["manual"],
    tools = [":codegen"],
)

genrule(
    name = "rabbit_framing_amqp_0_8",
    srcs = [
        "//deps/rabbitmq_codegen:amqp-rabbitmq-0.8.json",
    ],
    outs = ["gen/rabbit_framing_amqp_0_8.erl"],
    cmd = "$(location :codegen) body $(SRCS) $@",
    tags = ["manual"],
    tools = [":codegen"],
)

write_source_files(
    name = "write_framing_sources",
    files = {
        "include/rabbit_framing.hrl": ":rabbit_framing",
        "src/rabbit_framing_amqp_0_8.erl": ":rabbit_framing_amqp_0_8",
        "src/rabbit_framing_amqp_0_9_1.erl": ":rabbit_framing_amqp_0_9_1",
    },
)

APP_EXTRA_KEYS = """
%% Hex.pm package informations.
	{licenses, ["MPL-2.0"]},
	{links, [
	    {"Website", "https://www.rabbitmq.com/"},
	    {"GitHub", "https://github.com/rabbitmq/rabbitmq-common"}
	  ]},
	{build_tools, ["make", "rebar3"]},
	{files, [
	    "erlang.mk",
	    "git-revisions.txt",
	    "include",
	    "LICENSE*",
	    "Makefile",
	    "rabbitmq-components.mk",
	    "README",
	    "README.md",
	    "mk"
	  ]}
"""

# gazelle:erlang_app_extra_app compiler
# gazelle:erlang_app_extra_app crypto
# gazelle:erlang_app_extra_app public_key
# gazelle:erlang_app_extra_app sasl
# gazelle:erlang_app_extra_app ssl
# gazelle:erlang_app_extra_app syntax_tools
# gazelle:erlang_app_extra_app tools
# gazelle:erlang_app_extra_app xmerl
# gazelle:erlang_app_extra_app runtime_tools
# gazelle:erlang_app_extra_app os_mon

# gazelle:erlang_app_dep_exclude ranch

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")

rabbitmq_app(
    name = "erlang_app",
    srcs = [":all_srcs"],
    hdrs = [":public_hdrs"],
    app_description = "Modules shared by rabbitmq-server and rabbitmq-erlang-client",
    app_extra_keys = APP_EXTRA_KEYS,
    app_name = "rabbit_common",
    beam_files = [":beam_files"],
    extra_apps = [
        "compiler",
        "crypto",
        "public_key",
        "sasl",
        "ssl",
        "syntax_tools",
        "tools",
        "xmerl",
        "os_mon",
        "runtime_tools",
    ],
    license_files = [":license_files"],
    priv = [":priv"],
    deps = [
        "@credentials_obfuscation//:erlang_app",
        "@ranch//:erlang_app",  # keep
        "@recon//:erlang_app",
        "@thoas//:erlang_app",
    ],
)

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

plt(
    name = "deps_plt",
    apps = [
        "mnesia",  # keep
    ],
    for_target = ":erlang_app",
    ignore_warnings = True,
    plt = "//:base_plt",
)

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

rabbitmq_suite(
    name = "rabbit_env_SUITE",
    size = "small",
    deps = [
        "@proper//:erlang_app",
    ],
)

rabbitmq_suite(
    name = "supervisor2_SUITE",
    size = "small",
    additional_beam = [
        "test/test_event_handler.beam",
    ],
)

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

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

rabbitmq_suite(
    name = "unit_SUITE",
    size = "medium",
    additional_beam = [
        "test/gen_server2_test_server.beam",
    ],
    deps = [
        "@credentials_obfuscation//:erlang_app",
        "@proper//:erlang_app",
    ],
)

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

assert_suites()

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

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