load(
    "@bazel_skylib//rules:common_settings.bzl",
    "bool_flag",
)
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")
load("@bazel_gazelle//:def.bzl", "gazelle")
load("@rules_erlang//gazelle:def.bzl", "GAZELLE_ERLANG_RUNTIME_DEPS")
load("@rules_erlang//:erlang_bytecode2.bzl", "erlc_opts")
load("@rules_erlang//:dialyze.bzl", "DEFAULT_PLT_APPS", "plt")
load("@rules_erlang//:shell.bzl", "shell")
load("@rules_erlang//:erl_eval.bzl", "erl_eval")
load("@rules_erlang//gazelle:moduleindex.bzl", "moduleindex")
load("@rules_elixir//:iex_eval.bzl", "iex_eval")
load(":rabbitmq_home.bzl", "rabbitmq_home")
load(":rabbitmq_run.bzl", "rabbitmq_run", "rabbitmq_run_command")
load(":rabbitmqctl.bzl", "rabbitmqctl")
load(":dist.bzl", "package_generic_unix", "source_archive")
load(
    ":rabbitmq.bzl",
    "RABBITMQ_ERLC_OPTS",
    "RABBITMQ_TEST_ERLC_OPTS",
    "all_plugins",
    "without",
)

exports_files([
    "scripts/bazel/rabbitmq-run.sh",
    "scripts/bazel/rabbitmq-run.bat",
    "release-notes",
])

# gazelle:exclude .github
# gazelle:exclude .elixir_ls
# gazelle:exclude .erlang.mk
# gazelle:exclude bazel
# gazelle:exclude bazel-out
# gazelle:exclude deps/*/priv
# gazelle:exclude deps/accept
# gazelle:exclude deps/aten
# gazelle:exclude deps/base64url
# gazelle:exclude deps/cowboy
# gazelle:exclude deps/cowlib
# gazelle:exclude deps/credentials_obfuscation
# gazelle:exclude deps/csv
# gazelle:exclude deps/cth_styledout
# gazelle:exclude deps/cuttlefish
# gazelle:exclude deps/eetcd
# gazelle:exclude deps/elvis_mk
# gazelle:exclude deps/enough
# gazelle:exclude deps/gen_batch_server
# gazelle:exclude deps/getopt
# gazelle:exclude deps/gun
# gazelle:exclude deps/inet_tcp_proxy
# gazelle:exclude deps/jose
# gazelle:exclude deps/json
# gazelle:exclude deps/meck
# gazelle:exclude deps/observer_cli
# gazelle:exclude deps/osiris
# gazelle:exclude deps/prometheus
# gazelle:exclude deps/proper
# gazelle:exclude deps/quantile_estimator
# gazelle:exclude deps/ra
# gazelle:exclude deps/ranch
# gazelle:exclude deps/recon
# gazelle:exclude deps/redbug
# gazelle:exclude deps/seshat
# gazelle:exclude deps/stdout_formatter
# gazelle:exclude deps/syslog
# gazelle:exclude deps/sysmon_handler
# gazelle:exclude deps/systemd
# gazelle:exclude deps/thoas
# gazelle:exclude deps/*/deps
# gazelle:exclude deps/*/.erlang.mk
# gazelle:exclude deps/rabbitmq_cli/_build
# gazelle:exclude extra_deps
# gazelle:exclude packaging
# gazelle:exclude PACKAGES
# gazelle:exclude plugins
# gazelle:exclude release-notes
# gazelle:exclude logs
# gazelle:erlang_apps_dirs deps
# gazelle:erlang_skip_rules test_erlang_app
# gazelle:erlang_skip_rules ct_test
# gazelle:erlang_generate_beam_files_macro
# gazelle:erlang_generate_fewer_bytecode_rules
# gazelle:erlang_app_dep_exclude rabbitmq_cli
# gazelle:map_kind erlang_app rabbitmq_app //:rabbitmq.bzl
# gazelle:map_kind assert_suites2 assert_suites //:rabbitmq.bzl

# gazelle:erlang_module_source_lib Elixir.RabbitMQ.CLI.CommandBehaviour:rabbitmq_cli

gazelle(
    name = "gazelle",
    data = GAZELLE_ERLANG_RUNTIME_DEPS,
    extra_args = [
        "--verbose",
    ],
    gazelle = "@rules_erlang//gazelle:gazelle_erlang_binary",
)

gazelle(
    name = "gazelle-update-repos",
    command = "update-repos",
    data = GAZELLE_ERLANG_RUNTIME_DEPS,
    extra_args = [
        "--verbose",
        "--build_files_dir=bazel",
        "--recurse_with=gazelle-update-repos",
    ],
    gazelle = "@rules_erlang//gazelle:gazelle_erlang_binary",
)

bool_flag(
    name = "enable_test_build",
    build_setting_default = False,
    visibility = ["//visibility:public"],
)

config_setting(
    name = "test_build",
    flag_values = {
        "//:enable_test_build": "true",
    },
)

plt(
    name = "base_plt",
    apps = DEFAULT_PLT_APPS + [
        "compiler",
        "crypto",
    ],  # keep
    visibility = ["//visibility:public"],
)

PLUGINS = all_plugins(
    rabbitmq_workspace = "",
)

