# 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 VertexAIModels < GcpResourceBase
  name 'google_vertex_ai_models'
  desc 'Model plural resource'
  supports platform: 'gcp'

  attr_reader :table

  filter_table_config = FilterTable.create

  filter_table_config.add(:model_source_infos, field: :model_source_info)
  filter_table_config.add(:names, field: :name)
  filter_table_config.add(:metadata, field: :metadata)
  filter_table_config.add(:update_times, field: :update_time)
  filter_table_config.add(:etags, field: :etag)
  filter_table_config.add(:descriptions, field: :description)
  filter_table_config.add(:deployed_models, field: :deployed_models)
  filter_table_config.add(:create_times, field: :create_time)
  filter_table_config.add(:explanation_specs, field: :explanation_spec)
  filter_table_config.add(:encryption_specs, field: :encryption_spec)
  filter_table_config.add(:pipeline_jobs, field: :pipeline_job)
  filter_table_config.add(:predict_schemata, field: :predict_schemata)
  filter_table_config.add(:version_update_times, field: :version_update_time)
  filter_table_config.add(:supported_export_formats, field: :supported_export_formats)
  filter_table_config.add(:original_model_infos, field: :original_model_info)
  filter_table_config.add(:metadata_artifacts, field: :metadata_artifact)
  filter_table_config.add(:supported_input_storage_formats, field: :supported_input_storage_formats)
  filter_table_config.add(:metadata_schema_uris, field: :metadata_schema_uri)
  filter_table_config.add(:container_specs, field: :container_spec)
  filter_table_config.add(:version_ids, field: :version_id)
  filter_table_config.add(:artifact_uris, field: :artifact_uri)
  filter_table_config.add(:training_pipelines, field: :training_pipeline)
  filter_table_config.add(:display_names, field: :display_name)
  filter_table_config.add(:supported_deployment_resources_types, field: :supported_deployment_resources_types)
  filter_table_config.add(:supported_output_storage_formats, field: :supported_output_storage_formats)
  filter_table_config.add(:version_aliases, field: :version_aliases)
  filter_table_config.add(:version_create_times, field: :version_create_time)
  filter_table_config.add(:version_descriptions, field: :version_description)
  filter_table_config.add(:labels, field: :labels)

  filter_table_config.connect(self, :table)

  def initialize(params = {})
    super(params.merge({ use_http_transport: true }))
    @params = params
    @table = fetch_wrapped_resource('models')
  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
    {
      'modelSourceInfo' => ->(obj) { [:model_source_info, GoogleInSpec::VertexAI::Property::ModelModelSourceInfo.new(obj['modelSourceInfo'], to_s)] },
      'name' => ->(obj) { [:name, obj['name']] },
      'metadata' => ->(obj) { [:metadata, obj['metadata']] },
      'updateTime' => ->(obj) { [:update_time, obj['updateTime']] },
      'etag' => ->(obj) { [:etag, obj['etag']] },
      'description' => ->(obj) { [:description, obj['description']] },
      'deployedModels' => ->(obj) { [:deployed_models, GoogleInSpec::VertexAI::Property::ModelDeployedModelsArray.parse(obj['deployedModels'], to_s)] },
      'createTime' => ->(obj) { [:create_time, obj['createTime']] },
      'explanationSpec' => ->(obj) { [:explanation_spec, GoogleInSpec::VertexAI::Property::ModelExplanationSpec.new(obj['explanationSpec'], to_s)] },
      'encryptionSpec' => ->(obj) { [:encryption_spec, GoogleInSpec::VertexAI::Property::ModelEncryptionSpec.new(obj['encryptionSpec'], to_s)] },
      'pipelineJob' => ->(obj) { [:pipeline_job, obj['pipelineJob']] },
      'predictSchemata' => ->(obj) { [:predict_schemata, GoogleInSpec::VertexAI::Property::ModelPredictSchemata.new(obj['predictSchemata'], to_s)] },
      'versionUpdateTime' => ->(obj) { [:version_update_time, obj['versionUpdateTime']] },
      'supportedExportFormats' => ->(obj) { [:supported_export_formats, GoogleInSpec::VertexAI::Property::ModelSupportedExportFormatsArray.parse(obj['supportedExportFormats'], to_s)] },
      'originalModelInfo' => ->(obj) { [:original_model_info, GoogleInSpec::VertexAI::Property::ModelOriginalModelInfo.new(obj['originalModelInfo'], to_s)] },
      'metadataArtifact' => ->(obj) { [:metadata_artifact, obj['metadataArtifact']] },
      'supportedInputStorageFormats' => ->(obj) { [:supported_input_storage_formats, obj['supportedInputStorageFormats']] },
      'metadataSchemaUri' => ->(obj) { [:metadata_schema_uri, obj['metadataSchemaUri']] },
      'containerSpec' => ->(obj) { [:container_spec, GoogleInSpec::VertexAI::Property::ModelContainerSpec.new(obj['containerSpec'], to_s)] },
      'versionId' => ->(obj) { [:version_id, obj['versionId']] },
      'artifactUri' => ->(obj) { [:artifact_uri, obj['artifactUri']] },
      'trainingPipeline' => ->(obj) { [:training_pipeline, obj['trainingPipeline']] },
      'displayName' => ->(obj) { [:display_name, obj['displayName']] },
      'supportedDeploymentResourcesTypes' => ->(obj) { [:supported_deployment_resources_types, obj['supportedDeploymentResourcesTypes']] },
      'supportedOutputStorageFormats' => ->(obj) { [:supported_output_storage_formats, obj['supportedOutputStorageFormats']] },
      'versionAliases' => ->(obj) { [:version_aliases, obj['versionAliases']] },
      'versionCreateTime' => ->(obj) { [:version_create_time, obj['versionCreateTime']] },
      'versionDescription' => ->(obj) { [:version_description, obj['versionDescription']] },
      'labels' => ->(obj) { [:labels, GoogleInSpec::VertexAI::Property::ModelLabels.new(obj['labels'], to_s)] },
    }
  end

  private

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

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