<?php
/**
 * TitanCart Country & State Seeder.
 *
 * Populates tc_countries with ISO 3166-1 country list and
 * tc_states with US + CA states/provinces.
 *
 * Called by Installer::seed_data() on fresh install and by
 * Plugin.php migration for existing installs.
 *
 * @package TitanCart\DB
 */

namespace TitanCart\DB;

defined( 'ABSPATH' ) || exit;

class CountrySeeder {

    /**
     * Seed countries and states if tables are empty.
     */
    public static function seed(): void {
        global $wpdb;
        $p = $wpdb->prefix . TITANCART_TABLE_PREFIX;

        // Only seed if table is empty.
        $count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$p}countries" );
        if ( $count > 0 ) {
            return;
        }

        $countries = self::get_countries();
        foreach ( $countries as $c ) {
            $wpdb->insert( $p . 'countries', [
                'code'   => $c[0],
                'code3'  => $c[1],
                'name'   => $c[2],
                'status' => 'active',
            ] );
        }

        // Seed US states.
        $us_states = self::get_us_states();
        foreach ( $us_states as $code => $name ) {
            $wpdb->insert( $p . 'states', [
                'country_code' => 'US',
                'code'         => $code,
                'name'         => $name,
                'status'       => 'active',
            ] );
        }

