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

with lib;

let cfg = config.services.zugferd-service;
in {
  options.services.zugferd-service = {
    enable = mkEnableOption "ZUGFeRD REST API service";

    port = mkOption {
      type = types.port;
      default = 5000;
      description = "Port to listen on";
    };

    host = mkOption {
      type = types.str;
      default = "127.0.0.1";
      description = "Host to bind to";
    };

    package = mkOption {
      type = types.package;
      default = pkgs.zugferd-service;
      description = "Package to use for the service";
    };
  };

  config = mkIf cfg.enable {
    systemd.services.zugferd-service = {
      description = "ZUGFeRD REST API Service";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];

      serviceConfig = {
        Type = "simple";
        ExecStart =
          "${cfg.package}/bin/zugferd-service --host ${cfg.host} --port ${
            toString cfg.port
          }";
        Restart = "on-failure";
        DynamicUser = true;
        NoNewPrivileges = true;
        ProtectSystem = "strict";
        ProtectHome = true;
      };
    };
  };
}
