# SPDX-FileCopyrightText: (c) TagStudio Contributors
# SPDX-License-Identifier: GPL-3.0-only

{
  buildPythonPackage,
  cmake,
  fetchPypi,
  lib,
  numpy,
  packaging,
  pillow,
  pyexiv2,
  pytestCheckHook,
  rustPlatform,
}:

buildPythonPackage rec {
  pname = "pillow-jxl-plugin";
  version = "1.3.4";
  pyproject = true;

  src = fetchPypi {
    pname = "pillow_jxl_plugin";
    inherit version;
    hash = "sha256-jqWJ/FWep8XfzLQq9NgUj121CPX01FGDKLq1ox/LJo4=";
  };

  cargoDeps = rustPlatform.fetchCargoVendor {
    inherit src;
    hash = "sha256-7j+sCn+P6q6tsm2MJ/cM7hF2KEjILJNA6SDb35tecPg=";
  };

  nativeBuildInputs = [
    cmake
    rustPlatform.cargoSetupHook
    rustPlatform.maturinBuildHook
  ];

  nativeCheckInputs = [
    numpy
    pyexiv2
    pytestCheckHook
  ];

  # Working directory takes precedence in the Python path. Remove
  # `pillow_jxl` to prevent it from being loaded during pytest, rather than the
  # built module, as it includes a `pillow_jxl.pillow_jxl.so` that is imported.
  # See: https://github.com/NixOS/nixpkgs/issues/255262
  # See: https://github.com/NixOS/nixpkgs/pull/255471
  preCheck = ''
    rm -r pillow_jxl
  '';

  dontUseCmakeConfigure = true;

  pythonImportsCheck = [ "pillow_jxl" ];

  dependencies = [
    packaging
    pillow
  ];

  meta = {
    description = "Pillow plugin for JPEG-XL, using Rust for bindings";
    homepage = "https://github.com/Isotr0py/pillow-jpegxl-plugin";
    changelog = "https://github.com/Isotr0py/pillow-jpegxl-plugin/releases/tag/v${version}";
    license = lib.licenses.gpl3;
    maintainers = with lib.maintainers; [ xarvex ];
    platforms = lib.platforms.unix;
  };
}
