library(ggplot2)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(ggspatial)
library(dplyr)
library(ggrepel)

# Ensure devtools is installed
if (!requireNamespace("devtools", quietly = TRUE)) {
  install.packages("devtools")
}

# Install rnaturalearthhires if not already installed
if (!requireNamespace("rnaturalearthhires", quietly = TRUE)) {
  devtools::install_github("ropensci/rnaturalearthhires")
}

# Load rnaturalearthhires
library(rnaturalearthhires)

# Load the world map
latin_america <- ne_countries(scale = "medium", continent = "South America", returnclass = "sf")

# Load Ecuador shapefile (excluding Galapagos Islands)
ecuador <- ne_states(country = "Ecuador", returnclass = "sf") %>%
  filter(!name %in% c("Galapagos"))

# Case study locations
case_study <- data.frame(
  Name = c("Tena", "Naranjal", "Quito", "Guayaquil"),
  Latitude = c(-0.993, -2.666, -0.22985, -2.19616),
  Longitude = c(-77.812, -79.617, -78.52495, -79.88621),
  Type = c("Case Study", "Case Study", "Capital", "City")
)

# Create main map
main_map <- ggplot() +
  geom_sf(data = latin_america, fill = "gray90", color = "gray60", size = 0.2) +
  geom_sf(data = ecuador, fill = "lightgray", color = "darkgray", size = 0.4) +
  geom_text_repel(data = latin_america, aes(label = name, geometry = geometry),
                  stat = "sf_coordinates", size = 3, color = "black") +
  annotation_scale(location = "br", width_hint = 0.5) +
  annotation_north_arrow(location = "tl", which_north = "true",
                         style = north_arrow_fancy_orienteering) +
  # Labels and theme
  labs(
    title = "Case Study Locations in Ecuador",
    caption = "Data source: Natural Earth",
    x = "Longitude",
    y = "Latitude"
  ) +
  theme_minimal() +
  theme(
    text = element_text(color = "black"),
    panel.background = element_rect(fill = "white"),
    panel.grid = element_line(color = "gray80")
  )

# Create zoomed-in map
zoomed_map <- ggplot() +
  # Ecuador mainland map
  geom_sf(data = ecuador, fill = "lightgray", color = "darkgray", size = 0.4) +
  # Add case study points
  geom_point(data = case_study, aes(x = Longitude, y = Latitude, shape = Type),
             size = 4, color = "black") +
  scale_shape_manual(values = c("Case Study" = 16, "Capital" = 17, "City" = 15)) +
  geom_text(data = case_study %>% filter(Type == "Case Study"), 
            aes(x = Longitude, y = Latitude, label = Name),
            hjust = -0.2, vjust = -0.2, size = 4, color = "black") +
  geom_text(data = case_study %>% filter(Type != "Case Study"), 
            aes(x = Longitude, y = Latitude, label = Name),
            hjust = -0.2, vjust = -0.7, size = 3.5, color = "black", fontface = "italic") +
  # Simplified theme without legend
  coord_sf(xlim = c(-81, -75), ylim = c(-5, 2)) +  # Adjusted focus to include mainland borders
  theme_minimal() +
  theme(
    text = element_text(color = "black"),
    panel.background = element_rect(fill = "white"),
    panel.grid = element_line(color = "gray80"),
    axis.title = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    legend.position = "none"
  )

# Combine maps using patchwork
library(patchwork)
combined_map <- main_map + inset_element(zoomed_map, left = 0.01, bottom = 0.04, right = 0.45, top = 0.65)

# Display the combined map
print(combined_map)