        // Seed Canadian provinces.
        $ca_provinces = self::get_ca_provinces();
        foreach ( $ca_provinces as $code => $name ) {
            $wpdb->insert( $p . 'states', [
                'country_code' => 'CA',
                'code'         => $code,
                'name'         => $name,
                'status'       => 'active',
            ] );
        }
    }

    /**
     * ISO 3166-1 countries: [alpha2, alpha3, name]
     */
    private static function get_countries(): array {
        return [
            ['AF','AFG','Afghanistan'],['AL','ALB','Albania'],['DZ','DZA','Algeria'],['AS','ASM','American Samoa'],
            ['AD','AND','Andorra'],['AO','AGO','Angola'],['AG','ATG','Antigua and Barbuda'],['AR','ARG','Argentina'],
            ['AM','ARM','Armenia'],['AU','AUS','Australia'],['AT','AUT','Austria'],['AZ','AZE','Azerbaijan'],
            ['BS','BHS','Bahamas'],['BH','BHR','Bahrain'],['BD','BGD','Bangladesh'],['BB','BRB','Barbados'],
            ['BY','BLR','Belarus'],['BE','BEL','Belgium'],['BZ','BLZ','Belize'],['BJ','BEN','Benin'],
            ['BM','BMU','Bermuda'],['BT','BTN','Bhutan'],['BO','BOL','Bolivia'],['BA','BIH','Bosnia and Herzegovina'],
            ['BW','BWA','Botswana'],['BR','BRA','Brazil'],['BN','BRN','Brunei'],['BG','BGR','Bulgaria'],
            ['BF','BFA','Burkina Faso'],['BI','BDI','Burundi'],['KH','KHM','Cambodia'],['CM','CMR','Cameroon'],
            ['CA','CAN','Canada'],['CV','CPV','Cape Verde'],['KY','CYM','Cayman Islands'],['CF','CAF','Central African Republic'],
            ['TD','TCD','Chad'],['CL','CHL','Chile'],['CN','CHN','China'],['CO','COL','Colombia'],
            ['KM','COM','Comoros'],['CG','COG','Congo'],['CD','COD','Congo (DRC)'],['CR','CRI','Costa Rica'],
            ['CI','CIV','Ivory Coast'],['HR','HRV','Croatia'],['CU','CUB','Cuba'],['CY','CYP','Cyprus'],
            ['CZ','CZE','Czech Republic'],['DK','DNK','Denmark'],['DJ','DJI','Djibouti'],['DM','DMA','Dominica'],
            ['DO','DOM','Dominican Republic'],['EC','ECU','Ecuador'],['EG','EGY','Egypt'],['SV','SLV','El Salvador'],
            ['GQ','GNQ','Equatorial Guinea'],['ER','ERI','Eritrea'],['EE','EST','Estonia'],['SZ','SWZ','Eswatini'],
            ['ET','ETH','Ethiopia'],['FJ','FJI','Fiji'],['FI','FIN','Finland'],['FR','FRA','France'],
            ['GA','GAB','Gabon'],['GM','GMB','Gambia'],['GE','GEO','Georgia'],['DE','DEU','Germany'],
            ['GH','GHA','Ghana'],['GR','GRC','Greece'],['GD','GRD','Grenada'],['GU','GUM','Guam'],
            ['GT','GTM','Guatemala'],['GN','GIN','Guinea'],['GW','GNB','Guinea-Bissau'],['GY','GUY','Guyana'],
            ['HT','HTI','Haiti'],['HN','HND','Honduras'],['HK','HKG','Hong Kong'],['HU','HUN','Hungary'],
            ['IS','ISL','Iceland'],['IN','IND','India'],['ID','IDN','Indonesia'],['IR','IRN','Iran'],
            ['IQ','IRQ','Iraq'],['IE','IRL','Ireland'],['IL','ISR','Israel'],['IT','ITA','Italy'],
            ['JM','JAM','Jamaica'],['JP','JPN','Japan'],['JO','JOR','Jordan'],['KZ','KAZ','Kazakhstan'],
            ['KE','KEN','Kenya'],['KI','KIR','Kiribati'],['KP','PRK','North Korea'],['KR','KOR','South Korea'],
            ['KW','KWT','Kuwait'],['KG','KGZ','Kyrgyzstan'],['LA','LAO','Laos'],['LV','LVA','Latvia'],
            ['LB','LBN','Lebanon'],['LS','LSO','Lesotho'],['LR','LBR','Liberia'],['LY','LBY','Libya'],
            ['LI','LIE','Liechtenstein'],['LT','LTU','Lithuania'],['LU','LUX','Luxembourg'],['MO','MAC','Macau'],
            ['MG','MDG','Madagascar'],['MW','MWI','Malawi'],['MY','MYS','Malaysia'],['MV','MDV','Maldives'],
            ['ML','MLI','Mali'],['MT','MLT','Malta'],['MH','MHL','Marshall Islands'],['MR','MRT','Mauritania'],
            ['MU','MUS','Mauritius'],['MX','MEX','Mexico'],['FM','FSM','Micronesia'],['MD','MDA','Moldova'],
            ['MC','MCO','Monaco'],['MN','MNG','Mongolia'],['ME','MNE','Montenegro'],['MA','MAR','Morocco'],
            ['MZ','MOZ','Mozambique'],['MM','MMR','Myanmar'],['NA','NAM','Namibia'],['NR','NRU','Nauru'],
            ['NP','NPL','Nepal'],['NL','NLD','Netherlands'],['NZ','NZL','New Zealand'],['NI','NIC','Nicaragua'],
            ['NE','NER','Niger'],['NG','NGA','Nigeria'],['MK','MKD','North Macedonia'],['NO','NOR','Norway'],
            ['OM','OMN','Oman'],['PK','PAK','Pakistan'],['PW','PLW','Palau'],['PS','PSE','Palestine'],
            ['PA','PAN','Panama'],['PG','PNG','Papua New Guinea'],['PY','PRY','Paraguay'],['PE','PER','Peru'],
            ['PH','PHL','Philippines'],['PL','POL','Poland'],['PT','PRT','Portugal'],['PR','PRI','Puerto Rico'],
            ['QA','QAT','Qatar'],['RO','ROU','Romania'],['RU','RUS','Russia'],['RW','RWA','Rwanda'],
            ['KN','KNA','Saint Kitts and Nevis'],['LC','LCA','Saint Lucia'],['VC','VCT','Saint Vincent and the Grenadines'],
            ['WS','WSM','Samoa'],['SM','SMR','San Marino'],['ST','STP','Sao Tome and Principe'],['SA','SAU','Saudi Arabia'],
            ['SN','SEN','Senegal'],['RS','SRB','Serbia'],['SC','SYC','Seychelles'],['SL','SLE','Sierra Leone'],
            ['SG','SGP','Singapore'],['SK','SVK','Slovakia'],['SI','SVN','Slovenia'],['SB','SLB','Solomon Islands'],
            ['SO','SOM','Somalia'],['ZA','ZAF','South Africa'],['SS','SSD','South Sudan'],['ES','ESP','Spain'],
            ['LK','LKA','Sri Lanka'],['SD','SDN','Sudan'],['SR','SUR','Suriname'],['SE','SWE','Sweden'],
            ['CH','CHE','Switzerland'],['SY','SYR','Syria'],['TW','TWN','Taiwan'],['TJ','TJK','Tajikistan'],
            ['TZ','TZA','Tanzania'],['TH','THA','Thailand'],['TL','TLS','Timor-Leste'],['TG','TGO','Togo'],
            ['TO','TON','Tonga'],['TT','TTO','Trinidad and Tobago'],['TN','TUN','Tunisia'],['TR','TUR','Turkey'],
            ['TM','TKM','Turkmenistan'],['TV','TUV','Tuvalu'],['UG','UGA','Uganda'],['UA','UKR','Ukraine'],
            ['AE','ARE','United Arab Emirates'],['GB','GBR','United Kingdom'],['US','USA','United States'],
            ['UY','URY','Uruguay'],['UZ','UZB','Uzbekistan'],['VU','VUT','Vanuatu'],['VA','VAT','Vatican City'],
            ['VE','VEN','Venezuela'],['VN','VNM','Vietnam'],['YE','YEM','Yemen'],['ZM','ZMB','Zambia'],
            ['ZW','ZWE','Zimbabwe'],
        ];
    }

    /**
     * US states: code => name
     */
    private static function get_us_states(): array {
        return [
            'AL'=>'Alabama','AK'=>'Alaska','AZ'=>'Arizona','AR'=>'Arkansas','CA'=>'California',
            'CO'=>'Colorado','CT'=>'Connecticut','DE'=>'Delaware','DC'=>'District of Columbia','FL'=>'Florida',
            'GA'=>'Georgia','HI'=>'Hawaii','ID'=>'Idaho','IL'=>'Illinois','IN'=>'Indiana',
            'IA'=>'Iowa','KS'=>'Kansas','KY'=>'Kentucky','LA'=>'Louisiana','ME'=>'Maine',
            'MD'=>'Maryland','MA'=>'Massachusetts','MI'=>'Michigan','MN'=>'Minnesota','MS'=>'Mississippi',
            'MO'=>'Missouri','MT'=>'Montana','NE'=>'Nebraska','NV'=>'Nevada','NH'=>'New Hampshire',
            'NJ'=>'New Jersey','NM'=>'New Mexico','NY'=>'New York','NC'=>'North Carolina','ND'=>'North Dakota',
            'OH'=>'Ohio','OK'=>'Oklahoma','OR'=>'Oregon','PA'=>'Pennsylvania','RI'=>'Rhode Island',
            'SC'=>'South Carolina','SD'=>'South Dakota','TN'=>'Tennessee','TX'=>'Texas','UT'=>'Utah',
            'VT'=>'Vermont','VA'=>'Virginia','WA'=>'Washington','WV'=>'West Virginia','WI'=>'Wisconsin',
            'WY'=>'Wyoming',
        ];
    }

    /**
     * Canadian provinces: code => name
     */
    private static function get_ca_provinces(): array {
        return [
            'AB'=>'Alberta','BC'=>'British Columbia','MB'=>'Manitoba','NB'=>'New Brunswick',
            'NL'=>'Newfoundland and Labrador','NS'=>'Nova Scotia','NT'=>'Northwest Territories',
            'NU'=>'Nunavut','ON'=>'Ontario','PE'=>'Prince Edward Island','QC'=>'Quebec',
            'SK'=>'Saskatchewan','YT'=>'Yukon',
        ];
    }
}
