<?php
/**
 * LATAM city data — 15 countries × 5 cities = 75 entries.
 *
 * Each country returns an array of cities (called "communes" in GeoBot
 * terminology for compatibility). Each city entry includes name and region.
 *
 * @since 2.11.0
 */

if ( ! defined( 'ABSPATH' ) ) exit;

class DBA_LATAM_Cities {

    /**
     * Get all city data keyed by country.
     *
     * @return array
     */
    public static function get_all() {
        return array(

            'brazil' => array(
                array( 'name' => 'São Paulo',       'region' => 'São Paulo State' ),
                array( 'name' => 'Rio de Janeiro',   'region' => 'Rio de Janeiro State' ),
                array( 'name' => 'Brasília',         'region' => 'Federal District' ),
                array( 'name' => 'Salvador',          'region' => 'Bahia' ),
                array( 'name' => 'Fortaleza',         'region' => 'Ceará' ),
            ),

            'mexico' => array(
                array( 'name' => 'Mexico City',     'region' => 'Federal District' ),
                array( 'name' => 'Guadalajara',     'region' => 'Jalisco' ),
                array( 'name' => 'Monterrey',       'region' => 'Nuevo León' ),
                array( 'name' => 'Puebla',           'region' => 'Puebla State' ),
                array( 'name' => 'Tijuana',           'region' => 'Baja California' ),
            ),

            'argentina' => array(
                array( 'name' => 'Buenos Aires',     'region' => 'Buenos Aires Province' ),
                array( 'name' => 'Córdoba',           'region' => 'Córdoba Province' ),
                array( 'name' => 'Rosario',           'region' => 'Santa Fe Province' ),
                array( 'name' => 'Mendoza',           'region' => 'Mendoza Province' ),
                array( 'name' => 'La Plata',          'region' => 'Buenos Aires Province' ),
            ),

            'chile' => array(
                array( 'name' => 'Santiago',         'region' => 'Metropolitan Region' ),
                array( 'name' => 'Valparaíso',      'region' => 'Valparaíso Region' ),
                array( 'name' => 'Concepción',      'region' => 'Biobío Region' ),
                array( 'name' => 'Antofagasta',     'region' => 'Antofagasta Region' ),
                array( 'name' => 'Temuco',           'region' => 'Araucanía Region' ),
            ),

            'peru' => array(
                array( 'name' => 'Lima',             'region' => 'Lima Province' ),
                array( 'name' => 'Arequipa',         'region' => 'Arequipa Region' ),
                array( 'name' => 'Trujillo',         'region' => 'La Libertad Region' ),
                array( 'name' => 'Chiclayo',         'region' => 'Lambayeque Region' ),
                array( 'name' => 'Piura',             'region' => 'Piura Region' ),
            ),

            'colombia' => array(
                array( 'name' => 'Bogotá',          'region' => 'Capital District' ),
                array( 'name' => 'Medellín',        'region' => 'Antioquia' ),
                array( 'name' => 'Cali',             'region' => 'Valle del Cauca' ),
                array( 'name' => 'Barranquilla',     'region' => 'Atlántico' ),
                array( 'name' => 'Cartagena',        'region' => 'Bolívar' ),
            ),

            'ecuador' => array(
                array( 'name' => 'Guayaquil',       'region' => 'Guayas Province' ),
                array( 'name' => 'Quito',             'region' => 'Pichincha Province' ),
                array( 'name' => 'Cuenca',            'region' => 'Azuay Province' ),
                array( 'name' => 'Santo Domingo',    'region' => 'Santo Domingo Province' ),
                array( 'name' => 'Machala',           'region' => 'El Oro Province' ),
            ),

            'guatemala' => array(
                array( 'name' => 'Guatemala City',   'region' => 'Guatemala Department' ),
                array( 'name' => 'Mixco',             'region' => 'Guatemala Department' ),
                array( 'name' => 'Villa Nueva',      'region' => 'Guatemala Department' ),
                array( 'name' => 'Quetzaltenango',   'region' => 'Quetzaltenango Department' ),
                array( 'name' => 'Escuintla',        'region' => 'Escuintla Department' ),
            ),

            'honduras' => array(
                array( 'name' => 'Tegucigalpa',     'region' => 'Francisco Morazán' ),
                array( 'name' => 'San Pedro Sula',   'region' => 'Cortés' ),
                array( 'name' => 'Choloma',           'region' => 'Cortés' ),
                array( 'name' => 'La Ceiba',          'region' => 'Atlántida' ),
                array( 'name' => 'El Progreso',      'region' => 'Yoro' ),
            ),

            'el_salvador' => array(
                array( 'name' => 'San Salvador',     'region' => 'San Salvador Department' ),
                array( 'name' => 'San Miguel',       'region' => 'San Miguel Department' ),
                array( 'name' => 'Soyapango',        'region' => 'San Salvador Department' ),
                array( 'name' => 'Santa Ana',        'region' => 'Santa Ana Department' ),
                array( 'name' => 'Apopa',             'region' => 'San Salvador Department' ),
            ),

            'nicaragua' => array(
                array( 'name' => 'Managua',          'region' => 'Managua Department' ),
                array( 'name' => 'León',              'region' => 'León Department' ),
                array( 'name' => 'Masaya',            'region' => 'Masaya Department' ),
                array( 'name' => 'Chinandega',       'region' => 'Chinandega Department' ),
                array( 'name' => 'Estelí',           'region' => 'Estelí Department' ),
            ),

            'costa_rica' => array(
                array( 'name' => 'San José',        'region' => 'San José Province' ),
                array( 'name' => 'Alajuela',         'region' => 'Alajuela Province' ),
                array( 'name' => 'Heredia',           'region' => 'Heredia Province' ),
                array( 'name' => 'Cartago',           'region' => 'Cartago Province' ),
                array( 'name' => 'Liberia',           'region' => 'Guanacaste Province' ),
            ),

            'bolivia' => array(
                array( 'name' => 'Santa Cruz de la Sierra', 'region' => 'Santa Cruz Department' ),
                array( 'name' => 'El Alto',           'region' => 'La Paz Department' ),
                array( 'name' => 'La Paz',            'region' => 'La Paz Department' ),
                array( 'name' => 'Cochabamba',        'region' => 'Cochabamba Department' ),
                array( 'name' => 'Sucre',             'region' => 'Chuquisaca Department' ),
            ),

            'paraguay' => array(
                array( 'name' => 'Asunción',        'region' => 'Capital District' ),
                array( 'name' => 'Ciudad del Este',  'region' => 'Alto Paraná Department' ),
                array( 'name' => 'San Lorenzo',      'region' => 'Central Department' ),
                array( 'name' => 'Luque',             'region' => 'Central Department' ),
                array( 'name' => 'Capiatá',          'region' => 'Central Department' ),
            ),

            'uruguay' => array(
                array( 'name' => 'Montevideo',       'region' => 'Montevideo Department' ),
                array( 'name' => 'Salto',             'region' => 'Salto Department' ),
                array( 'name' => 'Paysandú',        'region' => 'Paysandú Department' ),
                array( 'name' => 'Maldonado',        'region' => 'Maldonado Department' ),
                array( 'name' => 'Rivera',            'region' => 'Rivera Department' ),
            ),
        );
    }

    /**
     * Get cities for a specific country.
     *
     * @param  string $country_key
     * @return array
     */
    public static function get_country_cities( $country_key ) {
        $all = self::get_all();
        return isset( $all[ $country_key ] ) ? $all[ $country_key ] : array();
    }

    /**
     * Load all city data into wp_options (one option per country).
     * Called on plugin activation.
     */
    public static function load_all_cities() {
        $all       = self::get_all();
        $countries = DBA_LATAM_Registry::get_countries();
        $loaded    = 0;

        foreach ( $all as $country_key => $cities ) {
            if ( ! isset( $countries[ $country_key ] ) ) {
                continue;
            }
            $option_key = $countries[ $country_key ]['communes_option'];
            update_option( $option_key, $cities, false );
            $loaded += count( $cities );
        }

        error_log( 'DBA LATAM: Loaded ' . $loaded . ' cities across ' . count( $all ) . ' countries.' );
    }
}
