module(name = "net_eagle0")

# Version constants
SCALA_VERSION = "3.7.4"

NETTY_VERSION = "4.1.127.Final"

SCALAPB_VERSION = "1.0.0-alpha.1"

AWS_SDK_VERSION = "2.41.18"

#
# Core Build Tools
#

bazel_dep(name = "bazel_skylib", version = "1.9.0")
bazel_dep(name = "platforms", version = "1.0.0")
bazel_dep(name = "rules_pkg", version = "1.2.0")

#
# Language Support - Scala
#

bazel_dep(name = "rules_scala", version = "7.2.1")

scala_config = use_extension(
    "@rules_scala//scala/extensions:config.bzl",
    "scala_config",
)
scala_config.settings(scala_version = SCALA_VERSION)

scala_deps = use_extension(
    "@rules_scala//scala/extensions:deps.bzl",
    "scala_deps",
)
scala_deps.scala()
scala_deps.scalatest()
scala_deps.scala_proto()

#
# Language Support - C++
#

bazel_dep(name = "toolchains_llvm", version = "1.6.0")

llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm")

# Native toolchain (macOS -> macOS, Linux -> Linux)
llvm.toolchain(
    name = "llvm_toolchain",
    llvm_version = "20.1.4",
)

# Cross-compilation toolchain (macOS -> Linux x86_64)
# Uses the same LLVM distribution but with a Linux sysroot
llvm.toolchain(
    name = "llvm_toolchain_linux",
    llvm_version = "20.1.4",
)

# Linux x86_64 sysroot for cross-compilation
llvm.sysroot(
    name = "llvm_toolchain_linux",
    label = "@linux_sysroot//sysroot",
    targets = ["linux-x86_64"],
)

# Cross-compilation toolchain (macOS -> Linux ARM64)
llvm.toolchain(
    name = "llvm_toolchain_linux_arm64",
    llvm_version = "20.1.4",
)

# Linux ARM64 sysroot for cross-compilation
llvm.sysroot(
    name = "llvm_toolchain_linux_arm64",
    label = "@linux_sysroot_arm64//sysroot",
    targets = ["linux-aarch64"],
)

use_repo(llvm, "llvm_toolchain", "llvm_toolchain_linux", "llvm_toolchain_linux_arm64")

# Download the Linux sysroots (Ubuntu 24.04 Noble for C++23 support)
# Built by: .github/workflows/build_sysroot.yml
# To rebuild: Run the "Build Linux Sysroot" workflow with a new version, then update sha256 and URL
sysroot = use_repo_rule("@toolchains_llvm//toolchain:sysroot.bzl", "sysroot")

# x86_64 sysroot
sysroot(
    name = "linux_sysroot",
    sha256 = "a06475004fe8003ae7ccb4fe1d5511feb9b27cce4a8826eb1dfd686ed83f3dba",
    urls = ["https://eagle0-sysroot.sfo3.digitaloceanspaces.com/v3/ubuntu_noble_amd64_sysroot.tar.xz"],
)

# ARM64 sysroot
sysroot(
    name = "linux_sysroot_arm64",
    sha256 = "87469137737e09bc73855007dab835477eb10a7b3ce3f725f93f64e25747f3f9",
    urls = ["https://eagle0-sysroot.sfo3.digitaloceanspaces.com/v4/ubuntu_noble_arm64_sysroot.tar.xz"],
)

#
# Language Support - Go
#

bazel_dep(name = "rules_go", version = "0.59.0", repo_name = "io_bazel_rules_go")
bazel_dep(name = "gazelle", version = "0.47.0", repo_name = "bazel_gazelle")

