{ 
  rustPlatform,
  system,
  python312,
  stdenv,
  python3Packages,
  fetchPypi,
  lib,
  fetchFromGitHub,
  python312Packages,
  makeWrapper,
  nodejs_22,
  nodePackages,
  buildNpmPackage,
}: let
  python = python312.withPackages (ps: with ps; [
    APScheduler
    deprecated
    jinja2
    pyjwt
    pyyaml
    alembic
    anyio
    bcrypt
    click
    commentjson
    cryptography
    fastapi
    jdatetime
    passlib
    psutil
    pyopenssl
    pydantic
    python-dateutil
    python-decouple
    python-dotenv
    python-multipart
    qrcode
    rich
    uvicorn
    websocket-client
    websockets
    sse-starlette
    (python3Packages.buildPythonPackage rec {
      pname = "gozargah_node_bridge";
      version = "0.0.39";
    
      format = "pyproject";
    
      src = python3Packages.fetchPypi {
        inherit pname version;
        sha256 = "sha256-NmoKepaD+Sutzr86Cx6hWsDtdKXreWNlQFQI4fFI70w=";
      };
    
      nativeBuildInputs = with python3Packages; [ 
        flit-core
        hatchling
        aiorwlock        
        httpx
        protobuf
        grpcio-tools
        grpclib
      ];
    
      doCheck = false;
    })
    httpx
    packaging
    sqlalchemy
    aiosqlite
    asyncpg
    asyncmy
    aiocache
    textual
    aiogram
    aiohttp-socks
    hatchling
    aiorwlock        
    protobuf
    grpcio-tools
    grpclib
  ]);

  marzbanRepo = fetchFromGitHub {
    owner = "gozargah";
    repo = "marzban";
    rev = "ef47e0b4b185d5e75b5e456d78266a9e1fdc2499";
    hash = "sha256-obapQyFfxO9CpvBzz+b98/vb3GltQ4yfRapnMfZOCDk=";
  };

  dashboardUi = buildNpmPackage {
    pname = "marzban-ui";
    version = "0.0.0";
  
    src = builtins.path { 
      path = ./src/marzban-src.tar.gz; 
      name = "marzban-src.tar.gz"; 
    };

    sourceRoot = "Marzban/dashboard";

    npmDepsHash = "sha256-/GrOv/iiVs5FCJoYKnKoiJmZay50XI4cGv32QXdAv/E=";

    npmFlags = [ "--legacy-peer-deps" ];
  
    npmBuild = "vite build --outDir build --assetsDir statics";
  
    installPhase = ''
      mkdir -p $out
      cp -r build/* $out/
    '';
  };
in
python3Packages.buildPythonApplication rec {
  pname = "marzban";
  version = "v0.8.4";

  src = marzbanRepo;

  installPhase = /* sh */ ''
    mkdir -p $out/lib/marzban
    mkdir -p $out/lib/marzban/dashboard/build
    cp -r . $out/lib/marzban/
    cp -r ${dashboardUi}/* $out/lib/marzban/dashboard/build/

    makeWrapper ${python}/bin/python $out/bin/marzban-cli \
      --prefix PYTHONPATH : $out/lib/marzban \
      --add-flags "$out/lib/marzban/marzban-cli.py"

    makeWrapper ${python}/bin/python $out/bin/marzban \
      --prefix PYTHONPATH : $out/lib/marzban \
      --add-flags "$out/lib/marzban/main.py"

    makeWrapper ${python}/bin/python $out/bin/python \
      --prefix PYTHONPATH : $out/lib/marzban
  '';

  nativeBuildInputs = [ 
    makeWrapper
    nodejs_22
  ];

  postInstall = ''
    # Generate bash completion and install it
    mkdir -p $out/share/bash-completion/completions
    $out/bin/marzban-cli completion bash > $out/share/bash-completion/completions/marzban-cli
  '';

  format = "other";
}
