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

/// Configuration for a job.
/// The maximum allowed size for a job is 1MB.
public struct Job: Codable, Equatable, GoogleCloudWkt._AnyPackable,
  Sendable
{
  /// Optionally caller-specified in
  /// [CreateJob][google.cloud.scheduler.v1.CloudScheduler.CreateJob], after
  /// which it becomes output only.
  ///
  /// The job name. For example:
  /// `projects/PROJECT_ID/locations/LOCATION_ID/jobs/JOB_ID`.
  ///
  /// * `PROJECT_ID` can contain letters ([A-Za-z]), numbers ([0-9]),
  ///    hyphens (-), colons (:), or periods (.).
  ///    For more information, see
  ///    [Identifying
  ///    projects](https://cloud.google.com/resource-manager/docs/creating-managing-projects#identifying_projects)
  /// * `LOCATION_ID` is the canonical ID for the job's location.
  ///    The list of available locations can be obtained by calling
  ///    [ListLocations][google.cloud.location.Locations.ListLocations].
  ///    For more information, see https://cloud.google.com/about/locations/.
  /// * `JOB_ID` can contain only letters ([A-Za-z]), numbers ([0-9]),
  ///    hyphens (-), or underscores (_). The maximum length is 500 characters.
  ///
  /// [google.cloud.scheduler.v1.CloudScheduler.CreateJob]: <doc:CloudScheduler/createJob(request:)>
  public var name: Swift.String = Swift.String()

  /// Optionally caller-specified in
  /// [CreateJob][google.cloud.scheduler.v1.CloudScheduler.CreateJob] or
  /// [UpdateJob][google.cloud.scheduler.v1.CloudScheduler.UpdateJob].
  ///
  /// A human-readable description for the job. This string must not contain
  /// more than 500 characters.
  ///
  /// [google.cloud.scheduler.v1.CloudScheduler.CreateJob]: <doc:CloudScheduler/createJob(request:)>
  /// [google.cloud.scheduler.v1.CloudScheduler.UpdateJob]: <doc:CloudScheduler/updateJob(request:)>
  public var description: Swift.String = Swift.String()

  /// Required, except when used with
  /// [UpdateJob][google.cloud.scheduler.v1.CloudScheduler.UpdateJob].
  ///
  /// Describes the schedule on which the job will be executed.
  ///
  /// The schedule can be either of the following types:
  ///
  /// * [Crontab](https://en.wikipedia.org/wiki/Cron#Overview)
  /// * English-like
  /// [schedule](https://cloud.google.com/scheduler/docs/configuring/cron-job-schedules)
  ///
  /// As a general rule, execution `n + 1` of a job will not begin
  /// until execution `n` has finished. Cloud Scheduler will never
  /// allow two simultaneously outstanding executions. For example,
  /// this implies that if the `n+1`th execution is scheduled to run at
  /// 16:00 but the `n`th execution takes until 16:15, the `n+1`th
  /// execution will not start until `16:15`.
  /// A scheduled start time will be delayed if the previous
  /// execution has not ended when its scheduled time occurs.
  ///
  /// If [retry_count][google.cloud.scheduler.v1.RetryConfig.retry_count] > 0 and
  /// a job attempt fails, the job will be tried a total of
  /// [retry_count][google.cloud.scheduler.v1.RetryConfig.retry_count] times,
  /// with exponential backoff, until the next scheduled start time. If
  /// retry_count is 0, a job attempt will not be retried if it fails. Instead
  /// the Cloud Scheduler system will wait for the next scheduled execution time.
  /// Setting retry_count to 0 does not prevent failed jobs from running
  /// according to schedule after the failure.
  ///
  /// [google.cloud.scheduler.v1.CloudScheduler.UpdateJob]: <doc:CloudScheduler/updateJob(request:)>
  /// [google.cloud.scheduler.v1.RetryConfig.retry_count]: <doc:RetryConfig/retryCount>
  public var schedule: Swift.String = Swift.String()

  /// Specifies the time zone to be used in interpreting
  /// [schedule][google.cloud.scheduler.v1.Job.schedule]. The value of this field
  /// must be a time zone name from the [tz
  /// database](http://en.wikipedia.org/wiki/Tz_database).
  ///
  /// Note that some time zones include a provision for
  /// daylight savings time. The rules for daylight saving time are
  /// determined by the chosen tz. For UTC use the string "utc". If a
  /// time zone is not specified, the default will be in UTC (also known
  /// as GMT).
  ///
  /// [google.cloud.scheduler.v1.Job.schedule]: <doc:Job/schedule>
  public var timeZone: Swift.String = Swift.String()

  /// Output only. The creation time of the job.
  public var userUpdateTime: GoogleCloudWkt.Timestamp? = nil

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

  /// Output only. The response from the target for the last attempted execution.
  public var status: GoogleRpc.Status? = nil

  /// Output only. The next time the job is scheduled. Note that this may be a
  /// retry of a previously failed attempt or the next execution time
  /// according to the schedule.
  public var scheduleTime: GoogleCloudWkt.Timestamp? = nil

  /// Output only. The time the last job attempt started.
  public var lastAttemptTime: GoogleCloudWkt.Timestamp? = nil

  /// Settings that determine the retry behavior.
  public var retryConfig: RetryConfig? = nil

  /// The deadline for job attempts. If the request handler does not respond by
  /// this deadline then the request is cancelled and the attempt is marked as a
  /// `DEADLINE_EXCEEDED` failure. The failed attempt can be viewed in
  /// execution logs. Cloud Scheduler will retry the job according
  /// to the [RetryConfig][google.cloud.scheduler.v1.RetryConfig].
  ///
  /// The default and the allowed values depend on the type of target:
  ///
  /// * For [HTTP targets][google.cloud.scheduler.v1.Job.http_target], the
  /// default is 3 minutes. The deadline must be in the interval [15 seconds, 30
  /// minutes].
  ///
  /// * For [App Engine HTTP
  /// targets][google.cloud.scheduler.v1.Job.app_engine_http_target], 0 indicates
  /// that the request has the default deadline. The default deadline depends on
  /// the scaling type of the service: 10 minutes for standard apps with
  /// automatic scaling, 24 hours for standard apps with manual and basic
  /// scaling, and 60 minutes for flex apps. If the request deadline is set, it
  /// must be in the interval [15 seconds, 24 hours 15 seconds].
  ///
  /// * For [Pub/Sub targets][google.cloud.scheduler.v1.Job.pubsub_target], this
  /// field is ignored.
  ///
  /// [google.cloud.scheduler.v1.Job.app_engine_http_target]: <doc:Job/appEngineHttpTarget>
  /// [google.cloud.scheduler.v1.Job.http_target]: <doc:Job/httpTarget>
  /// [google.cloud.scheduler.v1.Job.pubsub_target]: <doc:Job/pubsubTarget>
  /// [google.cloud.scheduler.v1.RetryConfig]: <doc:RetryConfig>
  public var attemptDeadline: GoogleCloudWkt.Duration? = nil

  /// Required.
  ///
  /// Delivery settings containing destination and parameters.
  public var target: OneOf_Target? = 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.name = ... }
  /// ```
  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 name = "name"
    case description = "description"
    case pubsubTarget = "pubsubTarget"
    case appEngineHttpTarget = "appEngineHttpTarget"
    case httpTarget = "httpTarget"
    case schedule = "schedule"
    case timeZone = "timeZone"
    case userUpdateTime = "userUpdateTime"
    case state = "state"
    case status = "status"
    case scheduleTime = "scheduleTime"
    case lastAttemptTime = "lastAttemptTime"
    case retryConfig = "retryConfig"
    case attemptDeadline = "attemptDeadline"
  }

  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.description = try container.decode(Swift.String.self, forKey: .description)
    self.schedule = try container.decode(Swift.String.self, forKey: .schedule)
    self.timeZone = try container.decode(Swift.String.self, forKey: .timeZone)
    self.userUpdateTime = try container.decodeIfPresent(
      GoogleCloudWkt.Timestamp.self, forKey: .userUpdateTime)
    self.state = try container.decode(Job.State.self, forKey: .state)
    self.status = try container.decodeIfPresent(GoogleRpc.Status.self, forKey: .status)
    self.scheduleTime = try container.decodeIfPresent(
      GoogleCloudWkt.Timestamp.self, forKey: .scheduleTime)
    self.lastAttemptTime = try container.decodeIfPresent(
      GoogleCloudWkt.Timestamp.self, forKey: .lastAttemptTime)
    self.retryConfig = try container.decodeIfPresent(RetryConfig.self, forKey: .retryConfig)
    self.attemptDeadline = try container.decodeIfPresent(
      GoogleCloudWkt.Duration.self, forKey: .attemptDeadline)

    var target: OneOf_Target? = nil
    let targetCheckAndSet = {
      if target != nil {
        throw DecodingError.dataCorrupted(
          DecodingError.Context(
            codingPath: decoder.codingPath,
            debugDescription: "Multiple values set for oneof 'target'"))
      }
      target = $0
    }
    if let pubsubTarget = try container.decodeIfPresent(PubsubTarget?.self, forKey: .pubsubTarget) {
      try targetCheckAndSet(.pubsubTarget(pubsubTarget))
    }
    if let appEngineHttpTarget = try container.decodeIfPresent(
      AppEngineHttpTarget?.self, forKey: .appEngineHttpTarget)
    {
      try targetCheckAndSet(.appEngineHttpTarget(appEngineHttpTarget))
    }
    if let httpTarget = try container.decodeIfPresent(HttpTarget?.self, forKey: .httpTarget) {
      try targetCheckAndSet(.httpTarget(httpTarget))
    }
    self.target = target
  }

  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.description, forKey: .description)
    try container.encode(self.schedule, forKey: .schedule)
    try container.encode(self.timeZone, forKey: .timeZone)
    try container.encode(self.userUpdateTime, forKey: .userUpdateTime)
    try container.encode(self.state, forKey: .state)
    try container.encode(self.status, forKey: .status)
    try container.encode(self.scheduleTime, forKey: .scheduleTime)
    try container.encode(self.lastAttemptTime, forKey: .lastAttemptTime)
    try container.encode(self.retryConfig, forKey: .retryConfig)
    try container.encode(self.attemptDeadline, forKey: .attemptDeadline)

    if let choice = self.target {
      switch choice {
      case .pubsubTarget(let value):
        try container.encode(value, forKey: .pubsubTarget)
      case .appEngineHttpTarget(let value):
        try container.encode(value, forKey: .appEngineHttpTarget)
      case .httpTarget(let value):
        try container.encode(value, forKey: .httpTarget)
      }
    }
  }

  /// State of the job.
  public enum State: Codable, Equatable, Sendable {
    /// Unspecified state.
    case unspecified
    /// The job is executing normally.
    case enabled
    /// The job is paused by the user. It will not execute. A user can
    /// intentionally pause the job using
    /// [PauseJobRequest][google.cloud.scheduler.v1.PauseJobRequest].
    ///
    /// [google.cloud.scheduler.v1.PauseJobRequest]: <doc:PauseJobRequest>
    case paused
    /// The job is disabled by the system due to error. The user
    /// cannot directly set a job to be disabled.
    case disabled
    /// The job state resulting from a failed
    /// [CloudScheduler.UpdateJob][google.cloud.scheduler.v1.CloudScheduler.UpdateJob]
    /// operation. To recover a job from this state, retry
    /// [CloudScheduler.UpdateJob][google.cloud.scheduler.v1.CloudScheduler.UpdateJob]
    /// until a successful response is received.
    ///
    /// [google.cloud.scheduler.v1.CloudScheduler.UpdateJob]: <doc:CloudScheduler/updateJob(request:)>
    case updateFailed
    /// 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 .enabled: return 1
      case .paused: return 2
      case .disabled: return 3
      case .updateFailed: 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 .unspecified: return "STATE_UNSPECIFIED"
      case .enabled: return "ENABLED"
      case .paused: return "PAUSED"
      case .disabled: return "DISABLED"
      case .updateFailed: return "UPDATE_FAILED"
      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 "ENABLED": self = .enabled
      case "PAUSED": self = .paused
      case "DISABLED": self = .disabled
      case "UPDATE_FAILED": self = .updateFailed
      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 = .enabled
      case 2: self = .paused
      case 3: self = .disabled
      case 4: self = .updateFailed
      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 .enabled: return try container.encode(1)
      case .paused: return try container.encode(2)
      case .disabled: return try container.encode(3)
      case .updateFailed: return try container.encode(4)
      case .unknownIntValue(let v): return try container.encode(v)
      case .unknownStringValue(let v): return try container.encode(v)
      }
    }
  }

  /// Required.
  ///
  /// Delivery settings containing destination and parameters.
  public enum OneOf_Target: Codable, Equatable, Sendable {
    /// Pub/Sub target.
    indirect case pubsubTarget(PubsubTarget?)
    /// App Engine HTTP target.
    indirect case appEngineHttpTarget(AppEngineHttpTarget?)
    /// HTTP target.
    indirect case httpTarget(HttpTarget?)
  }

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