{ lib, pkgs, config, ... }:

let
  cfg = config.local.octoprint;
in
{
  options.local.octoprint = with lib; {
    enable = mkEnableOption "octoprint";
  };

  config = lib.mkIf cfg.enable {
    services.octoprint = {
      enable = true;
      port = 33002;
      plugins = ps:
        let
          octoprintDisplayLayerProgress = ps.buildPlugin rec {
            pname = "DisplayLayerProgress";
            version = "1.28.0";

            src = pkgs.fetchFromGitHub {
              owner = "OllisGit";
              repo = "OctoPrint-DisplayLayerProgress";
              rev = version;
              sha256 = "sha256-FoQGv7a3ktodyQKOwR69/9Up+wPoW5NDq+k5LfP9WYs=";
            };
          };
          octoprintDashboard = ps.buildPlugin rec {
            pname = "Dashboard";
            version = "1.19.10";

            src = pkgs.fetchFromGitHub {
              owner = "j7126";
              repo = "OctoPrint-Dashboard";
              rev = version;
              sha256 = "sha256-d3HtSgJ9d6k7+F86aDuhq31jWZHdNxncFcK6MYPgUKo=";
            };
          };
          octoprintBedLevelVisualizer = ps.buildPlugin rec {
            pname = "BedLevelVisualizer";
            version = "1.1.1";

            src = pkgs.fetchFromGitHub {
              owner = "jneilliii";
              repo = "OctoPrint-BedLevelVisualizer";
              rev = version;
              sha256 = "sha256-6JcYvYgEmphp5zz4xZi4G0yTo4FCIR6Yh+MXYK7H7+w=";
            };
          };
        in
        [
          octoprintDisplayLayerProgress
          octoprintDashboard
          octoprintBedLevelVisualizer
        ];
    };
  };
}
