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

/// Job represents an operation for a `Rollout`.
public struct Job: Codable, Equatable, GoogleCloudWkt._AnyPackable,
  Sendable
{
  /// Output only. The ID of the Job.
  public var id: Swift.String = Swift.String()

  /// Output only. The current state of the Job.
  public var state: Job.State = Job.State()

  /// Output only. Additional information on why the Job was skipped, if
  /// available.
  public var skipMessage: Swift.String = Swift.String()

  /// Output only. The name of the `JobRun` responsible for the most recent
  /// invocation of this Job.
  public var jobRun: Swift.String = Swift.String()

  /// The type of Job.
  public var jobType: OneOf_JobType? = nil

  /// Initialize a new instance of `Job`.
  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 = Job().with { $0.id = ... }
  /// ```
  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 id = "id"
    case state = "state"
    case skipMessage = "skipMessage"
    case jobRun = "jobRun"
    case deployJob = "deployJob"
    case verifyJob = "verifyJob"
    case predeployJob = "predeployJob"
    case postdeployJob = "postdeployJob"
    case createChildRolloutJob = "createChildRolloutJob"
    case advanceChildRolloutJob = "advanceChildRolloutJob"
  }

  public init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    self.id = try container.decode(Swift.String.self, forKey: .id)
    self.state = try container.decode(Job.State.self, forKey: .state)
    self.skipMessage = try container.decode(Swift.String.self, forKey: .skipMessage)
    self.jobRun = try container.decode(Swift.String.self, forKey: .jobRun)

    var jobType: OneOf_JobType? = nil
    let jobTypeCheckAndSet = {
      if jobType != nil {
        throw DecodingError.dataCorrupted(
          DecodingError.Context(
            codingPath: decoder.codingPath,
            debugDescription: "Multiple values set for oneof 'jobType'"))
      }
      jobType = $0
    }
    if let deployJob = try container.decodeIfPresent(DeployJob?.self, forKey: .deployJob) {
      try jobTypeCheckAndSet(.deployJob(deployJob))
    }
    if let verifyJob = try container.decodeIfPresent(VerifyJob?.self, forKey: .verifyJob) {
      try jobTypeCheckAndSet(.verifyJob(verifyJob))
    }
    if let predeployJob = try container.decodeIfPresent(PredeployJob?.self, forKey: .predeployJob) {
      try jobTypeCheckAndSet(.predeployJob(predeployJob))
    }
    if let postdeployJob = try container.decodeIfPresent(
      PostdeployJob?.self, forKey: .postdeployJob)
    {
      try jobTypeCheckAndSet(.postdeployJob(postdeployJob))
    }
    if let createChildRolloutJob = try container.decodeIfPresent(
      CreateChildRolloutJob?.self, forKey: .createChildRolloutJob)
    {
      try jobTypeCheckAndSet(.createChildRolloutJob(createChildRolloutJob))
    }
    if let advanceChildRolloutJob = try container.decodeIfPresent(
      AdvanceChildRolloutJob?.self, forKey: .advanceChildRolloutJob)
    {
      try jobTypeCheckAndSet(.advanceChildRolloutJob(advanceChildRolloutJob))
    }
    self.jobType = jobType
  }

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

    if let choice = self.jobType {
      switch choice {
      case .deployJob(let value):
        try container.encode(value, forKey: .deployJob)
      case .verifyJob(let value):
        try container.encode(value, forKey: .verifyJob)
      case .predeployJob(let value):
        try container.encode(value, forKey: .predeployJob)
      case .postdeployJob(let value):
        try container.encode(value, forKey: .postdeployJob)
      case .createChildRolloutJob(let value):
        try container.encode(value, forKey: .createChildRolloutJob)
      case .advanceChildRolloutJob(let value):
        try container.encode(value, forKey: .advanceChildRolloutJob)
      }
    }
  }

  /// Valid states of a Job.
  public enum State: Codable, Equatable, Sendable {
    /// The Job has an unspecified state.
    case unspecified
    /// The Job is waiting for an earlier Phase(s) or Job(s) to complete.
    case pending
    /// The Job is disabled.
    case disabled
    /// The Job is in progress.
    case inProgress
    /// The Job succeeded.
    case succeeded
    /// The Job failed.
    case failed
    /// The Job was aborted.
    case aborted
    /// The Job was skipped.
    case skipped
    /// The Job was ignored.
    case ignored
    /// 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 .pending: return 1
      case .disabled: return 2
      case .inProgress: return 3
      case .succeeded: return 4
      case .failed: return 5
      case .aborted: return 6
      case .skipped: return 7
      case .ignored: return 8
      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 "STATE_UNSPECIFIED"
      case .pending: return "PENDING"
      case .disabled: return "DISABLED"
      case .inProgress: return "IN_PROGRESS"
      case .succeeded: return "SUCCEEDED"
      case .failed: return "FAILED"
      case .aborted: return "ABORTED"
      case .skipped: return "SKIPPED"
      case .ignored: return "IGNORED"
      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:State/unknownStringValue(_:)).
    public init(stringValue: Swift.String) {
      switch stringValue {
      case "STATE_UNSPECIFIED": self = .unspecified
      case "PENDING": self = .pending
      case "DISABLED": self = .disabled
      case "IN_PROGRESS": self = .inProgress
      case "SUCCEEDED": self = .succeeded
      case "FAILED": self = .failed
      case "ABORTED": self = .aborted
      case "SKIPPED": self = .skipped
      case "IGNORED": self = .ignored
      default: self = .unknownStringValue(stringValue)
      }
    }

    /// Initialize from an integer value.
    ///
    /// If the value is unknown, this initializes to [`unknownIntValue`](doc:State/unknownIntValue(_:)).
    public init(intValue: Int) {
      switch intValue {
      case 0: self = .unspecified
      case 1: self = .pending
      case 2: self = .disabled
      case 3: self = .inProgress
      case 4: self = .succeeded
      case 5: self = .failed
      case 6: self = .aborted
      case 7: self = .skipped
      case 8: self = .ignored
      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 .pending: return try container.encode(1)
      case .disabled: return try container.encode(2)
      case .inProgress: return try container.encode(3)
      case .succeeded: return try container.encode(4)
      case .failed: return try container.encode(5)
      case .aborted: return try container.encode(6)
      case .skipped: return try container.encode(7)
      case .ignored: return try container.encode(8)
      case .unknownIntValue(let v): return try container.encode(v)
      case .unknownStringValue(let v): return try container.encode(v)
      }
    }
  }

  /// The type of Job.
  public enum OneOf_JobType: Codable, Equatable, Sendable {
    /// Output only. A deploy Job.
    indirect case deployJob(DeployJob?)
    /// Output only. A verify Job.
    indirect case verifyJob(VerifyJob?)
    /// Output only. A predeploy Job.
    indirect case predeployJob(PredeployJob?)
    /// Output only. A postdeploy Job.
    indirect case postdeployJob(PostdeployJob?)
    /// Output only. A createChildRollout Job.
    indirect case createChildRolloutJob(CreateChildRolloutJob?)
    /// Output only. An advanceChildRollout Job.
    indirect case advanceChildRolloutJob(AdvanceChildRolloutJob?)
  }

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