module Support
  module Services
    class OwsSalesGoals
      include HTTParty

      base_uri DOMAINS[:ows_salesgoals]

      class << self
        def create_goals(product_id, session_id)
          path = "/goals/#{product_id}/1"
          payload = JSON.parse(File.read("features/json/sales_goals.json"))
          options = {
            body: payload.to_json,
            headers: {
              "Content-Type" => "application/json",
              "session" => session_id
            }
          }
          response = post(path, options)

          JSON.parse(response.body)
        end

        def get_digital_projections(product_id, session_id)
          path = "/digital/#{product_id}"
          options = {
            headers: {
              "Content-Type" => "application/json",
              "session" => session_id
            }
          }
          response = get(path, options)
          JSON.parse(response.body)
        end

        def delete_projections(product_id, projection_id, session_id)
          path = "/digital/#{product_id}/projection/#{projection_id}"
          options = {
            headers: {
              "Content-Type" => "application/json",
              "session" => session_id
            }
          }
          response = delete(path, options)
          JSON.parse(response.body) unless response.nil?
        end

        def create_projections(product_id, session_id, territory_id)
          path = "/digital/#{product_id}/projections"
          options = {
            body: { "items":
                       [{ "other_projection": "1",
                          "apple_projection": "1",
                          "spotify_projection": "1",
                          "territory_id": territory_id,
                          "downloads_projection": "1" }] }.to_json,
            headers: {
              "Content-Type" => "application/json",
              "session" => session_id
            }
          }

          response = post(path, options)

          JSON.parse(response.body)
        end

        def create_global_totals(product_id, session_id)
          path = "/digital/#{product_id}"
          options = {

            body: { downloads_total: 1, apple_total: 1, spotify_total: 1, other_total: 1 }.to_json,
            headers: {
              "Content-Type" => "application/json",
              "session" => session_id
            }
          }
          response = post(path, options)

          JSON.parse(response.body)
        end
      end
    end
  end
end
