// Code generated by sidekick. DO NOT EDIT.
//
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation
import GoogleCloudWkt

/// Represents a single metric value.
public struct MetricValue: Codable, Equatable, GoogleCloudWkt._AnyPackable,
  Sendable
{
  /// The labels describing the metric value.
  /// See comments on
  /// [google.api.servicecontrol.v1.Operation.labels][google.api.servicecontrol.v1.Operation.labels]
  /// for the overriding relationship. Note that this map must not contain
  /// monitored resource labels.
  ///
  /// [google.api.servicecontrol.v1.Operation.labels]: <doc:Operation/labels>
  public var labels: [Swift.String: Swift.String] = [:]

  /// The start of the time period over which this metric value's measurement
  /// applies. The time period has different semantics for different metric
  /// types (cumulative, delta, and gauge). See the metric definition
  /// documentation in the service configuration for details. If not specified,
  /// [google.api.servicecontrol.v1.Operation.start_time][google.api.servicecontrol.v1.Operation.start_time]
  /// will be used.
  ///
  /// [google.api.servicecontrol.v1.Operation.start_time]: <doc:Operation/startTime>
  public var startTime: GoogleCloudWkt.Timestamp? = nil

  /// The end of the time period over which this metric value's measurement
  /// applies.  If not specified,
  /// [google.api.servicecontrol.v1.Operation.end_time][google.api.servicecontrol.v1.Operation.end_time]
  /// will be used.
  ///
  /// [google.api.servicecontrol.v1.Operation.end_time]: <doc:Operation/endTime>
  public var endTime: GoogleCloudWkt.Timestamp? = nil

  /// The value. The type of value used in the request must
  /// agree with the metric definition in the service configuration, otherwise
  /// the MetricValue is rejected.
  public var value: OneOf_Value? = nil

  /// Initialize a new instance of `MetricValue`.
  public init() {}

  /// Use `config` to return a new instance of this object, with some fields updated.
  ///
  /// Commonly used to initialize the value, for example:
  ///
  /// ```
  /// let value = MetricValue().with { $0.labels = ... }
  /// ```
  public func with(_ config: (inout Self) throws -> Swift.Void) rethrows -> Self {
    var copy = self
    try config(&copy)
    return copy
  }

  private enum CodingKeys: Swift.String, CodingKey {
    case labels = "labels"
    case startTime = "startTime"
    case endTime = "endTime"
    case boolValue = "boolValue"
    case int64Value = "int64Value"
    case doubleValue = "doubleValue"
    case stringValue = "stringValue"
    case distributionValue = "distributionValue"
  }

  public init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    self.labels = try container.decode([Swift.String: Swift.String].self, forKey: .labels)
    self.startTime = try container.decodeIfPresent(
      GoogleCloudWkt.Timestamp.self, forKey: .startTime)
    self.endTime = try container.decodeIfPresent(GoogleCloudWkt.Timestamp.self, forKey: .endTime)

    var value: OneOf_Value? = nil
    let valueCheckAndSet = {
      if value != nil {
        throw DecodingError.dataCorrupted(
          DecodingError.Context(
            codingPath: decoder.codingPath,
            debugDescription: "Multiple values set for oneof 'value'"))
      }
      value = $0
    }
    if let boolValue = try container.decodeIfPresent(Swift.Bool.self, forKey: .boolValue) {
      try valueCheckAndSet(.boolValue(boolValue))
    }
    if let int64Value = try container.decodeIfPresent(Swift.Int64.self, forKey: .int64Value) {
      try valueCheckAndSet(.int64Value(int64Value))
    }
    if let doubleValue = try container.decodeIfPresent(Swift.Double.self, forKey: .doubleValue) {
      try valueCheckAndSet(.doubleValue(doubleValue))
    }
    if let stringValue = try container.decodeIfPresent(Swift.String.self, forKey: .stringValue) {
      try valueCheckAndSet(.stringValue(stringValue))
    }
    if let distributionValue = try container.decodeIfPresent(
      Distribution?.self, forKey: .distributionValue)
    {
      try valueCheckAndSet(.distributionValue(distributionValue))
    }
    self.value = value
  }

  public func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(self.labels, forKey: .labels)
    try container.encode(self.startTime, forKey: .startTime)
    try container.encode(self.endTime, forKey: .endTime)

    if let choice = self.value {
      switch choice {
      case .boolValue(let value):
        try container.encode(value, forKey: .boolValue)
      case .int64Value(let value):
        try container.encode(value, forKey: .int64Value)
      case .doubleValue(let value):
        try container.encode(value, forKey: .doubleValue)
      case .stringValue(let value):
        try container.encode(value, forKey: .stringValue)
      case .distributionValue(let value):
        try container.encode(value, forKey: .distributionValue)
      }
    }
  }

  /// The value. The type of value used in the request must
  /// agree with the metric definition in the service configuration, otherwise
  /// the MetricValue is rejected.
  public enum OneOf_Value: Codable, Equatable, Sendable {
    /// A boolean value.
    case boolValue(Swift.Bool)
    /// A signed 64-bit integer value.
    case int64Value(Swift.Int64)
    /// A double precision floating point value.
    case doubleValue(Swift.Double)
    /// A text string value.
    case stringValue(Swift.String)
    /// A distribution value.
    indirect case distributionValue(Distribution?)
  }

  public static var _anyTypeUrl: Swift.String {
    return "type.googleapis.com/google.api.servicecontrol.v1.MetricValue"
  }
  public init(fromAny any: GoogleCloudWkt.`Any`) throws {
    self = try GoogleCloudWkt._slowAnyDeserialize(Self.self, from: any)
  }
  public func _pack() throws -> GoogleCloudWkt.Struct {
    return try GoogleCloudWkt._slowAnySerialize(message: self)
  }
}
