{ config, pkgs, lib, ... }: let 
  cfg = config.services.plex-exporter; 
in {
  options.services.plex-exporter = {
    enable = lib.mkEnableOption "enables plex exporter";

    configFile = lib.mkOption {
      type = lib.types.path;
      default = "http://localhost:32400";
    };

    listenAddress = lib.mkOption {
      type = lib.types.str;
      default = ":9594";
    };
  };

  config = lib.mkIf cfg.enable {
    systemd.services.plex-exporter = {
      enable = true;
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      description = "Plex exporter for Prometheus";
      serviceConfig = {
        Type = "simple";
        User = "nobody";
        Group = "nobody";
        Restart = "always";
        ExecStart = "${pkgs.local-pkgs.plex-exporter}/bin/plex_exporter --listen-address ${cfg.listenAddress} --config-path ${cfg.configFile}";
        RuntimeDirectory = "plex-exporter";
      };
    };
  };
}
