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

/// Message describing an aggregation. The message includes the aggregation type,
/// parameters, and the field on which to perform the aggregation.
public struct Aggregation: Codable, Equatable, GoogleCloudWkt._AnyPackable,
  Sendable
{
  /// The name of the field on which to aggregate.
  public var field: Swift.String = Swift.String()

  public var aggregationFunction: OneOf_AggregationFunction? = nil

  /// Initialize a new instance of `Aggregation`.
  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 = Aggregation().with { $0.field = ... }
  /// ```
  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 field = "field"
    case count = "count"
    case sum = "sum"
    case histogram = "histogram"
    case frequency = "frequency"
  }

  public init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    self.field = try container.decode(Swift.String.self, forKey: .field)

    var aggregationFunction: OneOf_AggregationFunction? = nil
    let aggregationFunctionCheckAndSet = {
      if aggregationFunction != nil {
        throw DecodingError.dataCorrupted(
          DecodingError.Context(
            codingPath: decoder.codingPath,
            debugDescription: "Multiple values set for oneof 'aggregationFunction'"))
      }
      aggregationFunction = $0
    }
    if let count = try container.decodeIfPresent(Aggregation.Count?.self, forKey: .count) {
      try aggregationFunctionCheckAndSet(.count(count))
    }
    if let sum = try container.decodeIfPresent(Aggregation.Sum?.self, forKey: .sum) {
      try aggregationFunctionCheckAndSet(.sum(sum))
    }
    if let histogram = try container.decodeIfPresent(
      Aggregation.Histogram?.self, forKey: .histogram)
    {
      try aggregationFunctionCheckAndSet(.histogram(histogram))
    }
    if let frequency = try container.decodeIfPresent(
      Aggregation.Frequency?.self, forKey: .frequency)
    {
      try aggregationFunctionCheckAndSet(.frequency(frequency))
    }
    self.aggregationFunction = aggregationFunction
  }

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

    if let choice = self.aggregationFunction {
      switch choice {
      case .count(let value):
        try container.encode(value, forKey: .count)
      case .sum(let value):
        try container.encode(value, forKey: .sum)
      case .histogram(let value):
        try container.encode(value, forKey: .histogram)
      case .frequency(let value):
        try container.encode(value, forKey: .frequency)
      }
    }
  }

  /// Object count.
  public struct Count: Codable, Equatable, GoogleCloudWkt._AnyPackable,
    Sendable
  {
    /// Initialize a new instance of `Count`.
    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 = Count().with { $0.<placeholder> = ... }
    /// ```
    public func with(_ config: (inout Self) throws -> Swift.Void) rethrows -> Self {
      var copy = self
      try config(&copy)
      return copy
    }

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

  /// Sum of field values.
  public struct Sum: Codable, Equatable, GoogleCloudWkt._AnyPackable,
    Sendable
  {
    /// Initialize a new instance of `Sum`.
    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 = Sum().with { $0.<placeholder> = ... }
    /// ```
    public func with(_ config: (inout Self) throws -> Swift.Void) rethrows -> Self {
      var copy = self
      try config(&copy)
      return copy
    }

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

  /// Histogram of bucketed assets counts by field value.
  public struct Histogram: Codable, Equatable, GoogleCloudWkt._AnyPackable,
    Sendable
  {
    /// Lower bounds of buckets.
    /// The response will contain `n+1` buckets for `n` bounds.
    /// The first bucket will count all assets for which the field value is
    /// smaller than the first bound.
    /// Subsequent buckets will count assets for which the field value is
    /// greater or equal to a lower bound and smaller than the next one.
    /// The last bucket will count assets for which the field value is greater or
    /// equal to the final lower bound.
    /// You can define up to 20 lower bounds.
    public var lowerBounds: [Swift.Double] = []

    /// Initialize a new instance of `Histogram`.
    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 = Histogram().with { $0.lowerBounds = ... }
    /// ```
    public func with(_ config: (inout Self) throws -> Swift.Void) rethrows -> Self {
      var copy = self
      try config(&copy)
      return copy
    }

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

  /// Frequency distribution of all field values.
  public struct Frequency: Codable, Equatable, GoogleCloudWkt._AnyPackable,
    Sendable
  {
    /// Initialize a new instance of `Frequency`.
    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 = Frequency().with { $0.<placeholder> = ... }
    /// ```
    public func with(_ config: (inout Self) throws -> Swift.Void) rethrows -> Self {
      var copy = self
      try config(&copy)
      return copy
    }

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

  public enum OneOf_AggregationFunction: Codable, Equatable, Sendable {
    /// Count the number of matching objects.
    indirect case count(Aggregation.Count?)
    /// Sum over a numeric field.
    indirect case sum(Aggregation.Sum?)
    /// Creates a bucketed histogram of field values.
    indirect case histogram(Aggregation.Histogram?)
    /// Creates a frequency distribution of all field values.
    indirect case frequency(Aggregation.Frequency?)
  }

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