load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@rules_python//python:defs.bzl", "py_library")
load("@py_deps//:requirements.bzl", "requirement")
load("@rules_python//python:packaging.bzl", "py_package", "py_wheel")
load("//:defs.bzl", "anki_version")
load("//pylib:orjson.bzl", "orjson_if_available")
load("//pylib:protobuf.bzl", "py_proto")

copy_file(
    name = "buildinfo",
    src = "//:buildinfo.txt",
    out = "buildinfo.txt",
)

genrule(
    name = "hooks_gen",
    outs = ["hooks_gen.py"],
    cmd = "$(location //pylib/tools:genhooks) $@",
    tools = ["//pylib/tools:genhooks"],
)

_py_srcs = glob(
    ["**/*.py"],
    exclude = [
        "hooks_gen.py",
    ],
) + ["//pylib/anki/_vendor"]

py_library(
    name = "anki",
    srcs = _py_srcs,
    data = [
        "py.typed",
        ":buildinfo",
        ":hooks_gen",
        "//pylib/anki/_backend",
    ],
    imports = [
        "..",
    ],
    visibility = ["//visibility:public"],
    deps = [
        ":proto",
        requirement("beautifulsoup4"),
        requirement("decorator"),
        requirement("distro"),
        requirement("protobuf"),
        requirement("requests"),
        requirement("flask"),
        requirement("waitress"),
        requirement("markdown"),
    ] + orjson_if_available(),
)

py_package(
    name = "anki_pkg",
    packages = ["pylib.anki"],
    deps = [":anki"],
)

py_wheel(
    name = "wheel",
    abi = "abi3",
    description_file = "wheel_description.txt",
    distribution = "anki",
    extra_requires = {
        "syncserver": [
            "flask",
            "waitress",
        ],
    },
    platform = select({
        "//platforms:windows_x86_64": "win_amd64",
        "//platforms:macos_x86_64": "macosx_10_13_x86_64",
        "//platforms:macos_arm64": "macosx_11_0_arm64",
        # the glibc versions below are set for the official builds;
        # you can adjust them to suit your enviroment
        "//platforms:linux_x86_64": "manylinux_2_28_x86_64",
        "//platforms:linux_arm64": "manylinux_2_31_aarch64",
    }),
    python_tag = "cp39",
    requires = [
        "beautifulsoup4",
        "requests[socks]",
        "decorator",
        "protobuf>=3.17",
        "markdown",
        "orjson",
        'psutil; sys_platform == "win32"',
        'distro; sys_platform != "darwin" and sys_platform != "win32"',
    ],
    strip_path_prefixes = [
        "pylib/",
    ],
    tags = ["manual"],
    version = anki_version,
    visibility = ["//visibility:public"],
    deps = [":anki_pkg"],
)

filegroup(
    name = "py_source_files",
    srcs = _py_srcs + [
        "//pylib/anki/_backend:py_source_files",
    ],
    visibility = [
        "//pylib:__subpackages__",
    ],
)

py_proto(
    name = "proto_py",
    srcs = ["//proto"],
    visibility = [
        "//visibility:public",
    ],
)

py_library(
    name = "proto",
    srcs = [
        "__init__.py",
        # includes the .py files
        ":proto_py",
    ],
    # includes the .pyi files
    data = [
        "py.typed",
        ":proto_py",
    ],
    imports = [".."],
    visibility = ["//visibility:public"],
)
