{ config, lib, depot, ... }:

let
  inherit (lib) mkEnableOption mkOption mkIf types;
  cfg = config.services.rundeck;
in {
  options.services.rundeck = {
    enable = mkEnableOption "rundeck";

    user = mkOption {
      default = null;
      type = types.nullOr types.str;
    };

    package = mkOption {
      default = depot.pkgs.rundeck-bin;
      defaultText = "depot.pkgs.rundeck-bin";
      type = types.package;
    };

    pathPackages = mkOption {
      default = [];
      defaultText = "[]";
      type = types.listOf types.package;
    };
  };

  config = mkIf cfg.enable {
    systemd.services.rundeckd = {
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];
      path = cfg.pathPackages;
      serviceConfig = {
        User = cfg.user;
        DynamicUser = cfg.user != cfg.user;
        ExecStart = "${cfg.package}/bin/rundeckd -b /var/lib/rundeck";
        StateDirectory = "rundeck";
      };
    };
  };
}