When Napoleon invaded Portugal, Juan VI took refuge in Brazil (1807) and later promoted a legal reform (1815) by declaring Brazil as the territorial base of the "Empire of Brazil, Portugal, and the Algarve". Thus, Rio de Janeiro becomes the seat of an absolute monarchy as well as those of Europe, and no longer a colony. In 1821, Juan VI returned to Portugal, leaving his son Pedro de Braganza as governor of Brazil, but the following year he proclaimed himself emperor of Brazil. In 1831 he abdicated and was succeeded by his son, Pedro II, who reigned until 1889 when the first republic was proclaimed.