// 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.

#if GenAiTuningService
  import Foundation
  import GoogleCloudWkt

  /// The metric used for running evaluations.
  public struct Metric: Codable, Equatable, GoogleCloudWkt._AnyPackable,
    Sendable
  {
    /// Optional. The aggregation metrics to use.
    public var aggregationMetrics: [Metric.AggregationMetric] = []

    /// The spec for the metric.
    /// It would be either a pre-defined metric, or a inline metric spec.
    public var metricSpec: OneOf_MetricSpec? = nil

    /// Initialize a new instance of `Metric`.
    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 = Metric().with { $0.predefinedMetricSpec = ... }
    /// ```
    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 predefinedMetricSpec = "predefinedMetricSpec"
      case computationBasedMetricSpec = "computationBasedMetricSpec"
      case llmBasedMetricSpec = "llmBasedMetricSpec"
      case pointwiseMetricSpec = "pointwiseMetricSpec"
      case pairwiseMetricSpec = "pairwiseMetricSpec"
      case exactMatchSpec = "exactMatchSpec"
      case bleuSpec = "bleuSpec"
      case rougeSpec = "rougeSpec"
      case aggregationMetrics = "aggregationMetrics"
    }

    public init(from decoder: Decoder) throws {
      let container = try decoder.container(keyedBy: CodingKeys.self)
      self.aggregationMetrics = try container.decode(
        [Metric.AggregationMetric].self, forKey: .aggregationMetrics)

      var metricSpec: OneOf_MetricSpec? = nil
      let metricSpecCheckAndSet = {
        if metricSpec != nil {
          throw DecodingError.dataCorrupted(
            DecodingError.Context(
              codingPath: decoder.codingPath,
              debugDescription: "Multiple values set for oneof 'metricSpec'"))
        }
        metricSpec = $0
      }
      if let predefinedMetricSpec = try container.decodeIfPresent(
        PredefinedMetricSpec?.self, forKey: .predefinedMetricSpec)
      {
        try metricSpecCheckAndSet(.predefinedMetricSpec(predefinedMetricSpec))
      }
      if let computationBasedMetricSpec = try container.decodeIfPresent(
        ComputationBasedMetricSpec?.self, forKey: .computationBasedMetricSpec)
      {
        try metricSpecCheckAndSet(.computationBasedMetricSpec(computationBasedMetricSpec))
      }
      if let llmBasedMetricSpec = try container.decodeIfPresent(
        LLMBasedMetricSpec?.self, forKey: .llmBasedMetricSpec)
      {
        try metricSpecCheckAndSet(.llmBasedMetricSpec(llmBasedMetricSpec))
      }
      if let pointwiseMetricSpec = try container.decodeIfPresent(
        PointwiseMetricSpec?.self, forKey: .pointwiseMetricSpec)
      {
        try metricSpecCheckAndSet(.pointwiseMetricSpec(pointwiseMetricSpec))
      }
      if let pairwiseMetricSpec = try container.decodeIfPresent(
        PairwiseMetricSpec?.self, forKey: .pairwiseMetricSpec)
      {
        try metricSpecCheckAndSet(.pairwiseMetricSpec(pairwiseMetricSpec))
      }
      if let exactMatchSpec = try container.decodeIfPresent(
        ExactMatchSpec?.self, forKey: .exactMatchSpec)
      {
        try metricSpecCheckAndSet(.exactMatchSpec(exactMatchSpec))
      }
      if let bleuSpec = try container.decodeIfPresent(BleuSpec?.self, forKey: .bleuSpec) {
        try metricSpecCheckAndSet(.bleuSpec(bleuSpec))
      }
      if let rougeSpec = try container.decodeIfPresent(RougeSpec?.self, forKey: .rougeSpec) {
        try metricSpecCheckAndSet(.rougeSpec(rougeSpec))
      }
      self.metricSpec = metricSpec
    }

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

      if let choice = self.metricSpec {
        switch choice {
        case .predefinedMetricSpec(let value):
          try container.encode(value, forKey: .predefinedMetricSpec)
        case .computationBasedMetricSpec(let value):
          try container.encode(value, forKey: .computationBasedMetricSpec)
        case .llmBasedMetricSpec(let value):
          try container.encode(value, forKey: .llmBasedMetricSpec)
        case .pointwiseMetricSpec(let value):
          try container.encode(value, forKey: .pointwiseMetricSpec)
        case .pairwiseMetricSpec(let value):
          try container.encode(value, forKey: .pairwiseMetricSpec)
        case .exactMatchSpec(let value):
          try container.encode(value, forKey: .exactMatchSpec)
        case .bleuSpec(let value):
          try container.encode(value, forKey: .bleuSpec)
        case .rougeSpec(let value):
          try container.encode(value, forKey: .rougeSpec)
        }
      }
    }

    /// The per-metric statistics on evaluation results supported by
    /// `EvaluationService.EvaluateDataset`.
    public enum AggregationMetric: Codable, Equatable, Sendable {
      /// Unspecified aggregation metric.
      case unspecified
      /// Average aggregation metric. Not supported for Pairwise metric.
      case average
      /// Mode aggregation metric.
      case mode
      /// Standard deviation aggregation metric. Not supported for pairwise metric.
      case standardDeviation
      /// Variance aggregation metric. Not supported for pairwise metric.
      case variance
      /// Minimum aggregation metric. Not supported for pairwise metric.
      case minimum
      /// Maximum aggregation metric. Not supported for pairwise metric.
      case maximum
      /// Median aggregation metric. Not supported for pairwise metric.
      case median
      /// 90th percentile aggregation metric. Not supported for pairwise metric.
      case percentileP90
      /// 95th percentile aggregation metric. Not supported for pairwise metric.
      case percentileP95
      /// 99th percentile aggregation metric. Not supported for pairwise metric.
      case percentileP99
      /// Encodes an unknown integer value.
      ///
      /// The most common cause for an unknown values is for the service to send
      /// a value unknown to the library. We recommend you update your library to
      /// the latest version.
      case unknownIntValue(Int)
      /// Encodes an unknown string value.
      ///
      /// The most common cause for an unknown values is for the service to send
      /// a value unknown to the library. We recommend you update your library to
      /// the latest version.
      case unknownStringValue(String)

      public init() {
        self = .unspecified
      }

      /// Returns the integer value associated with the enumeration.
      ///
      /// If the enumeration was initialized with an unknown string value, this returns `nil`.
      public var intValue: Int? {
        switch self {
        case .unspecified: return 0
        case .average: return 1
        case .mode: return 2
        case .standardDeviation: return 3
        case .variance: return 4
        case .minimum: return 5
        case .maximum: return 6
        case .median: return 7
        case .percentileP90: return 8
        case .percentileP95: return 9
        case .percentileP99: return 10
        case .unknownIntValue(let v): return v
        case .unknownStringValue: return nil
        }
      }

      /// Returns the string value (or name) associated with the enumeration.
      ///
      /// If the enumeration was initialized with an unknown integer value, this returns `nil`.
      public var stringValue: Swift.String? {
        switch self {
        case .unspecified: return "AGGREGATION_METRIC_UNSPECIFIED"
        case .average: return "AVERAGE"
        case .mode: return "MODE"
        case .standardDeviation: return "STANDARD_DEVIATION"
        case .variance: return "VARIANCE"
        case .minimum: return "MINIMUM"
        case .maximum: return "MAXIMUM"
        case .median: return "MEDIAN"
        case .percentileP90: return "PERCENTILE_P90"
        case .percentileP95: return "PERCENTILE_P95"
        case .percentileP99: return "PERCENTILE_P99"
        case .unknownIntValue: return nil
        case .unknownStringValue(let v): return v
        }
      }

      /// Initialize from a string value.
      ///
      /// If the value is unknown, this initializes to [`unknownStringValue`](doc:AggregationMetric/unknownStringValue(_:)).
      public init(stringValue: Swift.String) {
        switch stringValue {
        case "AGGREGATION_METRIC_UNSPECIFIED": self = .unspecified
        case "AVERAGE": self = .average
        case "MODE": self = .mode
        case "STANDARD_DEVIATION": self = .standardDeviation
        case "VARIANCE": self = .variance
        case "MINIMUM": self = .minimum
        case "MAXIMUM": self = .maximum
        case "MEDIAN": self = .median
        case "PERCENTILE_P90": self = .percentileP90
        case "PERCENTILE_P95": self = .percentileP95
        case "PERCENTILE_P99": self = .percentileP99
        default: self = .unknownStringValue(stringValue)
        }
      }

      /// Initialize from an integer value.
      ///
      /// If the value is unknown, this initializes to [`unknownIntValue`](doc:AggregationMetric/unknownIntValue(_:)).
      public init(intValue: Int) {
        switch intValue {
        case 0: self = .unspecified
        case 1: self = .average
        case 2: self = .mode
        case 3: self = .standardDeviation
        case 4: self = .variance
        case 5: self = .minimum
        case 6: self = .maximum
        case 7: self = .median
        case 8: self = .percentileP90
        case 9: self = .percentileP95
        case 10: self = .percentileP99
        default: self = .unknownIntValue(intValue)
        }
      }

      public init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        if let v = try? container.decode(Int.self) {
          self.init(intValue: v)
          return
        }
        if let s = try? container.decode(String.self) {
          if let v = Int(s) {
            self.init(intValue: v)
          } else {
            self.init(stringValue: s)
          }
          return
        }
        throw DecodingError.dataCorruptedError(
          in: container, debugDescription: "Expected enum value, must be integer or string.")
      }

      public func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .unspecified: return try container.encode(0)
        case .average: return try container.encode(1)
        case .mode: return try container.encode(2)
        case .standardDeviation: return try container.encode(3)
        case .variance: return try container.encode(4)
        case .minimum: return try container.encode(5)
        case .maximum: return try container.encode(6)
        case .median: return try container.encode(7)
        case .percentileP90: return try container.encode(8)
        case .percentileP95: return try container.encode(9)
        case .percentileP99: return try container.encode(10)
        case .unknownIntValue(let v): return try container.encode(v)
        case .unknownStringValue(let v): return try container.encode(v)
        }
      }
    }

    /// The spec for the metric.
    /// It would be either a pre-defined metric, or a inline metric spec.
    public enum OneOf_MetricSpec: Codable, Equatable, Sendable {
      /// The spec for a pre-defined metric.
      indirect case predefinedMetricSpec(PredefinedMetricSpec?)
      /// Spec for a computation based metric.
      indirect case computationBasedMetricSpec(ComputationBasedMetricSpec?)
      /// Spec for an LLM based metric.
      indirect case llmBasedMetricSpec(LLMBasedMetricSpec?)
      /// Spec for pointwise metric.
      indirect case pointwiseMetricSpec(PointwiseMetricSpec?)
      /// Spec for pairwise metric.
      indirect case pairwiseMetricSpec(PairwiseMetricSpec?)
      /// Spec for exact match metric.
      indirect case exactMatchSpec(ExactMatchSpec?)
      /// Spec for bleu metric.
      indirect case bleuSpec(BleuSpec?)
      /// Spec for rouge metric.
      indirect case rougeSpec(RougeSpec?)
    }

    public static var _anyTypeUrl: Swift.String {
      return "type.googleapis.com/google.cloud.aiplatform.v1.Metric"
    }
    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)
    }
  }
#endif
