module PlanetExpressExpress
  class Rules
    def initialize(prices)
      @lowest_s_price = prices.select { |k, v| k[1] == :S }.values.min
    end

    def apply_rules(shipments)
      s_rule(shipments[:S])
      l_rule(shipments[:L])

      shipments
    end

    private

    attr_reader :lowest_s_price

    def l_rule(monthly_shipments)
      monthly_shipments.each do |month|
        lp_count = 0
        month.each do |shipment|
          next if shipment[:provider] != :LP

          lp_count += 1
          if lp_count == 3
            shipment.discount = shipment.price
            break
          end
        end
      end
    end

    def s_rule(monthly_shipments)
      monthly_shipments.each do |month|
        month.each do |shipment|
          shipment.discount += shipment.price - lowest_s_price
        end
      end
    end
  end
end