# Example configuration showing how to use the monitoring stack
# with the homelab.global approach for dynamic discovery
{
  config,
  pkgs,
  ...
}: {
  # Import the monitoring services
  imports = [
    ./services/prometheus.nix
    ./services/alertmanager.nix
    ./services/grafana.nix
    ./services/monitoring-stack.nix
  ];

  # Enable the full monitoring stack
  homelab.services.monitoring-stack.enable = true;

  # Configure Prometheus - it will automatically discover scrape targets
  # from homelab.global.monitoring.allMetrics
  homelab.services.prometheus = {
    enable = true;
    port = 9090;
    retention = "7d";

    # Optional: Add custom scrape configs if needed
    extraScrapeConfigs = [
      # Any additional manual scrape configs can go here
      # but most should be discovered via homelab.monitoring.metrics
    ];

    # Optional: Add custom alerting rules
    extraAlertingRules = [
      # Custom alert groups can be added here
    ];

    # Optional: Add external rule files
    ruleFiles = [
      # ./path/to/custom-rules.yml
    ];
  };

  # Configure Alertmanager with Telegram support (like your original)
  homelab.services.alertmanager = {
    enable = true;
    port = 9093;

    # Use sops secrets for environment variables
    environmentFile = config.sops.secrets."alertmanager/env".path;

    # Enable Telegram notifications
    telegram = {
      enable = true;
      botToken = "$TELEGRAM_BOT_TOKEN"; # From environment file
      chatId = -1002642560007;
      messageThreadId = 4;
    };

    # Custom templates (similar to your setup)
    templates = [
      (pkgs.writeText "telegram.tmpl" ''
        {{- define "telegram.message" -}}
        {{- if gt (len .Alerts.Firing) 0 -}}
        🔥 <b>FIRING</b> 🔥
        {{- range .Alerts.Firing }}
        <b>{{ .Annotations.summary }}</b>
        {{ .Annotations.description }}
        {{- end }}
        {{- end }}
        {{- if gt (len .Alerts.Resolved) 0 -}}
        ✅ <b>RESOLVED</b> ✅
        {{- range .Alerts.Resolved }}
        <b>{{ .Annotations.summary }}</b>
        {{- end }}
        {{- end }}
        {{- end -}}
      '')
    ];
  };

  # Configure Grafana with data sources (similar to your setup)
  homelab.services.grafana = {
    enable = true;
    port = 3000;
    domain = "grafana.procopius.dk";
    rootUrl = "https://grafana.procopius.dk";

    # Add grafana user to influxdb2 group for accessing secrets
    extraGroups = ["influxdb2"];

    # Enable data sources
    datasources = {
      prometheus.enable = true;
      loki.enable = true;
      influxdb = {
        enable = true;
        database = "proxmox";
        tokenPath = config.sops.secrets."influxdb/token".path;
      };
    };

    # Provision dashboards (similar to your environment.etc approach)
    dashboards.files = [
      {
        name = "traefik";
        source = ./dashboards/traefik.json;
      }
      {
        name = "traefik-access";
        source = ./dashboards/traefik-access.json;
      }
      {
        name = "grafana-traefik";
        source = ./dashboards/grafana-traefik.json;
      }
      {
        name = "node-exporter";
        source = ./dashboards/node-exporter.json;
      }
      {
        name = "promtail";
        source = ./dashboards/promtail.json;
      }
      {
        name = "gitea";
        source = ./dashboards/gitea.json;
      }
      {
        name = "postgres";
        source = ./dashboards/postgres.json;
      }
      {
        name = "gatus";
        source = ./dashboards/gatus.json;
      }
    ];
  };

  # Configure sops secrets (keep your existing setup)
  sops.secrets."alertmanager/env" = {
    sopsFile = ../../secrets/secrets.yaml;
    mode = "0440";
  };

  # All services automatically register with homelab.monitoring.metrics
  # and homelab.monitoring.healthChecks for Gatus monitoring
  # All services automatically get reverse proxy entries
}
