# Copyright (c) Meta Platforms, Inc. and affiliates.

load("@bazel_skylib//lib:shell.bzl", "shell")
load("@fbcode_macros//build_defs:native_rules.bzl", "buck_filegroup", "buck_genrule", "buck_sh_test")
load("@fbcode_macros//build_defs:python_binary.bzl", "python_binary")
load("@fbsource//tools/target_determinator/macros:ci.bzl", "ci")

oncall("data_compression")

BRANCH_NAME = read_package_value("directory_branching.branch_name")  # Gets "dev" or "prod" from the path

python_binary(
    name = "static_docs_build_script",
    srcs = ["static_docs_build_script.py"],
    base_module = "openzl.doc.mkdocs",
    main_module = "openzl.doc.mkdocs.static_docs_build_script",
    par_style = "xar",
    resources = {
        ":mkdocs": "mkdocs",
    },
)

buck_filegroup(
    name = "doc_srcs",
    srcs = glob(["doc/**/*"]),
)

buck_genrule(
    name = "static_docs",
    out = "static_docs.sh",
    cmd = (
        "echo -e {0} > $OUT && chmod +x $OUT".format(
            shell.quote(
                "\n".join([
                    "#!/bin/bash",
                    "SCRIPT=\\$(readlink -f $0)",
                    "SCRIPTPATH=\\$(dirname $SCRIPT)",
                    "cd $SCRIPTPATH",
                    "$(location :static_docs_build_script) --pwd \\$(hg root)/fbcode/openzl/{}/doc/mkdocs --use-system-python-extension --test".format(
                        BRANCH_NAME,
                    ),
                ]),
            ),
        )
    ),
)

# Test the internal website builds successfully
buck_sh_test(
    name = "static_docs_test",
    env = {
        "DOXYGEN_PATH": "$(location fbsource//third-party/doxygen:doxygen)",
        # Skip building web tools in tests since they require external network access
        # to fetch npm packages, which is not available in remote execution.
        "OPENZL_SKIP_WEB_TOOLS_BUILD": "1",
    },
    labels = ci.labels(ci.remove(ci.aarch64())),
    test = ":static_docs",
    deps = [
        ":static_docs_build_script",
        # Depend on all files under doc/ to ensure the test triggers on changes to the docs
        ":doc_srcs",
        # Inject fake dependencies to ensure the test triggers on changes to the library and web tools.
        # When adding a new web tool, add its :app_srcs target here so doc tests run on changes.
        "../..:zstronglib",  # @manual
        # Shared workspace manifests and packages are build inputs for every tool,
        # so changing them must reschedule this test too.
        "../../tools/web:web_workspace_srcs",  # @manual
        "../../tools/web/compression_playground:app_srcs",  # @manual
        "../../tools/web/visualization_app:app_srcs",  # @manual
        "../../tools/web/web_common:package_srcs",  # @manual
        # Doxygen binary must be explicitly declared as a dependency to be materialized in remote execution
        "fbsource//third-party/doxygen:doxygen",  # @manual
    ],
)

python_binary(
    # @autodeps-skip
    name = "mkdocs",
    main_function = "mkdocs.__main__.cli",
    par_style = "xar",
    deps = [
        "../../py:openzl",  # @manual
        "fbsource//third-party/pypi/mkdocs:mkdocs",
        "fbsource//third-party/pypi/mkdocs-material:mkdocs-material",  # @manual
        "fbsource//third-party/pypi/mkdocstrings:mkdocstrings",  # @manual
        "fbsource//third-party/pypi/mkdocstrings-python:mkdocstrings-python",  # @manual
        "mkdocs-openzl/src:mkdocs_openzl",  # @manual
        "mkdocstrings-zstd/src:mkdocstrings-zstd",  # @manual
    ],
)

python_binary(
    name = "import_mkdocstrings_zstd",
    srcs = ["import_mkdocstrings_zstd.py"],
    base_module = "openzl.doc.mkdocs",
    labels = ["autodeps2_generated"],
    main_module = "openzl.doc.mkdocs.import_mkdocstrings_zstd",
)
