# frozen_string_literal: false

# ----------------------------------------------------------------------------
#
#     ***     AUTO GENERATED CODE    ***    AUTO GENERATED CODE     ***
#
# ----------------------------------------------------------------------------
#
#     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'
require 'google/bigquery/property/dataset_access'
require 'google/bigquery/property/dataset_dataset_reference'
require 'google/bigquery/property/dataset_default_encryption_configuration'

# A provider to manage BigQuery resources.
class BigQueryDataset < GcpResourceBase
  name 'google_bigquery_dataset'
  desc 'Dataset'
  supports platform: 'gcp'

  attr_reader :params
  attr_reader :access
  attr_reader :creation_time
  attr_reader :dataset_reference
  attr_reader :default_table_expiration_ms
  attr_reader :default_partition_expiration_ms
  attr_reader :description
  attr_reader :etag
  attr_reader :friendly_name
  attr_reader :id
  attr_reader :labels
  attr_reader :last_modified_time
  attr_reader :location
  attr_reader :default_encryption_configuration

  def initialize(params)
    super(params.merge({ use_http_transport: true }))
    @params = params
    @fetched = @connection.fetch(product_url(params[:beta]), resource_base_url, params, 'Get')
    parse unless @fetched.nil?
  end

  def parse
    @access = GoogleInSpec::BigQuery::Property::DatasetAccessArray.parse(@fetched['access'], to_s)
    @creation_time = @fetched['creationTime']
    @dataset_reference = GoogleInSpec::BigQuery::Property::DatasetDatasetReference.new(@fetched['datasetReference'], to_s)
    @default_table_expiration_ms = @fetched['defaultTableExpirationMs']
    @default_partition_expiration_ms = @fetched['defaultPartitionExpirationMs']
    @description = @fetched['description']
    @etag = @fetched['etag']
    @friendly_name = @fetched['friendlyName']
    @id = @fetched['id']
    @labels = @fetched['labels']
    @last_modified_time = @fetched['lastModifiedTime']
    @location = @fetched['location']
    @default_encryption_configuration = GoogleInSpec::BigQuery::Property::DatasetDefaultEncryptionConfiguration.new(@fetched['defaultEncryptionConfiguration'], to_s)
  end

  def exists?
    !@fetched.nil?
  end

  def to_s
    "Dataset #{@params[:name]}"
  end

  def name
    dataset_reference&.dataset_id
  end

  private

  def product_url(_ = nil)
    'https://bigquery.googleapis.com/bigquery/v2/'
  end

  def resource_base_url
    'projects/{{project}}/datasets/{{name}}'
  end
end
