// 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
#if canImport(FoundationNetworking)
  import FoundationNetworking
#endif
import GoogleCloudWkt
import GoogleLongRunning
import GoogleRpc
import GoogleCloudGax

/// Service to manage AssuredWorkloads.
///
/// @Snippet(path: "AssuredWorkloadsServiceQuickstart")
public class AssuredWorkloadsServiceClient: Clients.AssuredWorkloadsServiceProtocol {
  let inner: any Clients.AssuredWorkloadsServiceStub
  let pollingErrorPolicy: GoogleCloudGax.PollingErrorPolicy
  let pollingBackoffPolicy: GoogleCloudGax.BackoffPolicy

  /// Creates a new `AssuredWorkloadsServiceClient` instance.
  public init(_ options: GoogleCloudGax.ClientOptions = .init()) throws {
    var inner: any Clients.AssuredWorkloadsServiceStub =
      try Clients.AssuredWorkloadsServiceTransport(options)
    inner = Clients.AssuredWorkloadsServiceRetry(inner, options: options)
    if let logger = options.logger {
      inner = Clients.AssuredWorkloadsServiceLogging(inner, logger: logger)
    }
    self.inner = inner
    self.pollingErrorPolicy = options.pollingErrorPolicy
    self.pollingBackoffPolicy = options.pollingBackoffPolicy
  }

