class Family::Legacy::DirectoriesController < Family::Controller
  before_action :validate_school_allows_changes, only: :update

  def show
    render_success :ok, json: family_props
  end

  def update
    if current_family.update(family_params)
      render_success :ok, json: family_props
    else
      render_error :unprocessable_entity, errors: current_family
    end
  end

  private
    def family_params
      params.permit(
        :address,
        :address_ext,
        :city,
        :county,
        :state,
        :zip,
        :country_id,
        :address_2,
        :address_ext_2,
        :city_2,
        :state_2,
        :zip_2,
        :country_2_id,
        :home_phone,
        :cell_phone,
        :directory,
        :directory_phone,
        :directory_address,
        :directory_cell,
        :directory_email
      )
    end

    def family_props
      {}.tap do |props|
        props[:family_name] = current_family.name
        props[:family_id] = current_family.id
        props[:home_phone] = current_family.home_phone
        props[:cell_phone] = current_family.cell_phone
        props[:address] = current_family.address
        props[:address_ext] = current_family.address_ext
        props[:city] = current_family.city
        props[:county] = current_family.county
        props[:state] = current_family.state
        props[:zip] = current_family.zip
        props[:country_id] = current_family.country_id
        props[:country_name] = current_family.country&.name
        props[:address_2] = current_family.address_2
        props[:address_ext_2] = current_family.address_ext_2
        props[:city_2] = current_family.city_2
        props[:state_2] = current_family.state_2
        props[:zip_2] = current_family.zip_2
        props[:country_2_id] = current_family.country_2_id
        props[:country_2_name] = current_family.country_2&.name
        props[:directory] = current_family.directory?
        props[:directory_address] = current_family.directory_address?
        props[:directory_phone] = current_family.directory_phone?
        props[:directory_email] = current_family.directory_email?
        props[:directory_cell] = current_family.directory_cell?
      end
    end
end
