lib/lighthouse/facilities/response.rb
# frozen_string_literal: true

require 'vets/model'
require_relative 'facility'

module Lighthouse
  module Facilities
    class Response
      include Vets::Model

      attribute :body, String
      attribute :current_page, Integer
      attribute :data, Hash, array: true
      attribute :links, Hash
      attribute :meta, Hash
      attribute :per_page, Integer
      attribute :status, Integer
      attribute :total_entries, Integer

      def initialize(body, status)
        super()
        @body = body
        @status = status
        parsed_body = JSON.parse(body)
        @data = Array.wrap(parsed_body['data']) # normalize data to array
        @meta = parsed_body['meta']
        @links = parsed_body['links']
        if meta
          @current_page = meta['pagination']['current_page']
          @per_page = meta['pagination']['per_page']
          @total_entries = meta['pagination']['total_entries']
        end
      end

      def facilities
        facilities = data.each_with_index.map do |facility, index|
          fac = Lighthouse::Facilities::Facility.new(facility)
          fac.distance = meta['distances'][index]['distance'] unless meta['distances'].empty?
          fac
        end

        WillPaginate::Collection.create(current_page, per_page) do |pager|
          pager.replace(facilities)
          pager.total_entries = total_entries
        end
      end

      def facility
        Lighthouse::Facilities::Facility.new(data.first)
      end
    end
  end
end
