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

  def index
    render_success :ok, json: medical_props
  end

  def update
    if medical.update(medical_params)
      render_success :ok, json: medical_props
    else
      render_error :unprocessable_entity, errors: medical
    end
  end

  private
    def medical
      @medical ||= current_family.find_or_build_family_medical
    end

    def medical_params
      params.permit(
        :physician,
        :physician_phone,
        :physician_address,
        :physician_address_ext,
        :physician_city,
        :physician_state,
        :physician_zip,
        :physician_country_id,
        :insurance_company,
        :insurance_group,
        :insurance_plan,
        :dentist,
        :dentist_phone,
        :dentist_address,
        :dentist_address_ext,
        :dentist_city,
        :dentist_state,
        :dentist_zip,
        :dentist_country_id,
        :comments,
        :hospital
      )
    end

    def medical_props
      {}.tap do |props|
        props[:family_id] = current_family.id
        props[:physician] = medical.physician
        props[:physician_phone] = medical.physician_phone
        props[:physician_address] = medical.physician_address
        props[:physician_address_ext] = medical.physician_address_ext
        props[:physician_city] = medical.physician_city
        props[:physician_state] = medical.physician_state
        props[:physician_zip] = medical.physician_zip
        props[:physician_country_id] =
          medical.physician_country_id? ? medical.physician_country_id : nil
        props[:insurance_company] = medical.insurance_company
        props[:dentist] = medical.dentist
        props[:dentist_phone] = medical.dentist_phone
        props[:dentist_address] = medical.dentist_address
        props[:dentist_address_ext] = medical.dentist_address_ext
        props[:dentist_city] = medical.dentist_city
        props[:dentist_state] = medical.dentist_state
        props[:dentist_zip] = medical.dentist_zip
        props[:dentist_country_id] = medical.dentist_country_id? ? medical.dentist_country_id : nil
        props[:insurance_company] = medical.insurance_company
        props[:insurance_plan] = medical.insurance_plan
        props[:insurance_group] = medical.insurance_group
        props[:hospital] = medical.hospital
        props[:comments] = medical.comments
      end
    end
end
