# frozen_string_literal: false

# ----------------------------------------------------------------------------
#
#     ***     AUTO GENERATED CODE    ***    Type: MMv1     ***
#
# ----------------------------------------------------------------------------
#
#     This file is automatically generated by Magic Modules and manual
#     changes will be clobbered when the file is regenerated.
#
#     Please read more about how to change this file in README.md and
#     CONTRIBUTING.md located at the root of this package.
#
# ----------------------------------------------------------------------------
require 'gcp_backend'
class VertexAIBatchPredictionJobs < GcpResourceBase
  name 'google_vertex_ai_batch_prediction_jobs'
  desc 'BatchPredictionJob plural resource'
  supports platform: 'gcp'

  attr_reader :table

  filter_table_config = FilterTable.create

  filter_table_config.add(:create_times, field: :create_time)
  filter_table_config.add(:model_parameters, field: :model_parameters)
  filter_table_config.add(:instance_configs, field: :instance_config)
  filter_table_config.add(:model_version_ids, field: :model_version_id)
  filter_table_config.add(:dedicated_resources, field: :dedicated_resources)
  filter_table_config.add(:output_infos, field: :output_info)
  filter_table_config.add(:disable_container_loggings, field: :disable_container_logging)
  filter_table_config.add(:explanation_specs, field: :explanation_spec)
  filter_table_config.add(:end_times, field: :end_time)
  filter_table_config.add(:generate_explanations, field: :generate_explanation)
  filter_table_config.add(:resources_consumeds, field: :resources_consumed)
  filter_table_config.add(:errors, field: :error)
  filter_table_config.add(:input_configs, field: :input_config)
  filter_table_config.add(:unmanaged_container_models, field: :unmanaged_container_model)
  filter_table_config.add(:completion_stats, field: :completion_stats)
  filter_table_config.add(:start_times, field: :start_time)
  filter_table_config.add(:manual_batch_tuning_parameters, field: :manual_batch_tuning_parameters)
  filter_table_config.add(:update_times, field: :update_time)
  filter_table_config.add(:names, field: :name)
  filter_table_config.add(:labels, field: :labels)
  filter_table_config.add(:states, field: :state)
  filter_table_config.add(:encryption_specs, field: :encryption_spec)
  filter_table_config.add(:partial_failures, field: :partial_failures)
  filter_table_config.add(:models, field: :model)
  filter_table_config.add(:output_configs, field: :output_config)
  filter_table_config.add(:display_names, field: :display_name)
  filter_table_config.add(:service_accounts, field: :service_account)

  filter_table_config.connect(self, :table)

  def initialize(params = {})
    super(params.merge({ use_http_transport: true }))
    @params = params
    @table = fetch_wrapped_resource('batchPredictionJobs')
  end

  def fetch_wrapped_resource(wrap_path)
    # fetch_resource returns an array of responses (to handle pagination)
    result = @connection.fetch_all(product_url, resource_base_url, @params, 'Get')
    return if result.nil?

    # Conversion of string -> object hash to symbol -> object hash that InSpec needs
    converted = []
    result.each do |response|
      next if response.nil? || !response.key?(wrap_path)
      response[wrap_path].each do |hash|
        hash_with_symbols = {}
        hash.each_key do |key|
          name, value = transform(key, hash)
          hash_with_symbols[name] = value
        end
        converted.push(hash_with_symbols)
      end
    end

    converted
  end

  def transform(key, value)
    return transformers[key].call(value) if transformers.key?(key)

    [key.to_sym, value]
  end

  def transformers
    {
      'createTime' => ->(obj) { [:create_time, obj['createTime']] },
      'modelParameters' => ->(obj) { [:model_parameters, obj['modelParameters']] },
      'instanceConfig' => ->(obj) { [:instance_config, GoogleInSpec::VertexAI::Property::BatchPredictionJobInstanceConfig.new(obj['instanceConfig'], to_s)] },
      'modelVersionId' => ->(obj) { [:model_version_id, obj['modelVersionId']] },
      'dedicatedResources' => ->(obj) { [:dedicated_resources, GoogleInSpec::VertexAI::Property::BatchPredictionJobDedicatedResources.new(obj['dedicatedResources'], to_s)] },
      'outputInfo' => ->(obj) { [:output_info, GoogleInSpec::VertexAI::Property::BatchPredictionJobOutputInfo.new(obj['outputInfo'], to_s)] },
      'disableContainerLogging' => ->(obj) { [:disable_container_logging, obj['disableContainerLogging']] },
      'explanationSpec' => ->(obj) { [:explanation_spec, GoogleInSpec::VertexAI::Property::BatchPredictionJobExplanationSpec.new(obj['explanationSpec'], to_s)] },
      'endTime' => ->(obj) { [:end_time, obj['endTime']] },
      'generateExplanation' => ->(obj) { [:generate_explanation, obj['generateExplanation']] },
      'resourcesConsumed' => ->(obj) { [:resources_consumed, GoogleInSpec::VertexAI::Property::BatchPredictionJobResourcesConsumed.new(obj['resourcesConsumed'], to_s)] },
      'error' => ->(obj) { [:error, GoogleInSpec::VertexAI::Property::BatchPredictionJobError.new(obj['error'], to_s)] },
      'inputConfig' => ->(obj) { [:input_config, GoogleInSpec::VertexAI::Property::BatchPredictionJobInputConfig.new(obj['inputConfig'], to_s)] },
      'unmanagedContainerModel' => ->(obj) { [:unmanaged_container_model, GoogleInSpec::VertexAI::Property::BatchPredictionJobUnmanagedContainerModel.new(obj['unmanagedContainerModel'], to_s)] },
      'completionStats' => ->(obj) { [:completion_stats, GoogleInSpec::VertexAI::Property::BatchPredictionJobCompletionStats.new(obj['completionStats'], to_s)] },
      'startTime' => ->(obj) { [:start_time, obj['startTime']] },
      'manualBatchTuningParameters' => ->(obj) { [:manual_batch_tuning_parameters, GoogleInSpec::VertexAI::Property::BatchPredictionJobManualBatchTuningParameters.new(obj['manualBatchTuningParameters'], to_s)] },
      'updateTime' => ->(obj) { [:update_time, obj['updateTime']] },
      'name' => ->(obj) { [:name, obj['name']] },
      'labels' => ->(obj) { [:labels, GoogleInSpec::VertexAI::Property::BatchPredictionJobLabels.new(obj['labels'], to_s)] },
      'state' => ->(obj) { [:state, obj['state']] },
      'encryptionSpec' => ->(obj) { [:encryption_spec, GoogleInSpec::VertexAI::Property::BatchPredictionJobEncryptionSpec.new(obj['encryptionSpec'], to_s)] },
      'partialFailures' => ->(obj) { [:partial_failures, GoogleInSpec::VertexAI::Property::BatchPredictionJobPartialFailuresArray.parse(obj['partialFailures'], to_s)] },
      'model' => ->(obj) { [:model, obj['model']] },
      'outputConfig' => ->(obj) { [:output_config, GoogleInSpec::VertexAI::Property::BatchPredictionJobOutputConfig.new(obj['outputConfig'], to_s)] },
      'displayName' => ->(obj) { [:display_name, obj['displayName']] },
      'serviceAccount' => ->(obj) { [:service_account, obj['serviceAccount']] },
    }
  end

  private

  def product_url(_ = nil)
    'https://{{region}}-aiplatform.googleapis.com/v1/'
  end

  def resource_base_url
    '{{parent}}/batchPredictionJobs'
  end
end
