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")

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

genrule(
    name = "extract_sass_colors",
    srcs = [
        "//sass:_vars.scss",
    ],
    outs = ["colors.py"],
    cmd = "$(location //qt:extract_sass_colors) $< $@",
    tools = [
        "//qt:extract_sass_colors",
    ],
)

_py_srcs = glob(
    [
        "**/*.py",
    ],
    exclude = ["hooks_gen.py"],
)

_py_srcs_and_forms = _py_srcs + [
    "//qt/aqt/forms:forms",
]

aqt_core_data = [
    "colors.py",
    "py.typed",
    ":hooks_gen",
]

aqt_deps = [
    requirement("protobuf"),
    requirement("decorator"),
    requirement("requests"),
    requirement("beautifulsoup4"),
    requirement("flask"),
    requirement("flask-cors"),
    requirement("waitress"),
    requirement("send2trash"),
    requirement("jsonschema"),
] + select({
    "@bazel_tools//src/conditions:host_windows": [
        requirement("psutil"),
        requirement("pywin32"),
        requirement("winrt"),
    ],
    "//conditions:default": [],
})

# library without web/i18n data; faster for testing
py_library(
    name = "aqt_without_data",
    srcs = _py_srcs_and_forms,
    data = aqt_core_data,
    visibility = ["//visibility:public"],
    deps = aqt_deps + [
        "@pyqt6",
    ],
)

py_library(
    name = "aqt_with_data",
    srcs = _py_srcs_and_forms,
    data = aqt_core_data + ["//qt/aqt/data"],
    visibility = ["//visibility:public"],
    deps = aqt_deps,
)

py_package(
    name = "aqt_pkg",
    packages = [
        "qt.aqt",
    ],
    deps = [
        ":aqt_with_data",
    ],
)

py_wheel(
    name = "wheel",
    description_file = "wheel_description.txt",
    distribution = "aqt",
    entry_points = {
        "console_scripts": ["anki = aqt:run"],
    },
    extra_requires = {
        "qt5": [
            "pyqt5>=5.14",
            "pyqtwebengine",
        ],
        "qt6": [
            "pyqt6>=6.2",
            "pyqt6-webengine>=6.2",
        ],
    },
    homepage = "https://apps.ankiweb.net",
    license = "License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)",
    python_tag = "py3",
    requires = [
        "beautifulsoup4",
        "requests",
        "send2trash",
        "jsonschema",
        "flask",
        "flask_cors",
        "waitress",
        'psutil; sys.platform == "win32"',
        'pywin32; sys.platform == "win32"',
        'winrt; sys.platform == "win32"',
        "anki==" + anki_version,
    ],
    strip_path_prefixes = [
        "qt/",
    ],
    tags = ["manual"],
    version = anki_version,
    visibility = ["//visibility:public"],
    deps = [
        ":aqt_pkg",
    ],
)

filegroup(
    name = "py_source_files",
    srcs = _py_srcs,
    visibility = [
        "//qt:__subpackages__",
    ],
)
