{ lib
, fetchFromGitHub
, rustPlatform
, pkg-config
, wrapGAppsHook3
, webkitgtk_4_1
, gtk3
, glib
, cairo
, gdk-pixbuf
, pango
, atk
, libsoup_3
, librsvg
, dbus
, openssl
, at-spi2-atk
, at-spi2-core
, glib-networking
, gsettings-desktop-schemas
, wasm-bindgen-cli
, trunk
, makeWrapper
}:

let
  pname = "pinepods";
  version = "0.8.2";

  src = fetchFromGitHub {
    owner = "madeofpendletonwool";
    repo = "PinePods";
    rev = version;
    sha256 = "1ivqazhzg3wwlqzz0dv0g48hv265c3qqhvxl02j20361j4mqn6rp";
  };

  # ── Step 1: Build the Yew/WASM frontend with trunk ──────────────────────────
  frontend = rustPlatform.buildRustPackage {
    pname = "pinepods-frontend";
    inherit version src;

    sourceRoot = "source/web";
    cargoRoot = ".";

    postUnpack = ''
      cp ${builtins.toString ./.}/Cargo.frontend.lock $sourceRoot/Cargo.lock
    '';

    cargoLock = {
      lockFile = "${builtins.toString ./.}/Cargo.frontend.lock";
      outputHashes = {
        "i18nrs-0.1.7" = "sha256-LEHQ8GI3eVKwhtxetxyIY4HV16TS3D9n+2Hh+sm5j74=";
      };
    };

    nativeBuildInputs = [ trunk wasm-bindgen-cli ];

    buildPhase = ''
      cp -r . $NIX_BUILD_TOP/web-build
      chmod -R u+w $NIX_BUILD_TOP/web-build
      cd $NIX_BUILD_TOP/web-build
      mkdir -p dist
      RUSTFLAGS="--cfg=web_sys_unstable_apis" trunk build --release
    '';

    installPhase = ''
      cp -r $NIX_BUILD_TOP/web-build/dist $out
    '';

    doCheck = false;
  };

in
# ── Step 2: Build the Tauri binary, pointing it at the pre-built frontend ────
rustPlatform.buildRustPackage {
  inherit pname version src;

  sourceRoot = "source/web/src-tauri";
  cargoRoot = ".";

  postUnpack = ''
      cp ${builtins.toString ./.}/Cargo.lock $sourceRoot/Cargo.lock
    '';

  cargoLock = {
    lockFile = "${builtins.toString ./.}/Cargo.lock";
  };

  nativeBuildInputs = [
    pkg-config
    wrapGAppsHook3
    makeWrapper
  ];

  buildInputs = [
    openssl
    dbus
    glib
    gtk3
    cairo
    gdk-pixbuf
    pango
    atk
    at-spi2-atk
    at-spi2-core
    libsoup_3
    webkitgtk_4_1
    librsvg
    glib-networking
    gsettings-desktop-schemas
  ];

  OPENSSL_NO_VENDOR = "1";
  TAURI_SKIP_DEVSERVER_CHECK = "true";

  preBuild = ''
    # Link the pre-built frontend dist where tauri.conf.json expects it (../ from src-tauri)
    ln -s ${frontend} $NIX_BUILD_TOP/source/web/dist
  '';

  installPhase = ''
    runHook preInstall

    install -Dm755 target/release/pinepods $out/bin/pinepods

    if [ -f ../../pinepods.desktop ]; then
      install -Dm644 ../../pinepods.desktop $out/share/applications/pinepods.desktop
    fi

    for size in 32x32 64x64 128x128 256x256; do
      if [ -f icons/''${size}.png ]; then
        install -Dm644 icons/''${size}.png \
          $out/share/icons/hicolor/''${size}/apps/pinepods.png
      fi
    done

    runHook postInstall
  '';

  postFixup = ''
    wrapProgram $out/bin/pinepods \
      --set WEBKIT_DISABLE_COMPOSITING_MODE 1 \
      --prefix XDG_DATA_DIRS : "${gsettings-desktop-schemas}/share/gsettings-schemas/${gsettings-desktop-schemas.name}:${gtk3}/share/gsettings-schemas/${gtk3.name}" \
      --prefix GIO_EXTRA_MODULES : "${glib-networking}/lib/gio/modules"
  '';

  doCheck = false;

  meta = with lib; {
    description = "PinePods desktop client - self-hosted podcast manager (Tauri v2)";
    homepage    = "https://github.com/madeofpendletonwool/PinePods";
    license     = licenses.gpl3Only;
    platforms   = platforms.linux;
    mainProgram = "pinepods";
  };
}