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

/// RBACRoleBinding represents a rbacrolebinding across the Fleet
public struct RBACRoleBinding: Codable, Equatable, GoogleCloudWkt._AnyPackable,
  Sendable
{
  /// The resource name for the rbacrolebinding
  /// `projects/{project}/locations/{location}/scopes/{scope}/rbacrolebindings/{rbacrolebinding}`
  /// or
  /// `projects/{project}/locations/{location}/memberships/{membership}/rbacrolebindings/{rbacrolebinding}`
  public var name: Swift.String = Swift.String()

  /// Output only. Google-generated UUID for this resource. This is unique across
  /// all rbacrolebinding resources. If a rbacrolebinding resource is deleted and
  /// another resource with the same name is created, it gets a different uid.
  public var uid: Swift.String = Swift.String()

  /// Output only. When the rbacrolebinding was created.
  public var createTime: GoogleCloudWkt.Timestamp? = nil

  /// Output only. When the rbacrolebinding was last updated.
  public var updateTime: GoogleCloudWkt.Timestamp? = nil

  /// Output only. When the rbacrolebinding was deleted.
  public var deleteTime: GoogleCloudWkt.Timestamp? = nil

  /// Output only. State of the rbacrolebinding resource.
  public var state: RBACRoleBindingLifecycleState? = nil

  /// Required. Role to bind to the principal
  public var role: RBACRoleBinding.Role? = nil

  /// Optional. Labels for this RBACRolebinding.
  public var labels: [Swift.String: Swift.String] = [:]

  /// Principal that is be authorized in the cluster (at least of one the oneof
  /// is required). Updating one will unset the other automatically.
  public var principal: OneOf_Principal? = nil

  /// Initialize a new instance of `RBACRoleBinding`.
  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 = RBACRoleBinding().with { $0.user = ... }
  /// ```
  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 user = "user"
    case group = "group"
    case name = "name"
    case uid = "uid"
    case createTime = "createTime"
    case updateTime = "updateTime"
    case deleteTime = "deleteTime"
    case state = "state"
    case role = "role"
    case labels = "labels"
  }

  public init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    self.name = try container.decode(Swift.String.self, forKey: .name)
    self.uid = try container.decode(Swift.String.self, forKey: .uid)
    self.createTime = try container.decodeIfPresent(
      GoogleCloudWkt.Timestamp.self, forKey: .createTime)
    self.updateTime = try container.decodeIfPresent(
      GoogleCloudWkt.Timestamp.self, forKey: .updateTime)
    self.deleteTime = try container.decodeIfPresent(
      GoogleCloudWkt.Timestamp.self, forKey: .deleteTime)
    self.state = try container.decodeIfPresent(RBACRoleBindingLifecycleState.self, forKey: .state)
    self.role = try container.decodeIfPresent(RBACRoleBinding.Role.self, forKey: .role)
    self.labels = try container.decode([Swift.String: Swift.String].self, forKey: .labels)

    var principal: OneOf_Principal? = nil
    let principalCheckAndSet = {
      if principal != nil {
        throw DecodingError.dataCorrupted(
          DecodingError.Context(
            codingPath: decoder.codingPath,
            debugDescription: "Multiple values set for oneof 'principal'"))
      }
      principal = $0
    }
    if let user = try container.decodeIfPresent(Swift.String.self, forKey: .user) {
      try principalCheckAndSet(.user(user))
    }
    if let group = try container.decodeIfPresent(Swift.String.self, forKey: .group) {
      try principalCheckAndSet(.group(group))
    }
    self.principal = principal
  }

  public func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(self.name, forKey: .name)
    try container.encode(self.uid, forKey: .uid)
    try container.encode(self.createTime, forKey: .createTime)
    try container.encode(self.updateTime, forKey: .updateTime)
    try container.encode(self.deleteTime, forKey: .deleteTime)
    try container.encode(self.state, forKey: .state)
    try container.encode(self.role, forKey: .role)
    try container.encode(self.labels, forKey: .labels)

    if let choice = self.principal {
      switch choice {
      case .user(let value):
        try container.encode(value, forKey: .user)
      case .group(let value):
        try container.encode(value, forKey: .group)
      }
    }
  }

  /// Role is the type for Kubernetes roles
  public struct Role: Codable, Equatable, GoogleCloudWkt._AnyPackable,
    Sendable
  {
    /// predefined_role is the Kubernetes default role to use
    public var predefinedRole: RBACRoleBinding.Role.PredefinedRoles = RBACRoleBinding.Role
      .PredefinedRoles()

    /// Optional. custom_role is the name of a custom KubernetesClusterRole to
    /// use.
    public var customRole: Swift.String = Swift.String()

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

    /// PredefinedRoles is an ENUM representation of the default Kubernetes Roles
    public enum PredefinedRoles: Codable, Equatable, Sendable {
      /// UNKNOWN
      case unknown
      /// ADMIN has EDIT and RBAC permissions
      case admin
      /// EDIT can edit all resources except RBAC
      case edit
      /// VIEW can only read resources
      case view
      /// ANTHOS_SUPPORT gives Google Support read-only access to a number of
      /// cluster resources.
      case anthosSupport
      /// 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 = .unknown
      }

      /// 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 .unknown: return 0
        case .admin: return 1
        case .edit: return 2
        case .view: return 3
        case .anthosSupport: return 4
        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 .unknown: return "UNKNOWN"
        case .admin: return "ADMIN"
        case .edit: return "EDIT"
        case .view: return "VIEW"
        case .anthosSupport: return "ANTHOS_SUPPORT"
        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:PredefinedRoles/unknownStringValue(_:)).
      public init(stringValue: Swift.String) {
        switch stringValue {
        case "UNKNOWN": self = .unknown
        case "ADMIN": self = .admin
        case "EDIT": self = .edit
        case "VIEW": self = .view
        case "ANTHOS_SUPPORT": self = .anthosSupport
        default: self = .unknownStringValue(stringValue)
        }
      }

      /// Initialize from an integer value.
      ///
      /// If the value is unknown, this initializes to [`unknownIntValue`](doc:PredefinedRoles/unknownIntValue(_:)).
      public init(intValue: Int) {
        switch intValue {
        case 0: self = .unknown
        case 1: self = .admin
        case 2: self = .edit
        case 3: self = .view
        case 4: self = .anthosSupport
        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 .unknown: return try container.encode(0)
        case .admin: return try container.encode(1)
        case .edit: return try container.encode(2)
        case .view: return try container.encode(3)
        case .anthosSupport: return try container.encode(4)
        case .unknownIntValue(let v): return try container.encode(v)
        case .unknownStringValue(let v): return try container.encode(v)
        }
      }
    }

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

  /// Principal that is be authorized in the cluster (at least of one the oneof
  /// is required). Updating one will unset the other automatically.
  public enum OneOf_Principal: Codable, Equatable, Sendable {
    /// user is the name of the user as seen by the kubernetes cluster, example
    /// "alice" or "alice@domain.tld"
    case user(Swift.String)
    /// group is the group, as seen by the kubernetes cluster.
    case group(Swift.String)
  }

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