What is the purpose of a country for anyway? Basically, it is hard to survive on your own. Your odds increase dramatically if you band together with others. Anybody who has done time in a state prison quickly realizes this. You either join a gang or suffer the consequences.
Countries were formed when one gang, aka tribes in the old days, took over an entire region. Generally, the head gang leader was called king or emperor by then and instituted hereditary rule out of selfish interest.
Democracies are relatively new. Notwithstanding, the purpose is protect the residents of that country. For example, see the preamble to the US constitution: "establish justice and domestic tranquility, provide for the common defense, and promote the general welfare". A country isn't for the welfare of nonresidents or to establish some utopian state with open borders.