# Infrastructure - Prometheus Schema
# Defines type-safe Prometheus configuration generation
# Validates scrape configs, alert rules, and recording rules

{
  # Scrape target configuration
  ScrapeTarget = {
    address | String,
    labels | {_ | String} | default = {},
  },

  # Scrape job configuration
  ScrapeConfig = {
    job_name | String,
    scrape_interval | String | default = "15s",
    scrape_timeout | String | default = "10s",
    metrics_path | String | default = "/metrics",
    static_configs | Array {
      targets | Array String,
      labels | {_ | String} | default = {},
    },
    honor_labels | Bool | default = false,
    honor_timestamps | Bool | default = true,
    scheme | [| 'http, 'https |] | default = 'http,
  },

  # Alert manager configuration
  AlertManagerConfig = {
    static_configs | Array {
      targets | Array String,
      labels | {_ | String} | default = {},
    } | default = [],
  },

  # Global Prometheus configuration
  GlobalConfig = {
    scrape_interval | String | default = "15s",
    evaluation_interval | String | default = "15s",
    external_labels | {_ | String} | default = {
      environment = "development",
    },
  },

  # Remote write configuration
  RemoteWrite = {
    url | String,
    write_relabel_configs | Array {
      source_labels | Array String | optional,
      target_label | String | optional,
      action | [| 'replace, 'keep, 'drop |] | default = 'replace,
    } | default = [],
  },

  # Complete Prometheus configuration
  PrometheusConfig = {
    global | GlobalConfig | default = {
      scrape_interval = "15s",
      evaluation_interval = "15s",
      external_labels = {
        environment = "development",
      },
    },
    alerting | {
      alertmanagers | Array AlertManagerConfig | default = [],
    } | default = {
      alertmanagers = [],
    },
    rule_files | Array String | default = [],
    scrape_configs | Array ScrapeConfig,
    remote_write | Array RemoteWrite | default = [],
  },

  # Service scrape preset (used to generate job for specific service)
  servicePreset = fun service_name port metrics_path interval =>
    {
      job_name = service_name,
      scrape_interval = interval,
      scrape_timeout = "5s",
      metrics_path = metrics_path,
      static_configs = [
        {
          targets = ["%{service_name}:%{port}"],
          labels = {
            service = service_name,
          },
        },
      ],
    },

  # Solo mode preset
  soloPrometheusPreset = {
    global = {
      scrape_interval = "15s",
      evaluation_interval = "15s",
      external_labels = {
        cluster = "provisioning-platform",
        environment = "development",
      },
    },
    alerting = {
      alertmanagers = [],
    },
    rule_files = ["/etc/prometheus/rules/*.yml"],
    scrape_configs = [
      {
        job_name = "prometheus",
        static_configs = [{targets = ["localhost:9090"]}],
      },
      {
        job_name = "orchestrator",
        scrape_interval = "10s",
        metrics_path = "/metrics",
        static_configs = [{targets = ["orchestrator:8080"]}],
      },
      {
        job_name = "control-center",
        scrape_interval = "10s",
        metrics_path = "/metrics",
        static_configs = [{targets = ["control-center:8081"]}],
      },
      {
        job_name = "oci-registry",
        scrape_interval = "30s",
        metrics_path = "/metrics",
        static_configs = [{targets = ["oci-registry:5001"]}],
      },
    ],
  },

  # Multiuser mode preset
  multiuserPrometheusPreset = {
    global = {
      scrape_interval = "15s",
      evaluation_interval = "15s",
      external_labels = {
        cluster = "provisioning-platform",
        environment = "staging",
      },
    },
    alerting = {
      alertmanagers = [
        {
          static_configs = [
            {
              targets = ["alertmanager:9093"],
            }
          ],
        }
      ],
    },
    rule_files = ["/etc/prometheus/rules/*.yml"],
    scrape_configs = [
      {
        job_name = "prometheus",
        static_configs = [{targets = ["localhost:9090"]}],
      },
      {
        job_name = "orchestrator",
        scrape_interval = "10s",
        metrics_path = "/metrics",
        static_configs = [
          {
            targets = ["orchestrator-1:8080", "orchestrator-2:8080"],
            labels = {service = "orchestrator"},
          }
        ],
      },
      {
        job_name = "control-center",
        scrape_interval = "10s",
        metrics_path = "/metrics",
        static_configs = [
          {
            targets = ["control-center-1:8081", "control-center-2:8081"],
            labels = {service = "control-center"},
          }
        ],
      },
      {
        job_name = "postgres",
        scrape_interval = "30s",
        static_configs = [{targets = ["postgres-exporter:9187"]}],
      },
      {
        job_name = "node",
        scrape_interval = "30s",
        static_configs = [{targets = ["node-exporter:9100"]}],
      },
    ],
  },

  # Enterprise mode preset
  enterprisePrometheusPreset = {
    global = {
      scrape_interval = "15s",
      evaluation_interval = "15s",
      external_labels = {
        cluster = "provisioning-platform",
        environment = "production",
      },
    },
    alerting = {
      alertmanagers = [
        {
          static_configs = [
            {
              targets = ["alertmanager-1:9093", "alertmanager-2:9093", "alertmanager-3:9093"],
            }
          ],
        }
      ],
    },
    rule_files = [
      "/etc/prometheus/rules/*.yml",
      "/etc/prometheus/rules/alerts.yml",
    ],
    remote_write = [
      {
        url = "http://remote-storage:9009/api/v1/push",
      }
    ],
    scrape_configs = [
      {
        job_name = "prometheus",
        static_configs = [{targets = ["localhost:9090"]}],
      },
      {
        job_name = "orchestrator",
        scrape_interval = "10s",
        metrics_path = "/metrics",
        static_configs = [
          {
            targets = ["orchestrator-1:8080", "orchestrator-2:8080", "orchestrator-3:8080"],
            labels = {service = "orchestrator", tier = "core"},
          }
        ],
      },
      {
        job_name = "control-center",
        scrape_interval = "10s",
        metrics_path = "/metrics",
        static_configs = [
          {
            targets = ["control-center-1:8081", "control-center-2:8081", "control-center-3:8081"],
            labels = {service = "control-center", tier = "core"},
          }
        ],
      },
      {
        job_name = "prometheus-fed",
        scrape_interval = "15s",
        static_configs = [
          {
            targets = ["prometheus-secondary-1:9090", "prometheus-secondary-2:9090"],
            labels = {replica = "secondary"},
          }
        ],
      },
      {
        job_name = "postgres",
        scrape_interval = "30s",
        static_configs = [{targets = ["postgres-exporter:9187"]}],
      },
      {
        job_name = "node",
        scrape_interval = "30s",
        static_configs = [
          {
            targets = ["node-exporter-1:9100", "node-exporter-2:9100", "node-exporter-3:9100"],
          }
        ],
      },
      {
        job_name = "cadvisor",
        scrape_interval = "30s",
        static_configs = [{targets = ["cadvisor:8080"]}],
      },
    ],
  },
}
