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",
    "rabbitmq_suite",
)
load(
    ":app.bzl",
    "all_beam_files",
    "all_srcs",
    "all_test_beam_files",
    "test_suite_beam_files",
)

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 inets
# gazelle:erlang_app_extra_app public_key

# gazelle:erlang_app_dep rabbit
# gazelle:erlang_app_dep_exclude rabbit_common

rabbitmq_app(
    name = "erlang_app",
    srcs = [":all_srcs"],
    hdrs = [":public_hdrs"],
    app_description = "OAuth 2 and JWT-based AuthN and AuthZ backend",
    app_name = "rabbitmq_auth_backend_oauth2",
    beam_files = [":beam_files"],
    extra_apps = [
        "inets",
        "public_key",
    ],
    license_files = [":license_files"],
    priv = [":priv"],
    deps = [
        "//deps/oauth2_client:erlang_app",
        "//deps/rabbit:erlang_app",
        "@base64url//:erlang_app",
        "@cowlib//:erlang_app",
        "@cuttlefish//:erlang_app",
        "@jose//: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",
    compiled_suites = [
        ":test_jwks_http_app_beam",
        ":test_jwks_http_handler_beam",
        ":test_openid_http_handler_beam",
        ":test_jwks_http_sup_beam",
        ":test_rabbit_auth_backend_oauth2_test_util_beam",
        ":test_oauth2_http_mock_beam",
    ],
    target = ":test_erlang_app",
)

broker_for_integration_suites(
    extra_plugins = [
        "//deps/rabbitmq_web_mqtt:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "add_uaa_key_command_SUITE",
    size = "small",
)

rabbitmq_integration_suite(
    name = "add_signing_key_command_SUITE",
    size = "small",
)

rabbitmq_integration_suite(
    name = "config_schema_SUITE",
)

rabbitmq_integration_suite(
    name = "rabbit_oauth2_provider_SUITE",
    additional_beam = [
        "test/oauth2_http_mock.beam",
    ],
    runtime_deps = [
        "@cowboy//:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "rabbit_oauth2_resource_server_SUITE",
)

rabbitmq_integration_suite(
    name = "jwks_SUITE",
    additional_beam = [
        "test/rabbit_auth_backend_oauth2_test_util.beam",
        "test/jwks_http_app.beam",
        "test/jwks_http_handler.beam",
        "test/openid_http_handler.beam",
        "test/jwks_http_sup.beam",
    ],
    deps = [
        "@cowboy//:erlang_app",
    ],
)

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

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

rabbitmq_integration_suite(
    name = "system_SUITE",
    size = "medium",
    additional_beam = [
        "test/rabbit_auth_backend_oauth2_test_util.beam",
    ],
    runtime_deps = [
        "//deps/oauth2_client:erlang_app",
        "//deps/rabbitmq_amqp_client:erlang_app",
        "@emqtt//:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "unit_SUITE",
    size = "medium",
    additional_beam = [
        "test/rabbit_auth_backend_oauth2_test_util.beam",
    ],
)

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

assert_suites()

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