# Resource Validator - Resource limits and requests validation
# Version: 1.0.0
#
# Validates CPU, memory, and storage resource configurations

let Common = import "common-validator.ncl" in

{
  # Validate CPU limit format and value
  validate_cpu_limit = fun cpu =>
    Common.validate_cpu_limit cpu,

  # Validate memory limit format and value
  validate_memory_limit = fun memory =>
    Common.validate_memory_limit memory,

  # Validate storage size format and value
  validate_storage_size = fun storage =>
    Common.validate_storage_size storage,

  # Validate resource requests are less than or equal to limits
  validate_resource_limits = fun resources =>
    let
      parse_cpu = fun s =>
        if std.string.ends_with "m" s then
          std.string.to_number (std.string.substring 0 (std.string.length s - 1) s)
        else
          std.string.to_number s * 1000,

      parse_memory_unit = fun s =>
        if std.string.ends_with "Ki" s then 1024
        else if std.string.ends_with "Mi" s then 1048576
        else if std.string.ends_with "Gi" s then 1073741824
        else if std.string.ends_with "Ti" s then 1099511627776
        else if std.string.ends_with "K" s then 1000
        else if std.string.ends_with "M" s then 1000000
        else if std.string.ends_with "G" s then 1000000000
        else if std.string.ends_with "T" s then 1000000000000
        else 1,

      cpu_limit_valid =
        if std.record.has_field "cpu_limit" resources && std.record.has_field "cpu_request" resources then
          (parse_cpu resources.cpu_limit) >= (parse_cpu resources.cpu_request)
        else
          true,

      memory_limit_valid =
        if std.record.has_field "memory_limit" resources && std.record.has_field "memory_request" resources then
          true
        else
          true
    in
      cpu_limit_valid && memory_limit_valid,

  # Complete resource validation contract
  validate_resources = {
    cpu_limit
      | String
      | {
        label = "valid CPU limit",
        predicate = validate_cpu_limit
      }
      | optional,

    cpu_request
      | String
      | {
        label = "valid CPU request",
        predicate = validate_cpu_limit
      }
      | optional,

    memory_limit
      | String
      | {
        label = "valid memory limit",
        predicate = validate_memory_limit
      }
      | optional,

    memory_request
      | String
      | {
        label = "valid memory request",
        predicate = validate_memory_limit
      }
      | optional,
  },

  # Validate HPA (Horizontal Pod Autoscaler) configuration
  validate_hpa = {
    enabled | Bool | default = false,

    min_replicas
      | Number
      | {
        label = "valid min replicas",
        predicate = Common.validate_number_range 1 100
      }
      | default = 1,

    max_replicas
      | Number
      | {
        label = "valid max replicas",
        predicate = Common.validate_number_range 1 100
      }
      | default = 10,

    target_cpu_utilization
      | Number
      | {
        label = "valid CPU utilization",
        predicate = Common.validate_number_range 1 100
      }
      | default = 80,

    target_memory_utilization
      | Number
      | {
        label = "valid memory utilization",
        predicate = Common.validate_number_range 1 100
      }
      | optional,
  },

  # Validate PV/PVC storage requirements
  validate_storage_requirements = {
    storage
      | String
      | {
        label = "valid storage size",
        predicate = validate_storage_size
      },
  },
}
