# RESTful API client library
require 'httparty'

# Abstraction layer for ows-pricing.
module OwsPricing
  include HTTParty

  base_uri ENV['OWS_PRICING_URL']

  def self.get_product_store_pricing(pricing_family_id, product_id, store_id)
    url = "/pricing_family/#{pricing_family_id}" \
      "/product/#{product_id}/store/#{store_id}/pricing"
    get(url)
  end

  def self.clean_product(product_id)
    delete("/legacy/product/#{product_id}/unmigrate")
  end

  def self.clean_track(track_id)
    delete("/legacy/track/#{track_id}/unmigrate")
  end

  def self.set_product_orchard_pricing_tier(
      product_id, pricing_family_id, orchard_pricing_tier_id
  )
    path = "/product/#{product_id}/pricing-family/#{pricing_family_id}" \
      '/orchard_pricing_tier'
    body = { "orchard_pricing_tier_id": orchard_pricing_tier_id }.to_json
    headers = { 'Content-Type' => 'application/json' }
    put(path, body: body, headers: headers)
  end

  def self.create_product_pricing_override(product_id, pricing_family_id)
    path = "/product/#{product_id}/override"
    body = {
      "pricing_family_id": pricing_family_id,
      "applies_worldwide": true,
      "price_code": 'OVERRIDE'
    }.to_json
    headers = { 'Content-Type' => 'application/json' }
    post(path, body: body, headers: headers)
  end

  def self.create_track_pricing_override(track_id)
    path = "/track/#{track_id}/override"
    body = {
      "applies_worldwide": true,
      "price_code": 'OVERRIDE'
    }.to_json
    headers = { 'Content-Type' => 'application/json' }
    post(path, body: body, headers: headers)
  end
end