rabbitmq_home(
    name = "broker-home",
    plugins = PLUGINS,
)

rabbitmq_run(
    name = "rabbitmq-run",
    home = ":broker-home",
    visibility = ["//visibility:public"],
)

# Allows us to `bazel run broker`
# for the equivalent of `make run-broker`
rabbitmq_run_command(
    name = "broker",
    rabbitmq_run = ":rabbitmq-run",
    subcommand = "run-broker",
)

# Allows us to `bazel run background-broker`
# to start a broker in the background
rabbitmq_run_command(
    name = "background-broker",
    rabbitmq_run = ":rabbitmq-run",
    subcommand = "start-background-broker",
)

# Allows us to `bazel run stop-broker`
# Useful is broker started in the background
rabbitmq_run_command(
    name = "stop-broker",
    rabbitmq_run = ":rabbitmq-run",
    subcommand = "stop-node",
)

# Allows us to `bazel run start-cluster`
# for the equivalent of `make start-cluster`
rabbitmq_run_command(
    name = "start-cluster",
    rabbitmq_run = ":rabbitmq-run",
    subcommand = "start-cluster",
)

# Allows us to `bazel run stop-cluster`
# for the equivalent of `make stop-cluster`
rabbitmq_run_command(
    name = "stop-cluster",
    rabbitmq_run = ":rabbitmq-run",
    subcommand = "stop-cluster",
)

# `bazel run rabbitmqctl`
rabbitmqctl(
    name = "rabbitmqctl",
    home = ":broker-home",
    visibility = ["//visibility:public"],
)

rabbitmqctl(
    name = "rabbitmq-diagnostics",
    home = ":broker-home",
)

rabbitmqctl(
    name = "rabbitmq-plugins",
    home = ":broker-home",
)

rabbitmqctl(
    name = "rabbitmq-streams",
    home = ":broker-home",
)

rabbitmqctl(
    name = "rabbitmq-queues",
    home = ":broker-home",
)

rabbitmqctl(
    name = "rabbitmq-upgrade",
    home = ":broker-home",
)

shell(
    name = "repl",
    deps = PLUGINS,
)

erl_eval(
    name = "otp_version",
    outs = ["otp_version.txt"],
    expression = """{ok, Version} = file:read_file(filename:join([code:root_dir(), "releases", erlang:system_info(otp_release), "OTP_VERSION"])), file:write_file(os:getenv("OUTS"), Version), halt().""",
    visibility = ["//visibility:public"],
)

iex_eval(
    name = "elixir_version",
    outs = ["elixir_version.txt"],
    expression = """File.write!(System.get_env("OUTS"), System.version()); System.halt()""",
    visibility = ["//visibility:public"],
)

filegroup(
    name = "root-licenses",
    srcs = glob(["LICENSE*"]),
    visibility = ["//visibility:public"],
)

pkg_files(
    name = "scripts-files",
    srcs = [
        "scripts/bash_autocomplete.sh",
        "scripts/rabbitmq-script-wrapper",
        "scripts/rabbitmqctl-autocomplete.sh",
        "scripts/zsh_autocomplete.sh",
    ],
    prefix = "scripts",
    visibility = ["//visibility:public"],
)

pkg_files(
    name = "release-notes-files",
    srcs = glob([
        "release-notes/*.md",
        "release-notes/*.txt",
    ]),
    prefix = "release-notes",
    visibility = ["//visibility:public"],
)

package_generic_unix(
    name = "package-generic-unix",
    plugins = PLUGINS,
)

source_archive(
    name = "source_archive",
    plugins = PLUGINS,
)

moduleindex(
    name = "moduleindex",
    testonly = True,
    apps = PLUGINS + [
        "@ct_helper//:erlang_app",
        "@emqtt//:erlang_app",
        "@inet_tcp_proxy_dist//:erlang_app",
        "@meck//:erlang_app",
        "@proper//:erlang_app",
        "//deps/rabbitmq_ct_client_helpers:erlang_app",
        "//deps/rabbitmq_ct_helpers:erlang_app",
        "//deps/trust_store_http:erlang_app",
    ],
    tags = ["manual"],
)

alias(
    name = "test-logs",
    actual = "//bazel/util:test-logs",
)

alias(
    name = "remote-test-logs",
    actual = "//bazel/util:remote-test-logs",
)

alias(
    name = "test-node-data",
    actual = "//bazel/util:test-node-data",
)

alias(
    name = "remote-test-node-data",
    actual = "//bazel/util:remote-test-node-data",
)

erlc_opts(
    name = "erlc_opts",
    values = select({
        "@rules_erlang//:debug_build": without("+deterministic", RABBITMQ_ERLC_OPTS),
        "//conditions:default": RABBITMQ_ERLC_OPTS,
    }) + select({
        ":test_build": [
            "-DTEST=1",
            "+nowarn_export_all",
        ],
        "//conditions:default": [],
    }),  # keep
    visibility = [":__subpackages__"],
)

erlc_opts(
    name = "test_erlc_opts",
    values = select({
        "@rules_erlang//:debug_build": without("+deterministic", RABBITMQ_TEST_ERLC_OPTS),
        "//conditions:default": RABBITMQ_TEST_ERLC_OPTS,
    }),  # keep
    visibility = [":__subpackages__"],
)