  /// Creates Assured Workload.
  ///
  /// @Snippet(path: "AssuredWorkloadsService_CreateWorkload")
  public func createWorkload(
    request: CreateWorkloadRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleLongRunning.Operation {
    try await self.inner.createWorkload(request: request, options: options)
  }

  /// Creates Assured Workload.
  ///
  /// @Snippet(path: "AssuredWorkloadsService_CreateWorkload")
  public func createWorkload(
    withPolling: CreateWorkloadRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> any GoogleCloudGax.PollableOperation<Workload> {
    let extractStatus = {
      (op: GoogleLongRunning.Operation) throws
        -> GoogleCloudGax._PollableOperationImpl<Workload>.State in
      guard op.done else {
        return .init(done: false, result: nil)
      }

      switch op.result {
      case .response(let anyValue):
        guard let anyValueUnwrapped = anyValue else {
          return .init(
            done: true,
            result: .failure(
              GoogleCloudGax.RequestError.binding(
                "Operation completed but response value was missing")))
        }
        let response = try Workload(fromAny: anyValueUnwrapped)
        return .init(done: true, result: .success(response))
      case .error(let status):
        guard let statusUnwrapped = status else {
          return .init(
            done: true,
            result: .failure(
              GoogleCloudGax.RequestError.binding("Operation completed but error value was missing")
            ))
        }
        let error = GoogleCloudGax.RequestError.service(
          GoogleCloudGax.ServiceError(
            code: GoogleRpc.Code(intValue: Int(statusUnwrapped.code)),
            message: statusUnwrapped.message))
        return .init(done: true, result: .failure(error))
      case .none:
        return .init(
          done: true,
          result: .failure(
            GoogleCloudGax.RequestError.binding("Operation completed but result was missing")))
      }
    }
    let rawOp = try await self.createWorkload(request: withPolling, options: options)
    let initialState = try extractStatus(rawOp)
    let poll = { () async throws -> GoogleCloudGax._PollableOperationImpl<Workload>.State in
      let op = try await self.getOperation(
        request: .init().with { $0.name = rawOp.name }, options: options)
      return try extractStatus(op)
    }
    return GoogleCloudGax._PollableOperationImpl(
      initialState: initialState,
      polling: options.pollingErrorPolicy ?? self.pollingErrorPolicy,
      backoff: options.pollingBackoffPolicy ?? self.pollingBackoffPolicy,
      poll: poll,
    )
  }

  /// Updates an existing workload.
  /// Currently allows updating of workload display_name and labels.
  /// For force updates don't set etag field in the Workload.
  /// Only one update operation per workload can be in progress.
  ///
  /// @Snippet(path: "AssuredWorkloadsService_UpdateWorkload")
  public func updateWorkload(
    request: UpdateWorkloadRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleCloudAssuredWorkloadsV1.Workload {
    try await self.inner.updateWorkload(request: request, options: options)
  }

  /// Restrict the list of resources allowed in the Workload environment.
  /// The current list of allowed products can be found at
  /// https://cloud.google.com/assured-workloads/docs/supported-products
  /// In addition to assuredworkloads.workload.update permission, the user should
  /// also have orgpolicy.policy.set permission on the folder resource
  /// to use this functionality.
  ///
  /// @Snippet(path: "AssuredWorkloadsService_RestrictAllowedResources")
  public func restrictAllowedResources(
    request: RestrictAllowedResourcesRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleCloudAssuredWorkloadsV1.RestrictAllowedResourcesResponse {
    try await self.inner.restrictAllowedResources(request: request, options: options)
  }

  /// Deletes the workload. Make sure that workload's direct children are already
  /// in a deleted state, otherwise the request will fail with a
  /// FAILED_PRECONDITION error.
  ///
  /// @Snippet(path: "AssuredWorkloadsService_DeleteWorkload")
  public func deleteWorkload(
    request: DeleteWorkloadRequest, options: GoogleCloudGax.RequestOptions
  ) async throws {
    try await self.inner.deleteWorkload(request: request, options: options)
  }

  /// Gets Assured Workload associated with a CRM Node
  ///
  /// @Snippet(path: "AssuredWorkloadsService_GetWorkload")
  public func getWorkload(
    request: GetWorkloadRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleCloudAssuredWorkloadsV1.Workload {
    try await self.inner.getWorkload(request: request, options: options)
  }

  /// Lists Assured Workloads under a CRM Node.
  ///
  /// @Snippet(path: "AssuredWorkloadsService_ListWorkloads")
  public func listWorkloads(
    request: ListWorkloadsRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleCloudAssuredWorkloadsV1.ListWorkloadsResponse {
    try await self.inner.listWorkloads(request: request, options: options)
  }

  /// Lists Assured Workloads under a CRM Node.
  ///
  /// @Snippet(path: "AssuredWorkloadsService_ListWorkloads")
  public func listWorkloads(
    byItem: ListWorkloadsRequest, options: GoogleCloudGax.RequestOptions
  ) throws -> any AsyncSequence<Workload, Swift.Error> {
    let listRpc = {
      (token: Swift.String) async throws -> GoogleCloudAssuredWorkloadsV1.ListWorkloadsResponse in
      var request = byItem
      request.pageToken = token
      return try await self.listWorkloads(request: request, options: options)
    }
    return GoogleCloudGax.PaginatedResponseSequence(listRpc: listRpc)
  }

  /// Provides the [Operations][google.longrunning.Operations] service functionality in this service.
  ///
  /// [google.longrunning.Operations]: https://www.google.com/search?q=Swift+google.longrunning+Operations
  ///
  /// @Snippet(path: "AssuredWorkloadsService_ListOperations")
  public func listOperations(
    request: GoogleLongRunning.ListOperationsRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleLongRunning.ListOperationsResponse {
    try await self.inner.listOperations(request: request, options: options)
  }

  /// Provides the [Operations][google.longrunning.Operations] service functionality in this service.
  ///
  /// [google.longrunning.Operations]: https://www.google.com/search?q=Swift+google.longrunning+Operations
  ///
  /// @Snippet(path: "AssuredWorkloadsService_ListOperations")
  public func listOperations(
    byItem: GoogleLongRunning.ListOperationsRequest, options: GoogleCloudGax.RequestOptions
  ) throws -> any AsyncSequence<GoogleLongRunning.Operation, Swift.Error> {
    let listRpc = {
      (token: Swift.String) async throws -> GoogleLongRunning.ListOperationsResponse in
      var request = byItem
      request.pageToken = token
      return try await self.listOperations(request: request, options: options)
    }
    return GoogleCloudGax.PaginatedResponseSequence(listRpc: listRpc)
  }

  /// Provides the [Operations][google.longrunning.Operations] service functionality in this service.
  ///
  /// [google.longrunning.Operations]: https://www.google.com/search?q=Swift+google.longrunning+Operations
  ///
  /// @Snippet(path: "AssuredWorkloadsService_GetOperation")
  func getOperation(
    request: GoogleLongRunning.GetOperationRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleLongRunning.Operation {
    try await self.inner.getOperation(request: request, options: options)
  }
}

extension Clients {
  /// A Swift protocol to mock `AssuredWorkloadsServiceClient`.
  ///
  /// To mock `AssuredWorkloadsServiceClient` change your functions to receive
  /// `some AssuredWorkloadsServiceProtocol` or `any AssuredWorkloadsServiceProtocol`
  /// and pass a mock implementation in your tests.
  public protocol AssuredWorkloadsServiceProtocol {
    /// See `AssuredWorkloadsServiceClient.createWorkload`.
    func createWorkload(request: CreateWorkloadRequest) async throws -> GoogleLongRunning.Operation

    /// See `AssuredWorkloadsServiceClient.createWorkload`.
    func createWorkload(withPolling: CreateWorkloadRequest) async throws -> any GoogleCloudGax
      .PollableOperation<Workload>

    /// See `AssuredWorkloadsServiceClient.createWorkload`.
    func createWorkload(
      parent: Swift.String,
      workload: Workload?,
    ) async throws -> any GoogleCloudGax.PollableOperation<Workload>

    /// See `AssuredWorkloadsServiceClient.updateWorkload`.
    func updateWorkload(request: UpdateWorkloadRequest) async throws
      -> GoogleCloudAssuredWorkloadsV1.Workload

    /// See `AssuredWorkloadsServiceClient.updateWorkload`.
    func updateWorkload(
      workload: Workload?,
      updateMask: GoogleCloudWkt.FieldMask?,
    ) async throws -> GoogleCloudAssuredWorkloadsV1.Workload

    /// See `AssuredWorkloadsServiceClient.restrictAllowedResources`.
    func restrictAllowedResources(request: RestrictAllowedResourcesRequest) async throws
      -> GoogleCloudAssuredWorkloadsV1.RestrictAllowedResourcesResponse

    /// See `AssuredWorkloadsServiceClient.deleteWorkload`.
    func deleteWorkload(request: DeleteWorkloadRequest) async throws

    /// See `AssuredWorkloadsServiceClient.deleteWorkload`.
    func deleteWorkload(
      name: Swift.String,
    ) async throws

    /// See `AssuredWorkloadsServiceClient.getWorkload`.
    func getWorkload(request: GetWorkloadRequest) async throws
      -> GoogleCloudAssuredWorkloadsV1.Workload

    /// See `AssuredWorkloadsServiceClient.getWorkload`.
    func getWorkload(
      name: Swift.String,
    ) async throws -> GoogleCloudAssuredWorkloadsV1.Workload

    /// See `AssuredWorkloadsServiceClient.listWorkloads`.
    func listWorkloads(request: ListWorkloadsRequest) async throws
      -> GoogleCloudAssuredWorkloadsV1.ListWorkloadsResponse

    /// See `AssuredWorkloadsServiceClient.listWorkloads`.
    func listWorkloads(
      byItem: ListWorkloadsRequest
    ) throws -> any AsyncSequence<Workload, Swift.Error>

    /// See `AssuredWorkloadsServiceClient.listWorkloads`.
    func listWorkloads(
      parent: Swift.String,
    ) throws -> any AsyncSequence<Workload, Swift.Error>

    /// See `AssuredWorkloadsServiceClient.listOperations`.
    func listOperations(request: GoogleLongRunning.ListOperationsRequest) async throws
      -> GoogleLongRunning.ListOperationsResponse

    /// See `AssuredWorkloadsServiceClient.listOperations`.
    func listOperations(
      byItem: GoogleLongRunning.ListOperationsRequest
    ) throws -> any AsyncSequence<GoogleLongRunning.Operation, Swift.Error>

    /// See `AssuredWorkloadsServiceClient.listOperations`.
    func listOperations(
      name: Swift.String,
      filter: Swift.String,
    ) throws -> any AsyncSequence<GoogleLongRunning.Operation, Swift.Error>

    /// See `AssuredWorkloadsServiceClient.createWorkload`.
    func createWorkload(
      request: CreateWorkloadRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleLongRunning.Operation

    /// See `AssuredWorkloadsServiceClient.createWorkload`.
    func createWorkload(
      withPolling: CreateWorkloadRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> any GoogleCloudGax.PollableOperation<Workload>

    /// See `AssuredWorkloadsServiceClient.updateWorkload`.
    func updateWorkload(
      request: UpdateWorkloadRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleCloudAssuredWorkloadsV1.Workload

    /// See `AssuredWorkloadsServiceClient.restrictAllowedResources`.
    func restrictAllowedResources(
      request: RestrictAllowedResourcesRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleCloudAssuredWorkloadsV1.RestrictAllowedResourcesResponse

    /// See `AssuredWorkloadsServiceClient.deleteWorkload`.
    func deleteWorkload(
      request: DeleteWorkloadRequest, options: GoogleCloudGax.RequestOptions
    ) async throws

    /// See `AssuredWorkloadsServiceClient.getWorkload`.
    func getWorkload(
      request: GetWorkloadRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleCloudAssuredWorkloadsV1.Workload

    /// See `AssuredWorkloadsServiceClient.listWorkloads`.
    func listWorkloads(
      request: ListWorkloadsRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleCloudAssuredWorkloadsV1.ListWorkloadsResponse

    /// See `AssuredWorkloadsServiceClient.listWorkloads`.
    func listWorkloads(
      byItem: ListWorkloadsRequest, options: GoogleCloudGax.RequestOptions
    ) throws -> any AsyncSequence<Workload, Swift.Error>

    /// See `AssuredWorkloadsServiceClient.listOperations`.
    func listOperations(
      request: GoogleLongRunning.ListOperationsRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleLongRunning.ListOperationsResponse

    /// See `AssuredWorkloadsServiceClient.listOperations`.
    func listOperations(
      byItem: GoogleLongRunning.ListOperationsRequest, options: GoogleCloudGax.RequestOptions
    ) throws -> any AsyncSequence<GoogleLongRunning.Operation, Swift.Error>
  }
}

// Default implementations
extension Clients.AssuredWorkloadsServiceProtocol {
  public func createWorkload(request: CreateWorkloadRequest) async throws
    -> GoogleLongRunning.Operation
  {
    try await self.createWorkload(request: request, options: .init())
  }

  public func createWorkload(
    request: CreateWorkloadRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleLongRunning.Operation {
    throw GoogleCloudGax.RequestError.unimplemented
  }

  public func createWorkload(withPolling: CreateWorkloadRequest) async throws -> any GoogleCloudGax
    .PollableOperation<Workload>
  {
    try await self.createWorkload(withPolling: withPolling, options: .init())
  }

  public func createWorkload(
    withPolling: CreateWorkloadRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> any GoogleCloudGax.PollableOperation<Workload> {
    let poll = { () async throws -> GoogleCloudGax._PollableOperationImpl<Workload>.State in
      throw GoogleCloudGax.RequestError.unimplemented
    }
    return GoogleCloudGax._PollableOperationImpl(
      initialState: .init(done: false, result: nil), poll: poll)
  }

  public func createWorkload(
    parent: Swift.String,
    workload: Workload?,
  ) async throws -> any GoogleCloudGax.PollableOperation<Workload> {
    let request = CreateWorkloadRequest().with {
      $0.parent = parent
      $0.workload = workload
    }
    return try await self.createWorkload(withPolling: request)
  }

  public func updateWorkload(request: UpdateWorkloadRequest) async throws
    -> GoogleCloudAssuredWorkloadsV1.Workload
  {
    try await self.updateWorkload(request: request, options: .init())
  }

  public func updateWorkload(
    request: UpdateWorkloadRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleCloudAssuredWorkloadsV1.Workload {
    throw GoogleCloudGax.RequestError.unimplemented
  }

  public func updateWorkload(
    workload: Workload?,
    updateMask: GoogleCloudWkt.FieldMask?,
  ) async throws -> GoogleCloudAssuredWorkloadsV1.Workload {
    let request = UpdateWorkloadRequest().with {
      $0.workload = workload
      $0.updateMask = updateMask
    }
    return try await self.updateWorkload(request: request)
  }

  public func restrictAllowedResources(request: RestrictAllowedResourcesRequest) async throws
    -> GoogleCloudAssuredWorkloadsV1.RestrictAllowedResourcesResponse
  {
    try await self.restrictAllowedResources(request: request, options: .init())
  }

  public func restrictAllowedResources(
    request: RestrictAllowedResourcesRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleCloudAssuredWorkloadsV1.RestrictAllowedResourcesResponse {
    throw GoogleCloudGax.RequestError.unimplemented
  }

  public func deleteWorkload(request: DeleteWorkloadRequest) async throws {
    try await self.deleteWorkload(request: request, options: .init())
  }

  public func deleteWorkload(
    request: DeleteWorkloadRequest, options: GoogleCloudGax.RequestOptions
  ) async throws {
    throw GoogleCloudGax.RequestError.unimplemented
  }

  public func deleteWorkload(
    name: Swift.String,
  ) async throws {
    let request = DeleteWorkloadRequest().with {
      $0.name = name
    }
    try await self.deleteWorkload(request: request)
  }

  public func getWorkload(request: GetWorkloadRequest) async throws
    -> GoogleCloudAssuredWorkloadsV1.Workload
  {
    try await self.getWorkload(request: request, options: .init())
  }

  public func getWorkload(
    request: GetWorkloadRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleCloudAssuredWorkloadsV1.Workload {
    throw GoogleCloudGax.RequestError.unimplemented
  }

  public func getWorkload(
    name: Swift.String,
  ) async throws -> GoogleCloudAssuredWorkloadsV1.Workload {
    let request = GetWorkloadRequest().with {
      $0.name = name
    }
    return try await self.getWorkload(request: request)
  }

  public func listWorkloads(request: ListWorkloadsRequest) async throws
    -> GoogleCloudAssuredWorkloadsV1.ListWorkloadsResponse
  {
    try await self.listWorkloads(request: request, options: .init())
  }

  public func listWorkloads(
    request: ListWorkloadsRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleCloudAssuredWorkloadsV1.ListWorkloadsResponse {
    throw GoogleCloudGax.RequestError.unimplemented
  }

  public func listWorkloads(
    byItem: ListWorkloadsRequest
  ) throws -> any AsyncSequence<Workload, Swift.Error> {
    try self.listWorkloads(byItem: byItem, options: .init())
  }

  public func listWorkloads(
    byItem: ListWorkloadsRequest, options: GoogleCloudGax.RequestOptions
  ) throws -> any AsyncSequence<Workload, Swift.Error> {
    let listRpc = {
      (token: Swift.String) async throws -> GoogleCloudAssuredWorkloadsV1.ListWorkloadsResponse in
      throw GoogleCloudGax.RequestError.unimplemented
    }
    return GoogleCloudGax.PaginatedResponseSequence(listRpc: listRpc)
  }

  public func listWorkloads(
    parent: Swift.String,
  ) throws -> any AsyncSequence<Workload, Swift.Error> {
    let request = ListWorkloadsRequest().with {
      $0.parent = parent
    }
    return try self.listWorkloads(byItem: request)
  }

  public func listOperations(request: GoogleLongRunning.ListOperationsRequest) async throws
    -> GoogleLongRunning.ListOperationsResponse
  {
    try await self.listOperations(request: request, options: .init())
  }

  public func listOperations(
    request: GoogleLongRunning.ListOperationsRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleLongRunning.ListOperationsResponse {
    throw GoogleCloudGax.RequestError.unimplemented
  }

  public func listOperations(
    byItem: GoogleLongRunning.ListOperationsRequest
  ) throws -> any AsyncSequence<GoogleLongRunning.Operation, Swift.Error> {
    try self.listOperations(byItem: byItem, options: .init())
  }

  public func listOperations(
    byItem: GoogleLongRunning.ListOperationsRequest, options: GoogleCloudGax.RequestOptions
  ) throws -> any AsyncSequence<GoogleLongRunning.Operation, Swift.Error> {
    let listRpc = {
      (token: Swift.String) async throws -> GoogleLongRunning.ListOperationsResponse in
      throw GoogleCloudGax.RequestError.unimplemented
    }
    return GoogleCloudGax.PaginatedResponseSequence(listRpc: listRpc)
  }

  public func listOperations(
    name: Swift.String,
    filter: Swift.String,
  ) throws -> any AsyncSequence<GoogleLongRunning.Operation, Swift.Error> {
    let request = GoogleLongRunning.ListOperationsRequest().with {
      $0.name = name
      $0.filter = filter
    }
    return try self.listOperations(byItem: request)
  }

  public func getOperation(request: GoogleLongRunning.GetOperationRequest) async throws
    -> GoogleLongRunning.Operation
  {
    try await self.getOperation(request: request, options: .init())
  }

  public func getOperation(
    request: GoogleLongRunning.GetOperationRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleLongRunning.Operation {
    throw GoogleCloudGax.RequestError.unimplemented
  }

  public func getOperation(
    name: Swift.String,
  ) async throws -> GoogleLongRunning.Operation {
    let request = GoogleLongRunning.GetOperationRequest().with {
      $0.name = name
    }
    return try await self.getOperation(request: request)
  }
}