go_sdk = use_extension("@io_bazel_rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(version = "1.23.3")
use_repo(go_sdk, "go_default_sdk")

go_deps = use_extension("@bazel_gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")
use_repo(
    go_deps,
    "com_github_aws_aws_sdk_go_v2",
    "com_github_aws_aws_sdk_go_v2_config",
    "com_github_aws_aws_sdk_go_v2_credentials",
    "com_github_aws_aws_sdk_go_v2_service_s3",
    "com_github_golang_jwt_jwt_v5",
    "com_github_google_uuid",
    "com_github_webview_webview_go",
    "org_golang_google_grpc",
    "org_golang_google_protobuf",
    "org_golang_x_sys",
)

#
# Language Support - Rust
#

bazel_dep(name = "rules_rust", version = "0.68.1")

rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(edition = "2021")
use_repo(rust, "rust_toolchains")

register_toolchains("@rust_toolchains//:all")

crate = use_extension("@rules_rust//crate_universe:extension.bzl", "crate")
crate.from_cargo(
    name = "map_generator_crates",
    cargo_lockfile = "//src/main/rust/net/eagle0/eagle/map_generator:Cargo.lock",
    manifests = ["//src/main/rust/net/eagle0/eagle/map_generator:Cargo.toml"],
)
use_repo(crate, "map_generator_crates")

#
# Platform Support - Apple/iOS
#

bazel_dep(name = "apple_support", version = "1.24.1", repo_name = "build_bazel_apple_support")
bazel_dep(name = "rules_apple", version = "4.3.3", repo_name = "build_bazel_rules_apple")

# Register Apple CC toolchain for Objective-C compilation
apple_cc_configure = use_extension(
    "@build_bazel_apple_support//crosstool:setup.bzl",
    "apple_cc_configure_extension",
)
use_repo(apple_cc_configure, "local_config_apple_cc", "local_config_apple_cc_toolchains")

#
# Protocol Buffers & RPC
#

bazel_dep(name = "protobuf", version = "33.5", repo_name = "com_google_protobuf")

# Use pre-built protoc binaries instead of compiling from source.
# See: https://protobuf.dev/reference/cpp/cpp-generated/#invocation
prebuilt_protoc = use_extension("@com_google_protobuf//bazel/private:prebuilt_protoc_extension.bzl", "protoc")
use_repo(
    prebuilt_protoc,
    "prebuilt_protoc.linux_aarch_64",
    "prebuilt_protoc.linux_ppcle_64",
    "prebuilt_protoc.linux_s390_64",
    "prebuilt_protoc.linux_x86_32",
    "prebuilt_protoc.linux_x86_64",
    "prebuilt_protoc.osx_aarch_64",
    "prebuilt_protoc.osx_x86_64",
    "prebuilt_protoc.win32",
    "prebuilt_protoc.win64",
)

bazel_dep(name = "rules_proto", version = "7.1.0")
bazel_dep(name = "rules_cc", version = "0.2.14")
bazel_dep(name = "grpc", version = "1.78.0")

# Patch grpc 1.78.0 to fix rules_python incompatibility: grpc requests Python
# 3.14.0b2 toolchain but rules_python >= 1.6.0 removed it in favor of 3.14.
# Upstream fix: https://github.com/grpc/grpc/commit/459279b
# TODO: Remove this override once a fixed grpc version is published to BCR.
single_version_override(
    module_name = "grpc",
    patch_strip = 1,
    patches = ["//third_party/patches:grpc_fix_python_version.patch"],
    version = "1.78.0",
)

bazel_dep(name = "grpc-java", version = "1.78.0")
bazel_dep(name = "rules_proto_grpc_csharp", version = "5.8.0")
bazel_dep(name = "flatbuffers", version = "25.12.19")

#
# Testing
#

bazel_dep(name = "googletest", version = "1.17.0")

#
# Container Images (OCI)
#

bazel_dep(name = "rules_oci", version = "2.2.7")
bazel_dep(name = "aspect_bazel_lib", version = "2.22.5")

oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")

# Base image for Eagle (Java 25 JRE - smaller runtime, no dev tools needed)
# Digest pinned for Bazel repository cache hit; update with:
#   TOKEN=$(curl -s "https://auth.docker.io/token?service=registry.docker.io&scope=repository:library/eclipse-temurin:pull" | python3 -c "import json,sys; print(json.load(sys.stdin)['token'])")
#   curl -s -H "Authorization: Bearer $TOKEN" -H "Accept: application/vnd.oci.image.index.v1+json,application/vnd.docker.distribution.manifest.list.v2+json" "https://registry-1.docker.io/v2/library/eclipse-temurin/manifests/25-jre" | python3 -c "import json,sys; m=json.load(sys.stdin); [print(p['digest']) for p in m.get('manifests',[]) if p['platform']['architecture']=='amd64']"
oci.pull(
    name = "eclipse_temurin_25_jre",
    digest = "sha256:98236ffdfb61cff3fc4f8e40e5b80460a5a4c35a0f56a5b4182a926844a7db49",
    image = "docker.io/library/eclipse-temurin",
    platforms = ["linux/amd64"],
    tag = "25-jre",
)

# Base image for JFR sidecar (Java 25 JDK - includes jcmd for JFR dumps)
oci.pull(
    name = "eclipse_temurin_25_jdk",
    digest = "sha256:bc2d562355813350f47bc472b9721b0b84761866671ad95d9a9f1151fa69da6e",
    image = "docker.io/library/eclipse-temurin",
    platforms = ["linux/amd64"],
    tag = "25-jdk",
)

# Base image for Shardok (Ubuntu 24.04 for C++ runtime)
oci.pull(
    name = "ubuntu_24_04",
    image = "docker.io/library/ubuntu",
    platforms = [
        "linux/amd64",
        "linux/arm64/v8",
    ],
    tag = "24.04",
)

# Base image for Admin Server (Alpine for lightweight Go binary)
oci.pull(
    name = "alpine_linux",
    image = "docker.io/library/alpine",
    platforms = ["linux/amd64"],
    tag = "3.21",
)
use_repo(oci, "alpine_linux", "alpine_linux_linux_amd64", "eclipse_temurin_25_jdk", "eclipse_temurin_25_jdk_linux_amd64", "eclipse_temurin_25_jre", "eclipse_temurin_25_jre_linux_amd64", "ubuntu_24_04", "ubuntu_24_04_linux_amd64", "ubuntu_24_04_linux_arm64_v8")

#
# Java/Scala Dependencies
#

bazel_dep(name = "rules_java", version = "9.3.0")
bazel_dep(name = "rules_jvm_external", version = "6.10")

maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
maven.install(
    artifacts = [
        # Netty
        "io.netty:netty-codec:%s" % NETTY_VERSION,
        "io.netty:netty-codec-http:%s" % NETTY_VERSION,
        "io.netty:netty-codec-socks:%s" % NETTY_VERSION,
        "io.netty:netty-codec-http2:%s" % NETTY_VERSION,
        "io.netty:netty-handler:%s" % NETTY_VERSION,
        "io.netty:netty-buffer:%s" % NETTY_VERSION,
        "io.netty:netty-transport:%s" % NETTY_VERSION,
        "io.netty:netty-resolver:%s" % NETTY_VERSION,
        "io.netty:netty-common:%s" % NETTY_VERSION,
        "io.netty:netty-handler-proxy:%s" % NETTY_VERSION,

        # ScalaPB
        "com.thesamet.scalapb:lenses_3:%s" % SCALAPB_VERSION,
        "com.thesamet.scalapb:scalapb-json4s_3:%s" % SCALAPB_VERSION,
        "com.thesamet.scalapb:scalapb-runtime_3:%s" % SCALAPB_VERSION,
        "com.thesamet.scalapb:scalapb-runtime-grpc_3:%s" % SCALAPB_VERSION,
        "com.thesamet.scalapb:compilerplugin_3:%s" % SCALAPB_VERSION,
        "com.thesamet.scalapb:protoc-bridge_3:0.9.9",

        # JSON
        "org.json4s:json4s-ast_3:4.1.0-M8",
        "org.json4s:json4s-core_3:4.1.0-M8",
        "org.json4s:json4s-native_3:4.1.0-M8",

        # Testing
        "org.scalamock:scalamock_3:7.4.1",

        # AWS SDK
        "software.amazon.awssdk:s3-transfer-manager:%s" % AWS_SDK_VERSION,
        "software.amazon.awssdk:s3:%s" % AWS_SDK_VERSION,
        "software.amazon.awssdk:regions:%s" % AWS_SDK_VERSION,
        "software.amazon.awssdk:aws-core:%s" % AWS_SDK_VERSION,
        "software.amazon.awssdk:sdk-core:%s" % AWS_SDK_VERSION,
        "software.amazon.awssdk:utils:%s" % AWS_SDK_VERSION,
        "software.amazon.awssdk:http-client-spi:%s" % AWS_SDK_VERSION,

        # AWS Lambda
        "com.amazonaws:aws-lambda-java-core:1.2.3",
        "com.amazonaws:aws-lambda-java-events:3.13.0",

        # Logging
        "org.slf4j:slf4j-api:2.0.16",
        "org.slf4j:slf4j-simple:2.0.16",

        # Other
        "org.reactivestreams:reactive-streams:1.0.4",
        "javax.xml.bind:jaxb-api:2.3.1",

        # OkHttp (for SSE with read timeout support, OAuth HTTP calls)
        "com.squareup.okhttp3:okhttp:4.12.0",
        "com.squareup.okhttp3:okhttp-sse:4.12.0",

        # JWT (for OAuth token handling)
        "com.nimbusds:nimbus-jose-jwt:9.37.3",

        # Error tracking
        "io.sentry:sentry:8.31.0",

        # SQLite for client text storage
        "org.xerial:sqlite-jdbc:3.46.1.0",
    ],
    duplicate_version_warning = "error",
    fail_if_repin_required = True,
    lock_file = "//:maven_install.json",
    repositories = [
        "https://repo1.maven.org/maven2",
    ],
)

# Force specific versions for dependencies with conflicts between grpc-java and protobuf
maven.artifact(
    artifact = "gson",
    force_version = True,
    group = "com.google.code.gson",
    version = "2.11.0",
)
maven.artifact(
    artifact = "error_prone_annotations",
    force_version = True,
    group = "com.google.errorprone",
    version = "2.30.0",
)
maven.artifact(
    artifact = "guava",
    force_version = True,
    group = "com.google.guava",
    version = "33.3.1-android",
)

use_repo(maven, "maven", "unpinned_maven")

#
# External Libraries
#

http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_file = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")

# GTL (for parallel_hashmap)
GTL_VERSION = "1.2.0"

GTL_SHA = "1969c45dd76eac0dd87e9e2b65cffe358617f4fe1bcd203f72f427742537913a"

http_archive(
    name = "gtl",
    build_file_content = """
cc_library(
    name = "gtl",
    hdrs = glob(["include/gtl/*.hpp"]),
    includes = ["include"],
    visibility = ["//visibility:public"],
)
""",
    sha256 = GTL_SHA,
    strip_prefix = "gtl-%s" % GTL_VERSION,
    url = "https://github.com/greg7mdp/gtl/archive/refs/tags/v%s.zip" % GTL_VERSION,
)

# Unity GoDice Plugin
UNITY_GODICE_COMMIT = "18d6823991592e4d45fcc0f22692db849dea9063"

UNITY_GODICE_SHA = "04e6ae4155965aab3372592e04061eba1256bb6ea7ccffd0d83f27574e5b3349"

http_archive(
    name = "net_eagle0_unity_godice",
    sha256 = UNITY_GODICE_SHA,
    strip_prefix = "godice-framework-%s" % UNITY_GODICE_COMMIT,
    urls = [
        "https://github.com/nolen777/godice-framework/archive/%s.zip" % UNITY_GODICE_COMMIT,
    ],
)

# Sparkle framework for macOS auto-updates
SPARKLE_VERSION = "2.6.4"

http_archive(
    name = "sparkle",
    build_file = "//src/main/objc/net/eagle0/clients/unity/sparkle/external:BUILD.sparkle",
    sha256 = "50612a06038abc931f16011d7903b8326a362c1074dabccb718404ce8e585f0b",
    strip_prefix = "",
    url = "https://github.com/sparkle-project/Sparkle/releases/download/%s/Sparkle-%s.tar.xz" % (SPARKLE_VERSION, SPARKLE_VERSION),
)

# Busybox static binary for Docker health checks (provides nc, wget, etc.)
# Primary: DigitalOcean Spaces (public, reliable)
# Fallback: busybox.net (can be unreliable/slow)
http_file(
    name = "busybox_x86_64",
    sha256 = "6e123e7f3202a8c1e9b1f94d8941580a25135382b99e8d3e34fb858bba311348",
    urls = [
        "https://eagle0-sysroot.sfo3.digitaloceanspaces.com/busybox/busybox-1.35.0-x86_64-linux-musl",
        "https://busybox.net/downloads/binaries/1.35.0-x86_64-linux-musl/busybox",
    ],
    downloaded_file_path = "busybox",
    executable = True,
)

http_file(
    name = "busybox_aarch64",
    sha256 = "141adb1b625a6f44c4b114f76b4387b4ea4f7ab802b88eb40e0d2f6adcccb1c3",
    urls = [
        # TODO: Upload aarch64 binary to GitHub release when needed
        "https://busybox.net/downloads/binaries/1.35.0-aarch64-linux-musl/busybox",
    ],
    downloaded_file_path = "busybox",
    executable = True,
)

# LLVM MinGW toolchain for Windows cross-compilation from macOS
# This provides a complete toolchain for building Windows executables including
# the MinGW-w64 libraries needed for CGO cross-compilation
LLVM_MINGW_VERSION = "20250305"

http_archive(
    name = "llvm_mingw",
    build_file_content = """
package(default_visibility = ["//visibility:public"])

filegroup(
    name = "all_files",
    srcs = glob(["**/*"]),
)

filegroup(
    name = "compiler_files",
    srcs = glob([
        "bin/x86_64-w64-mingw32-*",
        "bin/clang*",
        "bin/llvm-*",
        "bin/lld*",
    ]),
)

filegroup(
    name = "windows_x86_64_sysroot",
    srcs = glob([
        "x86_64-w64-mingw32/**/*",
        "generic-w64-mingw32/include/**/*",
    ]),
)

filegroup(
    name = "linker_files",
    srcs = glob([
        "bin/x86_64-w64-mingw32-*",
        "bin/lld*",
        "bin/ld.lld*",
        "lib/**/*",
        "x86_64-w64-mingw32/lib/**/*",
    ]),
)

exports_files([
    "bin/x86_64-w64-mingw32-clang",
    "bin/x86_64-w64-mingw32-clang++",
])
""",
    sha256 = "32c24fc62fc8b9f8a900bf2c730b78b36767688f816f9d21e97a168289ff44e0",
    strip_prefix = "llvm-mingw-%s-ucrt-macos-14.4.1-universal" % LLVM_MINGW_VERSION,
    urls = ["https://github.com/fathonix/llvm-mingw-arm64ec-macos/releases/download/%s/llvm-mingw-%s-ucrt-macos-14.4.1-universal.tar.xz" % (LLVM_MINGW_VERSION, LLVM_MINGW_VERSION)],
)

#
# Toolchain Registration
#

register_toolchains(
    "//tools:unused_dependency_checker_error_and_opts_toolchain",
    "@rules_scala//testing:scalatest_toolchain",
)

# Apple CC toolchain for Objective-C compilation (SparklePlugin).
# Registered before LLVM so it has higher priority; Apple constraint matching
# ensures it's only used for Apple-platform targets (objc_library, macos_bundle).
register_toolchains("@local_config_apple_cc_toolchains//:all")

# Set dev_dependency so we can turn this off for swift MacOS builds
register_toolchains(
    "@llvm_toolchain//:all",
    "@llvm_toolchain_linux//:all",
    "@llvm_toolchain_linux_arm64//:all",
    dev_dependency = True,
)
