When(/^(existent|non-existent) product is provided$/) do |product|
  case product
  when "existent"
    @res = retrieve_release_assets_info(PRODUCT_A_ID)
  when "non-existent"
    @res = retrieve_release_assets_info("non-existent")
  end
end

When(/^existent product is provided by admin$/) do
  @res = retrieve_release_assets_info(PRODUCT_A_ID, OA_USER_SERVICE_HEADERS)
end

Then(/^a valid assets details should be obtained$/) do
  parsed_response = JSON.parse(@res[:response])
  refute_nil(parsed_response)

  # this piece is intended to create a new hash with keys as asset_type
  # because elements' order can change and access via index is not reliable enough here
  output_hash = {}
  refute_nil(parsed_response["assets"], "no assets were found")
  parsed_response["assets"].each do |hash_item|
    output_hash = output_hash.merge(hash_item["asset_type"] => hash_item)
  end

  assert_equal(200, @res[:code])
  assert_equal(PRODUCT_A_ID.to_s, parsed_response["product_id"])
  assert_equal(PRODUCT_A_UPC.to_i, parsed_response["upc"])

  assert_equal(PRODUCT_A_TUID, output_hash["audio"]["track_unique_id"])
  assert_equal("audio", output_hash["audio"]["asset_type"])

  assert_includes(output_hash["audio"]["filename"], ".#{@filetype.downcase}")
  refute_nil(output_hash["audio"]["access"])
  refute_nil(output_hash["audio"]["stream"])

  refute_nil(output_hash["image"]["url"])
  assert_equal("image", output_hash["image"]["asset_type"])
end

Then(/^an (\d+) error should be obtained in get assets details response$/) do |error|
  parsed_response = JSON.parse(@res[:response])

  case error.to_i
  when 404
    assert_equal(404, @res[:code])
    assert_equal("Product not found for provided product id.", parsed_response["message"])
    assert_equal("not_found_error", parsed_response["code"])
  when 500
    # not sure how to simulate this yet
  end
end

Given(
  /^a new (JPG|WAV|TIF|FLAC|M4A) asset file is uploaded for a (product_a|product_b)$/
) do |filetype, product|
  @filetype = filetype

  step "the client has uploaded a valid #{filetype.downcase} file to s3"

  token_details = JSON.parse(@token_details[:response])
  case product
  when "product_a"
    post_asset_body = create_post_asset_body(token_details, filetype)
    @res = post_asset(post_asset_body.to_json)
  when "product_b"
    post_asset_body = create_post_asset_body(token_details,
                                             filetype,
                                             PRODUCT_B_ID,
                                             PRODUCT_B_UPC,
                                             PRODUCT_B_TUID)
    @res = post_asset(post_asset_body.to_json)
  end
  step "the /POST asset response contains a 200 code"
  step "/GET asset response is [finished]"
end
