# Common Validators
# Reusable validation logic for ports, positive numbers, strings, ranges

let constraints = import "../constraints/constraints.toml" in

{
  # Validate port number within allowed range
  ValidPort = fun port =>
    if port < constraints.common.server.port.min then
      std.contract.blame_with_message
        "Port must be >= %{std.to_string constraints.common.server.port.min}"
        port
    else if port > constraints.common.server.port.max then
      std.contract.blame_with_message
        "Port must be <= %{std.to_string constraints.common.server.port.max}"
        port
    else
      port,

  # Validate positive number (> 0)
  ValidPositiveNumber = fun n =>
    if n <= 0 then
      std.contract.blame_with_message "Value must be positive (> 0)" n
    else
      n,

  # Validate non-negative number (>= 0)
  ValidNonNegativeNumber = fun n =>
    if n < 0 then
      std.contract.blame_with_message "Value must be non-negative (>= 0)" n
    else
      n,

  # Validate non-empty string
  ValidNonEmptyString = fun s =>
    if s == "" then
      std.contract.blame_with_message "String cannot be empty" s
    else
      s,

  # Validate string length
  ValidStringLength = fun min_len max_len value =>
    let len = std.string.length value in
    if len < min_len then
      std.contract.blame_with_message
        "String length must be >= %{std.to_string min_len}"
        value
    else if len > max_len then
      std.contract.blame_with_message
        "String length must be <= %{std.to_string max_len}"
        value
    else
      value,

  # Validate generic range (min to max inclusive)
  ValidRange = fun min_val max_val value =>
    if value < min_val then
      std.contract.blame_with_message
        "Value must be >= %{std.to_string min_val}, got %{std.to_string value}"
        value
    else if value > max_val then
      std.contract.blame_with_message
        "Value must be <= %{std.to_string max_val}, got %{std.to_string value}"
        value
    else
      value,

  # Validate enum value in allowed set
  ValidEnum = fun allowed_values value =>
    if std.array.elem value allowed_values then
      value
    else
      std.contract.blame_with_message
        "Value must be one of: %{std.to_string allowed_values}"
        value,

  # Validate timeout duration in milliseconds
  ValidTimeoutMs = fun timeout =>
    if timeout < 1000 then
      std.contract.blame_with_message "Timeout must be >= 1000ms (1 second)" timeout
    else if timeout > 86400000 then
      std.contract.blame_with_message "Timeout must be <= 86400000ms (24 hours)" timeout
    else
      timeout,

  # Validate interval/period in seconds
  ValidIntervalSeconds = fun interval =>
    if interval < 1 then
      std.contract.blame_with_message "Interval must be >= 1 second" interval
    else if interval > 3600 then
      std.contract.blame_with_message "Interval must be <= 3600 seconds (1 hour)" interval
    else
      interval,

  # Validate percentage (0-100)
  ValidPercentage = fun percentage =>
    if percentage < 0 then
      std.contract.blame_with_message "Percentage must be >= 0" percentage
    else if percentage > 100 then
      std.contract.blame_with_message "Percentage must be <= 100" percentage
    else
      percentage,

  # Validate sample rate (0.0-1.0)
  ValidSampleRate = fun rate =>
    if rate < 0.0 then
      std.contract.blame_with_message "Sample rate must be >= 0.0" rate
    else if rate > 1.0 then
      std.contract.blame_with_message "Sample rate must be <= 1.0" rate
    else
      rate,
}
