# 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/resourcemanager/property/project_parent'

# A provider to manage Resource Manager resources.
class ResourceManagerProject < GcpResourceBase
  name 'google_project'
  desc 'Project'
  supports platform: 'gcp'

  attr_reader :params
  attr_reader :number
  attr_reader :lifecycle_state
  attr_reader :name
  attr_reader :create_time
  attr_reader :labels
  attr_reader :parent
  attr_reader :project_id

  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
    @number = @fetched['projectNumber']
    @lifecycle_state = @fetched['lifecycleState']
    @name = @fetched['name']
    @create_time = parse_time_string(@fetched['createTime'])
    @labels = @fetched['labels']
    @parent = GoogleInSpec::ResourceManager::Property::ProjectParent.new(@fetched['parent'], to_s)
    @project_id = @fetched['projectId']
  end

  # Handles parsing RFC3339 time string
  def parse_time_string(time_string)
    time_string ? Time.parse(time_string) : nil
  end

  def exists?
    !@fetched.nil?
  end

  def to_s
    "Project #{@params[:project]}"
  end

  def project_name
    @name
  end

  def project_number
    @number&.to_i
  end

  def label_value_by_key(label_key)
    return nil if @labels.nil? || !@labels.has_key(label_key)
    @labels[label_key]
  end

  private

  def product_url(_ = nil)
    'https://cloudresourcemanager.googleapis.com/v1/'
  end

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