class LocalRestriction
  attr_reader :gss_code

  def initialize(gss_code)
    @gss_code = gss_code
  end

  def all_restrictions
    @all_restrictions ||= YAML.load_file(file_name)
  end

  def restriction
    all_restrictions[gss_code] || {}
  end

  def file_name
    "lib/local_restrictions/local-restrictions.yaml"
  end

  def area_name
    restriction["name"]
  end

  def current_alert_level
    current["alert_level"] if current.present?
  end

  def future_alert_level
    future["alert_level"] if future.present?
  end

  def current
    restrictions = restriction["restrictions"]
    current_restrictions = restrictions.select do |rest|
      start_date = Time.zone.parse("#{rest['start_date']} #{rest['start_time']}")
      start_date.past?
    end

    current_restrictions.max_by { |rest| rest["start_date"] }
  end

  def future
    restrictions = restriction["restrictions"]
    future_restrictions = restrictions.select do |rest|
      start_date = Time.zone.parse("#{rest['start_date']} #{rest['start_time']}")
      start_date.future?
    end

    future_restrictions.min_by { |rest| rest["start_date"] }
  end
end

class LocalRestriction
  attr_reader :gss_code

  def initialize(gss_code)
    @gss_code = gss_code
    @time_zone = Time.find_zone("London")
  end

  def all_restrictions
    @all_restrictions ||= YAML.load_file(file_name)
  end

  def restriction
    all_restrictions[gss_code] || {}
  end

  def file_name
    "lib/local_restrictions/local-restrictions.yaml"
  end

  def area_name
    restriction["name"]
  end

  def current_alert_level
    current["alert_level"] if current.present?
  end

  def future_alert_level
    future["alert_level"] if future.present?
  end

  def current
    restrictions = restriction["restrictions"]
    current_restrictions = restrictions.select do |rest|
      start_date = @time_zone.parse("#{rest['start_date']} #{rest['start_time']}")
      start_date.past?
    end

    current_restrictions.max_by { |rest| rest["start_date"] }
  end

  def future
    restrictions = restriction["restrictions"]
    future_restrictions = restrictions.select do |rest|
      start_date = @time_zone.parse("#{rest['start_date']} #{rest['start_time']}")
      start_date.future?
    end

    future_restrictions.min_by { |rest| rest["start_date"] }
  end
end

Set time zone to London
Set time zone to London

Borrowed from: https://github.com/alphagov/content-publisher/commit/4ae1c4ed683bf0b24eb11d135aebd3cdd1a8ffa6

We need to make sure the alert level changes go out at the correct time.
The time zone on the server is incorrect, which means that when we're using
dates and times to set the level they're an hour out of date during daylight saving
time. This sets the Local restrictions time zone to 'London'.
We tried doing this at the app level, but it broke a lot of unrelated tests.
