class Threoze::InvoiceService < Threoze::ApplicationService
  def call
    return unless endpoint

    response = case @action
    when :index
      header = headers.merge(params: { status: @payload['status'] })
      RestClient.get(uri, header)
    when :show
      RestClient.get("#{uri}/#{@payload[:id]}", headers)
    when :pdf
      return RestClient.get("#{uri}/#{@payload[:id]}", headers)
    end

    JSON.parse(response.body)
  rescue RestClient::Exception => e
    JSON.parse(e.response.body)
  end

  private
    def uri
      "#{base_uri}/#{endpoint}/#{customer}"
    end

    def endpoint
      case @action
      when :index
        'CustomerInvoices'
      when :show
        'CustomerInvoice'
      when :pdf
        'CustomerInvoicePdf'
      end
    end
end
