# Monitoring Schema - Monitoring configurations (Prometheus, Grafana, etc.)
# Version: 1.0.0

{
  # Prometheus scrape configuration
  PrometheusScrapeConfig = {
    job_name
      | String
      | doc "Job name for the scrape target",

    scrape_interval
      | String
      | doc "Scrape interval (e.g., '30s', '1m')"
      | default = "30s",

    scrape_timeout
      | String
      | doc "Scrape timeout (e.g., '10s')"
      | default = "10s",

    metrics_path
      | String
      | doc "HTTP path to scrape metrics from"
      | default = "/metrics",

    scheme
      | [| 'http, 'https |]
      | doc "HTTP scheme"
      | default = 'http,

    static_configs
      | Array {
        targets
          | Array String
          | doc "List of targets (e.g., ['localhost:9090'])",

        labels
          | {_: String}
          | doc "Labels to attach to scraped metrics"
          | default = {},
      }
      | doc "Static target configurations"
      | default = [],

    relabel_configs
      | Array {
        source_labels
          | Array String
          | doc "Source labels"
          | optional,

        target_label
          | String
          | doc "Target label"
          | optional,

        regex
          | String
          | doc "Regex pattern"
          | optional,

        replacement
          | String
          | doc "Replacement value"
          | optional,

        action
          | [| 'replace, 'keep, 'drop, 'hashmod, 'labelmap, 'labeldrop, 'labelkeep |]
          | doc "Relabeling action"
          | default = 'replace,
      }
      | doc "Relabeling configurations"
      | default = [],
  },

  # Prometheus configuration
  PrometheusConfig = {
    enabled
      | Bool
      | doc "Enable Prometheus monitoring"
      | default = true,

    port
      | Number
      | doc "Prometheus server port (1024-65535)"
      | {
        label = "valid port",
        predicate = fun p => p >= 1024 && p <= 65535
      }
      | default = 9090,

    image
      | String
      | doc "Prometheus Docker image"
      | default = "prom/prometheus:latest",

    scrape_interval
      | String
      | doc "Global scrape interval (e.g., '15s', '1m')"
      | default = "15s",

    evaluation_interval
      | String
      | doc "Rule evaluation interval (e.g., '15s')"
      | default = "15s",

    retention
      | String
      | doc "Data retention period (e.g., '15d', '30d')"
      | default = "15d",

    scrape_configs
      | Array PrometheusScrapeConfig
      | doc "Scrape configurations for targets"
      | default = [],

    alerting
      | {
        alertmanagers
          | Array {
            static_configs
              | Array {
                targets
                  | Array String
                  | doc "Alertmanager targets",
              }
              | doc "Static alertmanager configs",
          }
          | doc "Alertmanager configurations"
          | default = [],
      }
      | doc "Alerting configuration"
      | optional,

    rule_files
      | Array String
      | doc "Paths to alert/recording rule files"
      | default = [],

    storage_path
      | String
      | doc "Path to store Prometheus data"
      | default = "/prometheus",

    external_labels
      | {_: String}
      | doc "External labels to attach to all metrics"
      | default = {},
  },

  # Grafana datasource configuration
  GrafanaDatasource = {
    name
      | String
      | doc "Datasource name",

    type
      | [| 'prometheus, 'loki, 'jaeger, 'tempo, 'influxdb, 'elasticsearch |]
      | doc "Datasource type",

    access
      | [| 'proxy, 'direct |]
      | doc "Access mode"
      | default = 'proxy,

    url
      | String
      | doc "Datasource URL",

    is_default
      | Bool
      | doc "Set as default datasource"
      | default = false,

    basic_auth
      | Bool
      | doc "Enable basic authentication"
      | default = false,

    basic_auth_user
      | String
      | doc "Basic auth username"
      | optional,

    basic_auth_password
      | String
      | doc "Basic auth password"
      | optional,

    json_data
      | {_: _}
      | doc "Additional JSON configuration"
      | default = {},
  },

  # Grafana configuration
  GrafanaConfig = {
    enabled
      | Bool
      | doc "Enable Grafana dashboards"
      | default = true,

    port
      | Number
      | doc "Grafana server port (1024-65535)"
      | {
        label = "valid port",
        predicate = fun p => p >= 1024 && p <= 65535
      }
      | default = 3000,

    image
      | String
      | doc "Grafana Docker image"
      | default = "grafana/grafana:latest",

    admin_user
      | String
      | doc "Admin username"
      | default = "admin",

    admin_password
      | String
      | doc "Admin password (8-128 characters)"
      | {
        label = "valid password",
        predicate = fun s => std.string.length s >= 8 && std.string.length s <= 128
      },

    datasources
      | Array GrafanaDatasource
      | doc "Datasource configurations"
      | default = [],

    dashboards_path
      | String
      | doc "Path to dashboard JSON files"
      | default = "/etc/grafana/dashboards",

    plugins
      | Array String
      | doc "Grafana plugins to install"
      | default = [],

    environment
      | {_: String}
      | doc "Additional environment variables"
      | default = {},

    anonymous_access
      | Bool
      | doc "Enable anonymous access"
      | default = false,

    serve_from_sub_path
      | Bool
      | doc "Serve Grafana from a sub-path"
      | default = false,

    root_url
      | String
      | doc "Full public-facing URL"
      | optional,
  },

  # Loki configuration
  LokiConfig = {
    enabled
      | Bool
      | doc "Enable Loki log aggregation"
      | default = false,

    port
      | Number
      | doc "Loki server port (1024-65535)"
      | {
        label = "valid port",
        predicate = fun p => p >= 1024 && p <= 65535
      }
      | default = 3100,

    image
      | String
      | doc "Loki Docker image"
      | default = "grafana/loki:latest",

    storage_path
      | String
      | doc "Path to store Loki data"
      | default = "/loki",

    retention_period
      | String
      | doc "Log retention period (e.g., '7d', '30d')"
      | default = "7d",
  },

  # Jaeger tracing configuration
  JaegerConfig = {
    enabled
      | Bool
      | doc "Enable Jaeger distributed tracing"
      | default = false,

    collector_port
      | Number
      | doc "Jaeger collector port (1024-65535)"
      | {
        label = "valid port",
        predicate = fun p => p >= 1024 && p <= 65535
      }
      | default = 14268,

    query_port
      | Number
      | doc "Jaeger query UI port (1024-65535)"
      | {
        label = "valid port",
        predicate = fun p => p >= 1024 && p <= 65535
      }
      | default = 16686,

    image
      | String
      | doc "Jaeger all-in-one Docker image"
      | default = "jaegertracing/all-in-one:latest",

    sampling_strategy
      | [| 'const, 'probabilistic, 'ratelimiting, 'remote |]
      | doc "Sampling strategy"
      | default = 'const,

    sampling_param
      | Number
      | doc "Sampling parameter (0.0-1.0 for probabilistic)"
      | default = 1.0,
  },

  # Main monitoring configuration
  MonitoringConfig = {
    prometheus
      | PrometheusConfig
      | doc "Prometheus configuration"
      | optional,

    grafana
      | GrafanaConfig
      | doc "Grafana configuration"
      | optional,

    loki
      | LokiConfig
      | doc "Loki configuration"
      | optional,

    jaeger
      | JaegerConfig
      | doc "Jaeger configuration"
      | optional,

    alerts_path
      | String
      | doc "Path to alert rule files"
      | default = "./alerts",

    dashboard_refresh_interval
      | String
      | doc "Default dashboard refresh interval (e.g., '5s', '30s')"
      | default = "30s",
  },
}
