serviceName: {
  config,
  lib,
  ...
}:
with lib; let
  cfg = config.homelab.services.${serviceName};
  homelabCfg = config.homelab;

  hasMetricsConfig =
    cfg.monitoring.metrics.path
    != null
    || cfg.monitoring.metrics.extraEndpoints != [];

  hasHealthCheckConfig =
    cfg.monitoring.healthCheck.path
    != null
    || cfg.monitoring.healthCheck.conditions != []
    || cfg.monitoring.healthCheck.extraChecks != [];
in {
  # Define the service-specific monitoring options
  options.homelab.services.${serviceName}.monitoring = {
    enable = mkOption {
      type = types.bool;
      description = "Enable monitoring for ${serviceName}";
      default = hasMetricsConfig || hasHealthCheckConfig;
    };

    metrics = {
      enable = mkOption {
        type = types.bool;
        default = hasMetricsConfig;
      };

      path = mkOption {
        type = types.nullOr types.str;
        default = null;
        description = "Metrics endpoint path. Setting this enables metrics collection.";
      };

      extraEndpoints = mkOption {
        type = types.listOf types.attrs;
        default = [];
        description = "Additional metrics endpoints. Adding endpoints enables metrics collection.";
      };
    };

    healthCheck = {
      enable = mkOption {
        type = types.bool;
        default = hasHealthCheckConfig;
      };

      path = mkOption {
        type = types.nullOr types.str;
        default = null;
        description = "Health check endpoint path. Setting this enables health checks.";
        example = "/health";
      };

      conditions = mkOption {
        type = types.listOf types.str;
        default = ["[STATUS] == 200"];
        description = "Health check conditions. Setting conditions enables health checks.";
        example = ["[STATUS] == 200"];
      };

      extraChecks = mkOption {
        type = types.listOf types.attrs;
        default = [];
        description = "Additional health checks. Adding checks enables health monitoring.";
      };
    };

    extraLabels = mkOption {
      type = types.attrsOf types.str;
      default = {};
    };
  };

  # Generate the homelab config automatically when service is enabled
  config = mkIf cfg.enable {
    homelab.monitoring = mkIf cfg.monitoring.enable {
      metrics = mkIf hasMetricsConfig (
        (optional (cfg.monitoring.metrics.path != null) {
          name = "${serviceName}-main";
          host = homelabCfg.hostname;
          port = cfg.port;
          path = cfg.monitoring.metrics.path;
          jobName = serviceName;
          scrapeInterval = "30s";
          labels =
            cfg.monitoring.extraLabels
            // {
              service = serviceName;
              node = homelabCfg.hostname;
              environment = homelabCfg.environment;
            };
        })
        ++ cfg.monitoring.metrics.extraEndpoints
      );

      healthChecks = mkIf hasHealthCheckConfig (
        (optional (cfg.monitoring.healthCheck.path != null) {
          name = "${serviceName}-health";
          host = homelabCfg.hostname;
          port = cfg.port;
          path = cfg.monitoring.healthCheck.path;
          protocol = "http";
          method = "GET";
          interval = "30s";
          timeout = "10s";
          conditions = cfg.monitoring.healthCheck.conditions;
          group = "services";
          labels =
            cfg.monitoring.extraLabels
            // {
              service = serviceName;
              node = homelabCfg.hostname;
              environment = homelabCfg.environment;
            };
          enabled = true;
        })
        ++ cfg.monitoring.healthCheck.extraChecks
      );
    };
  };
}
