-- Country Data Import SQL Script
-- Generated from Lastenheft_Requirements Appendix A_Länder 1.csv


-- ===================================================
-- INSERT a property set if not exists.
-- ===================================================

INSERT INTO property_set (state)
SELECT 'VALID'
WHERE NOT EXISTS (
    SELECT 1 FROM property_set ps
    WHERE ps.state = 'VALID'
      AND ps.start_date <= NOW()
      AND (ps.end_date IS NULL OR ps.end_date > NOW())
);



-- =============================================================================
-- 1. INSERT COUNTRY PROPERTY TYPES
-- =============================================================================

INSERT INTO `country_property_type`
(`name`, `external_mapping_id`, `data_type`, `validation_rule`, `is_required`, `description`, `property_group`, `sequence_number`)
VALUES
    ('Customs Union', 'UNION', 'ENUMERATION', '{ "ENUM" : ["EU", "NONE"]}', FALSE, 'Specifies the customs union in which the country is located. When crossing a customs union border, customs costs are added to the calculation result.', 'General', 1),
    ('Safety Stock  [working days]', 'SAFETY_STOCK', 'INT', '{"GTE": 0}', FALSE, 'Specifies the safety stock in working days that is maintained when sourcing from this country.', 'General', 2),
    ('Air Freight Share  [%]', 'AIR_SHARE', 'PERCENTAGE', '{"GTE": 0}', FALSE, 'Specifies the maximum air freight proportion that is included in the calculation when sourcing from this country. The actual air freight proportion that is used additionally depends on the overseas share of the part number and lies between 0% and this value.', 'General', 3),
    ('Wage Factor [%]', 'WAGE', 'PERCENTAGE', '{"GT": 0}', FALSE, 'Specifies the wage factor level for calculating handling costs in relation to the German wage factor level.', 'General', 4);

-- =============================================================================
-- 2. INSERT COUNTRIES
-- =============================================================================

INSERT INTO `country` (`iso_code`, `name`, `region_code`, `is_deprecated`) VALUES
                                                                       ('AD', 'Andorra', 'EMEA', FALSE),
                                                                       ('AE', 'United Arab Emirates', 'EMEA', FALSE),
                                                                       ('AF', 'Afghanistan', 'EMEA', FALSE),
                                                                       ('AG', 'Antigua and Barbuda', 'LATAM', FALSE),
                                                                       ('AI', 'Anguilla', 'LATAM', FALSE),
                                                                       ('AL', 'Albania', 'EMEA', FALSE),
                                                                       ('AM', 'Armenia', 'EMEA', FALSE),
                                                                       ('AO', 'Angola', 'EMEA', FALSE),
                                                                       ('AQ', 'Antarctica', 'EMEA', FALSE),
                                                                       ('AR', 'Argentina', 'LATAM', FALSE),
                                                                       ('AS', 'American Samoa', 'APAC', FALSE),
                                                                       ('AT', 'Austria', 'EMEA', FALSE),
                                                                       ('AU', 'Australia', 'APAC', FALSE),
                                                                       ('AW', 'Aruba', 'LATAM', FALSE),
                                                                       ('AX', 'Åland Islands', 'EMEA', FALSE),
                                                                       ('AZ', 'Azerbaijan', 'EMEA', FALSE),
                                                                       ('BA', 'Bosnia and Herzegovina', 'EMEA', FALSE),
                                                                       ('BB', 'Barbados', 'LATAM', FALSE),
                                                                       ('BD', 'Bangladesh', 'EMEA', FALSE),
                                                                       ('BE', 'Belgium', 'EMEA', FALSE),
                                                                       ('BF', 'Burkina Faso', 'EMEA', FALSE),
                                                                       ('BG', 'Bulgaria', 'EMEA', FALSE),
                                                                       ('BH', 'Bahrain', 'EMEA', FALSE),
                                                                       ('BI', 'Burundi', 'EMEA', FALSE),
                                                                       ('BJ', 'Benin', 'EMEA', FALSE),
                                                                       ('BL', 'Saint Barthélemy', 'LATAM', FALSE),
                                                                       ('BM', 'Bermuda', 'NAM', FALSE),
                                                                       ('BN', 'Brunei Darussalam', 'APAC', FALSE),
                                                                       ('BO', 'Bolivia', 'LATAM', FALSE),
                                                                       ('BQ', 'Bonaire, Sint Eustatius and Saba', 'LATAM', FALSE),
                                                                       ('BR', 'Brazil', 'LATAM', FALSE),
                                                                       ('BS', 'Bahamas', 'LATAM', FALSE),
                                                                       ('BT', 'Bhutan', 'APAC', FALSE),
                                                                       ('BV', 'Bouvet Island', 'EMEA', FALSE),
                                                                       ('BW', 'Botswana', 'EMEA', FALSE),
                                                                       ('BY', 'Belarus', 'EMEA', FALSE),
                                                                       ('BZ', 'Belize', 'LATAM', FALSE),
                                                                       ('CA', 'Canada', 'NAM', FALSE),
                                                                       ('CC', 'Cocos (Keeling) Islands', 'APAC', FALSE),
                                                                       ('CD', 'Congo, Democratic Republic', 'EMEA', FALSE),
                                                                       ('CF', 'Central African Republic', 'EMEA', FALSE),
                                                                       ('CG', 'Congo', 'EMEA', FALSE),
                                                                       ('CH', 'Switzerland', 'EMEA', FALSE),
                                                                       ('CI', 'Côte d''Ivoire', 'EMEA', FALSE),
                                                                       ('CK', 'Cook Islands', 'APAC', FALSE),
                                                                       ('CL', 'Chile', 'LATAM', FALSE),
                                                                       ('CM', 'Cameroon', 'EMEA', FALSE),
                                                                       ('CN', 'China', 'APAC', FALSE),
                                                                       ('CO', 'Colombia', 'LATAM', FALSE),
                                                                       ('CR', 'Costa Rica', 'LATAM', FALSE),
                                                                       ('CU', 'Cuba', 'LATAM', FALSE),
                                                                       ('CV', 'Cabo Verde', 'EMEA', FALSE),
                                                                       ('CW', 'Curaçao', 'LATAM', FALSE),
                                                                       ('CX', 'Christmas Island', 'APAC', FALSE),
                                                                       ('CY', 'Cyprus', 'EMEA', FALSE),
                                                                       ('CZ', 'Czech Republic', 'EMEA', FALSE),
                                                                       ('DE', 'Germany', 'EMEA', FALSE),
                                                                       ('DJ', 'Djibouti', 'EMEA', FALSE),
                                                                       ('DK', 'Denmark', 'EMEA', FALSE),
                                                                       ('DM', 'Dominica', 'LATAM', FALSE),
                                                                       ('DO', 'Dominican Republic', 'LATAM', FALSE),
                                                                       ('DZ', 'Algeria', 'EMEA', FALSE),
                                                                       ('EC', 'Ecuador', 'LATAM', FALSE),
                                                                       ('EE', 'Estonia', 'EMEA', FALSE),
                                                                       ('EG', 'Egypt', 'EMEA', FALSE),
                                                                       ('EH', 'Western Sahara', 'EMEA', FALSE),
                                                                       ('ER', 'Eritrea', 'EMEA', FALSE),
                                                                       ('ES', 'Spain', 'EMEA', FALSE),
                                                                       ('ET', 'Ethiopia', 'EMEA', FALSE),
                                                                       ('FI', 'Finland', 'EMEA', FALSE),
                                                                       ('FJ', 'Fiji', 'APAC', FALSE),
                                                                       ('FK', 'Falkland Islands', 'LATAM', FALSE),
                                                                       ('FM', 'Micronesia', 'APAC', FALSE),
                                                                       ('FO', 'Faroe Islands', 'EMEA', FALSE),
                                                                       ('FR', 'France', 'EMEA', FALSE),
                                                                       ('GA', 'Gabon', 'EMEA', FALSE),
                                                                       ('GB', 'United Kingdom', 'EMEA', FALSE),
                                                                       ('GD', 'Grenada', 'LATAM', FALSE),
                                                                       ('GE', 'Georgia', 'EMEA', FALSE),
                                                                       ('GF', 'French Guiana', 'LATAM', FALSE),
                                                                       ('GG', 'Guernsey', 'EMEA', FALSE),
                                                                       ('GH', 'Ghana', 'EMEA', FALSE),
                                                                       ('GI', 'Gibraltar', 'EMEA', FALSE),
                                                                       ('GL', 'Greenland', 'NAM', FALSE),
                                                                       ('GM', 'Gambia', 'EMEA', FALSE),
                                                                       ('GN', 'Guinea', 'EMEA', FALSE),
                                                                       ('GP', 'Guadeloupe', 'LATAM', FALSE),
                                                                       ('GQ', 'Equatorial Guinea', 'EMEA', FALSE),
                                                                       ('GR', 'Greece', 'EMEA', FALSE),
                                                                       ('GS', 'South Georgia and South Sandwich Islands', 'LATAM', FALSE),
                                                                       ('GT', 'Guatemala', 'LATAM', FALSE),
                                                                       ('GU', 'Guam', 'APAC', FALSE),
                                                                       ('GW', 'Guinea-Bissau', 'EMEA', FALSE),
                                                                       ('GY', 'Guyana', 'LATAM', FALSE),
                                                                       ('HK', 'Hong Kong', 'APAC', FALSE),
                                                                       ('HM', 'Heard Island and McDonald Islands', 'APAC', FALSE),
                                                                       ('HN', 'Honduras', 'LATAM', FALSE),
                                                                       ('HR', 'Croatia', 'EMEA', FALSE),
                                                                       ('HT', 'Haiti', 'LATAM', FALSE),
                                                                       ('HU', 'Hungary', 'EMEA', FALSE),
                                                                       ('ID', 'Indonesia', 'APAC', FALSE),
                                                                       ('IE', 'Ireland', 'EMEA', FALSE),
                                                                       ('IL', 'Israel', 'EMEA', FALSE),
                                                                       ('IM', 'Isle of Man', 'EMEA', FALSE),
                                                                       ('IN', 'India', 'APAC', FALSE),
                                                                       ('IO', 'British Indian Ocean Territory', 'APAC', FALSE),
                                                                       ('IQ', 'Iraq', 'EMEA', FALSE),
                                                                       ('IR', 'Iran', 'EMEA', FALSE),
                                                                       ('IS', 'Iceland', 'EMEA', FALSE),
                                                                       ('IT', 'Italy', 'EMEA', FALSE),
                                                                       ('JE', 'Jersey', 'EMEA', FALSE),
                                                                       ('JM', 'Jamaica', 'LATAM', FALSE),
                                                                       ('JO', 'Jordan', 'EMEA', FALSE),
                                                                       ('JP', 'Japan', 'APAC', FALSE),
                                                                       ('KE', 'Kenya', 'EMEA', FALSE),
                                                                       ('KG', 'Kyrgyzstan', 'EMEA', FALSE),
                                                                       ('KH', 'Cambodia', 'APAC', FALSE),
                                                                       ('KI', 'Kiribati', 'APAC', FALSE),
                                                                       ('KM', 'Comoros', 'EMEA', FALSE),
                                                                       ('KN', 'Saint Kitts and Nevis', 'LATAM', FALSE),
                                                                       ('KP', 'Korea, North', 'APAC', FALSE),
                                                                       ('KR', 'Korea, South', 'APAC', FALSE),
                                                                       ('KW', 'Kuwait', 'EMEA', FALSE),
                                                                       ('KY', 'Cayman Islands', 'LATAM', FALSE),
                                                                       ('KZ', 'Kazakhstan', 'EMEA', FALSE),
                                                                       ('LA', 'Laos', 'APAC', FALSE),
                                                                       ('LB', 'Lebanon', 'EMEA', FALSE),
                                                                       ('LC', 'Saint Lucia', 'LATAM', FALSE),
                                                                       ('LI', 'Liechtenstein', 'EMEA', FALSE),
                                                                       ('LK', 'Sri Lanka', 'APAC', FALSE),
                                                                       ('LR', 'Liberia', 'EMEA', FALSE),
                                                                       ('LS', 'Lesotho', 'EMEA', FALSE),
                                                                       ('LT', 'Lithuania', 'EMEA', FALSE),
                                                                       ('LU', 'Luxembourg', 'EMEA', FALSE),
                                                                       ('LV', 'Latvia', 'EMEA', FALSE),
                                                                       ('LY', 'Libya', 'EMEA', FALSE),
                                                                       ('MA', 'Morocco', 'EMEA', FALSE),
                                                                       ('MC', 'Monaco', 'EMEA', FALSE),
                                                                       ('MD', 'Moldova', 'EMEA', FALSE),
                                                                       ('ME', 'Montenegro', 'EMEA', FALSE),
                                                                       ('MF', 'Saint Martin', 'LATAM', FALSE),
                                                                       ('MG', 'Madagascar', 'EMEA', FALSE),
                                                                       ('MH', 'Marshall Islands', 'APAC', FALSE),
                                                                       ('MK', 'North Macedonia', 'EMEA', FALSE),
                                                                       ('ML', 'Mali', 'EMEA', FALSE),
                                                                       ('MM', 'Myanmar', 'APAC', FALSE),
                                                                       ('MN', 'Mongolia', 'APAC', FALSE),
                                                                       ('MO', 'Macao', 'APAC', FALSE),
                                                                       ('MP', 'Northern Mariana Islands', 'APAC', FALSE),
                                                                       ('MQ', 'Martinique', 'LATAM', FALSE),
                                                                       ('MR', 'Mauritania', 'EMEA', FALSE),
                                                                       ('MS', 'Montserrat', 'LATAM', FALSE),
                                                                       ('MT', 'Malta', 'EMEA', FALSE),
                                                                       ('MU', 'Mauritius', 'EMEA', FALSE),
                                                                       ('MV', 'Maldives', 'APAC', FALSE),
                                                                       ('MW', 'Malawi', 'EMEA', FALSE),
                                                                       ('MX', 'Mexico', 'LATAM', FALSE),
                                                                       ('MY', 'Malaysia', 'APAC', FALSE),
                                                                       ('MZ', 'Mozambique', 'EMEA', FALSE),
                                                                       ('NA', 'Namibia', 'EMEA', FALSE),
                                                                       ('NC', 'New Caledonia', 'APAC', FALSE),
                                                                       ('NE', 'Niger', 'EMEA', FALSE),
                                                                       ('NF', 'Norfolk Island', 'APAC', FALSE),
                                                                       ('NG', 'Nigeria', 'EMEA', FALSE),
                                                                       ('NI', 'Nicaragua', 'LATAM', FALSE),
                                                                       ('NL', 'Netherlands', 'EMEA', FALSE),
                                                                       ('NO', 'Norway', 'EMEA', FALSE),
                                                                       ('NP', 'Nepal', 'APAC', FALSE),
                                                                       ('NR', 'Nauru', 'APAC', FALSE),
                                                                       ('NU', 'Niue', 'APAC', FALSE),
                                                                       ('NZ', 'New Zealand', 'APAC', FALSE),
                                                                       ('OM', 'Oman', 'EMEA', FALSE),
                                                                       ('PA', 'Panama', 'LATAM', FALSE),
                                                                       ('PE', 'Peru', 'LATAM', FALSE),
                                                                       ('PF', 'French Polynesia', 'APAC', FALSE),
                                                                       ('PG', 'Papua New Guinea', 'APAC', FALSE),
                                                                       ('PH', 'Philippines', 'APAC', FALSE),
                                                                       ('PK', 'Pakistan', 'APAC', FALSE),
                                                                       ('PL', 'Poland', 'EMEA', FALSE),
                                                                       ('PM', 'Saint Pierre and Miquelon', 'NAM', FALSE),
                                                                       ('PN', 'Pitcairn', 'APAC', FALSE),
                                                                       ('PR', 'Puerto Rico', 'LATAM', FALSE),
                                                                       ('PS', 'Palestine', 'EMEA', FALSE),
                                                                       ('PT', 'Portugal', 'EMEA', FALSE),
                                                                       ('PW', 'Palau', 'APAC', FALSE),
                                                                       ('PY', 'Paraguay', 'LATAM', FALSE),
                                                                       ('QA', 'Qatar', 'EMEA', FALSE),
                                                                       ('RE', 'Réunion', 'EMEA', FALSE),
                                                                       ('RO', 'Romania', 'EMEA', FALSE),
                                                                       ('RS', 'Serbia', 'EMEA', FALSE),
                                                                       ('RU', 'Russian Federation', 'EMEA', FALSE),
                                                                       ('RW', 'Rwanda', 'EMEA', FALSE),
                                                                       ('SA', 'Saudi Arabia', 'EMEA', FALSE),
                                                                       ('SB', 'Solomon Islands', 'APAC', FALSE),
                                                                       ('SC', 'Seychelles', 'EMEA', FALSE),
                                                                       ('SD', 'Sudan', 'EMEA', FALSE),
                                                                       ('SE', 'Sweden', 'EMEA', FALSE),
                                                                       ('SG', 'Singapore', 'APAC', FALSE),
                                                                       ('SH', 'Saint Helena', 'EMEA', FALSE),
                                                                       ('SI', 'Slovenia', 'EMEA', FALSE),
                                                                       ('SJ', 'Svalbard and Jan Mayen', 'EMEA', FALSE),
                                                                       ('SK', 'Slovakia', 'EMEA', FALSE),
                                                                       ('SL', 'Sierra Leone', 'EMEA', FALSE),
                                                                       ('SM', 'San Marino', 'EMEA', FALSE),
                                                                       ('SN', 'Senegal', 'EMEA', FALSE),
                                                                       ('SO', 'Somalia', 'EMEA', FALSE),
                                                                       ('SR', 'Suriname', 'LATAM', FALSE),
                                                                       ('SS', 'South Sudan', 'EMEA', FALSE),
                                                                       ('ST', 'Sao Tome and Principe', 'EMEA', FALSE),
                                                                       ('SV', 'El Salvador', 'LATAM', FALSE),
                                                                       ('SX', 'Sint Maarten', 'LATAM', FALSE),
                                                                       ('SY', 'Syrian Arab Republic', 'EMEA', FALSE),
                                                                       ('SZ', 'Eswatini', 'EMEA', FALSE),
                                                                       ('TC', 'Turks and Caicos Islands', 'LATAM', FALSE),
                                                                       ('TD', 'Chad', 'EMEA', FALSE),
                                                                       ('TF', 'French Southern Territories', 'EMEA', FALSE),
                                                                       ('TG', 'Togo', 'EMEA', FALSE),
                                                                       ('TH', 'Thailand', 'APAC', FALSE),
                                                                       ('TJ', 'Tajikistan', 'EMEA', FALSE),
                                                                       ('TK', 'Tokelau', 'APAC', FALSE),
                                                                       ('TL', 'Timor-Leste', 'APAC', FALSE),
                                                                       ('TM', 'Turkmenistan', 'EMEA', FALSE),
                                                                       ('TN', 'Tunisia', 'EMEA', FALSE),
                                                                       ('TO', 'Tonga', 'APAC', FALSE),
                                                                       ('TR', 'Turkey', 'EMEA', FALSE),
                                                                       ('TT', 'Trinidad and Tobago', 'LATAM', FALSE),
                                                                       ('TV', 'Tuvalu', 'APAC', FALSE),
                                                                       ('TW', 'Taiwan', 'APAC', FALSE),
                                                                       ('TZ', 'Tanzania', 'EMEA', FALSE),
                                                                       ('UA', 'Ukraine', 'EMEA', FALSE),
                                                                       ('UG', 'Uganda', 'EMEA', FALSE),
                                                                       ('UM', 'United States Minor Outlying Islands', 'APAC', FALSE),
                                                                       ('US', 'United States', 'NAM', FALSE),
                                                                       ('UY', 'Uruguay', 'LATAM', FALSE),
                                                                       ('UZ', 'Uzbekistan', 'EMEA', FALSE),
                                                                       ('VA', 'Vatican City', 'EMEA', FALSE),
                                                                       ('VC', 'Saint Vincent and the Grenadines', 'LATAM', FALSE),
                                                                       ('VE', 'Venezuela', 'LATAM', FALSE),
                                                                       ('VG', 'Virgin Islands, British', 'LATAM', FALSE),
                                                                       ('VI', 'Virgin Islands, U.S.', 'LATAM', FALSE),
                                                                       ('VN', 'Viet Nam', 'APAC', FALSE),
                                                                       ('VU', 'Vanuatu', 'APAC', FALSE),
                                                                       ('WF', 'Wallis and Futuna', 'APAC', FALSE),
                                                                       ('WS', 'Samoa', 'APAC', FALSE),
                                                                       ('YE', 'Yemen', 'EMEA', FALSE),
                                                                       ('YT', 'Mayotte', 'EMEA', FALSE),
                                                                       ('ZA', 'South Africa', 'EMEA', FALSE),
                                                                       ('ZM', 'Zambia', 'EMEA', FALSE),
                                                                       ('ZW', 'Zimbabwe', 'EMEA', FALSE),
                                                                       ('XK', 'Kosovo', 'EMEA', FALSE);

-- =============================================================================
-- 3. INSERT COUNTRY PROPERTIES
-- =============================================================================

-- Note: Uses the currently valid property set (state = 'VALID' and within date range)
-- If no valid property set exists, these inserts will fail with NULL constraint violation
-- To create a new property set if none exists, uncomment the following:
-- INSERT INTO `property_set` (`start_date`, `state`) VALUES (NOW(), 'VALID');

-- Note: Using current valid property set
-- Customs Union Properties (only for EU countries)
INSERT INTO `country_property`
(`country_id`, `country_property_type_id`, `property_set_id`, `property_value`)
SELECT
    c.id,
    cpt.id,
    (SELECT ps.id FROM `property_set` ps
     WHERE ps.state = 'VALID'
       AND ps.start_date <= NOW()
       AND (ps.end_date IS NULL OR ps.end_date > NOW())
     ORDER BY ps.start_date DESC
     LIMIT 1),
    CASE
        WHEN c.iso_code IN ('AT', 'BE', 'BG', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'FR', 'GR', 'HR', 'HU', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'PL', 'PT', 'RO', 'SE', 'SI', 'SK')
            THEN 'EU'
        ELSE 'NONE'
        END
FROM `country` c, `country_property_type` cpt
WHERE cpt.external_mapping_id = 'UNION';

-- Safety Stock Properties
INSERT INTO `country_property`
(`country_id`, `country_property_type_id`, `property_set_id`, `property_value`)
SELECT
    c.id,
    cpt.id,
    (SELECT ps.id FROM `property_set` ps
     WHERE ps.state = 'VALID'
       AND ps.start_date <= NOW()
       AND (ps.end_date IS NULL OR ps.end_date > NOW())
     ORDER BY ps.start_date DESC
     LIMIT 1),
    CASE c.iso_code
            WHEN 'AD' THEN '15'
            WHEN 'AE' THEN '20'
            WHEN 'AF' THEN '30'
            WHEN 'AG' THEN '55'
            WHEN 'AI' THEN '55'
            WHEN 'AL' THEN '15'
            WHEN 'AM' THEN '15'
            WHEN 'AO' THEN '15'
            WHEN 'AQ' THEN '55'
            WHEN 'AR' THEN '55'
            WHEN 'AS' THEN '55'
            WHEN 'AT' THEN '10'
            WHEN 'AU' THEN '55'
            WHEN 'AW' THEN '55'
            WHEN 'AZ' THEN '15'
            WHEN 'BA' THEN '15'
            WHEN 'BB' THEN '55'
            WHEN 'BD' THEN '55'
            WHEN 'BE' THEN '10'
            WHEN 'BF' THEN '30'
            WHEN 'BG' THEN '10'
            WHEN 'BH' THEN '20'
            WHEN 'BI' THEN '30'
            WHEN 'BJ' THEN '30'
            WHEN 'BL' THEN '30'
            WHEN 'BM' THEN '55'
            WHEN 'BN' THEN '55'
            WHEN 'BO' THEN '55'
            WHEN 'BQ' THEN '55'
            WHEN 'BR' THEN '55'
            WHEN 'BS' THEN '55'
            WHEN 'BT' THEN '55'
            WHEN 'BV' THEN '30'
            WHEN 'BW' THEN '15'
            WHEN 'BY' THEN '55'
            WHEN 'BZ' THEN '55'
            WHEN 'CA' THEN '55'
            WHEN 'CC' THEN '55'
            WHEN 'CD' THEN '30'
            WHEN 'CF' THEN '30'
            WHEN 'CG' THEN '30'
            WHEN 'CH' THEN '10'
            WHEN 'CI' THEN '30'
            WHEN 'CK' THEN '30'
            WHEN 'CL' THEN '55'
            WHEN 'CM' THEN '30'
            WHEN 'CN' THEN '55'
            WHEN 'CO' THEN '55'
            WHEN 'CR' THEN '55'
            WHEN 'CU' THEN '55'
            WHEN 'CV' THEN '30'
            WHEN 'CW' THEN '30'
            WHEN 'CX' THEN '55'
            WHEN 'CY' THEN '10'
            WHEN 'CZ' THEN '10'
            WHEN 'DE' THEN '10'
            WHEN 'DJ' THEN '30'
            WHEN 'DK' THEN '10'
            WHEN 'DM' THEN '55'
            WHEN 'DO' THEN '55'
            WHEN 'DZ' THEN '10'
            WHEN 'EC' THEN '55'
            WHEN 'EE' THEN '10'
            WHEN 'EG' THEN '30'
            WHEN 'EH' THEN '30'
            WHEN 'ER' THEN '30'
            WHEN 'ES' THEN '10'
            WHEN 'ET' THEN '30'
            WHEN 'FI' THEN '10'
            WHEN 'FJ' THEN '55'
            WHEN 'FK' THEN '55'
            WHEN 'FM' THEN '55'
            WHEN 'FO' THEN '30'
            WHEN 'FR' THEN '10'
            WHEN 'GA' THEN '30'
            WHEN 'GB' THEN '30'
            WHEN 'GD' THEN '55'
            WHEN 'GE' THEN '10'
            WHEN 'GF' THEN '30'
            WHEN 'GG' THEN '30'
            WHEN 'GH' THEN '30'
            WHEN 'GI' THEN '10'
            WHEN 'GL' THEN '30'
            WHEN 'GM' THEN '30'
            WHEN 'GN' THEN '30'
            WHEN 'GP' THEN '30'
            WHEN 'GQ' THEN '30'
            WHEN 'GR' THEN '10'
            WHEN 'GS' THEN '55'
            WHEN 'GT' THEN '55'
            WHEN 'GU' THEN '55'
            WHEN 'GW' THEN '30'
            WHEN 'GY' THEN '55'
            WHEN 'HK' THEN '55'
            WHEN 'HM' THEN '30'
            WHEN 'HN' THEN '55'
            WHEN 'HR' THEN '10'
            WHEN 'HT' THEN '55'
            WHEN 'HU' THEN '10'
            WHEN 'ID' THEN '55'
            WHEN 'IE' THEN '10'
            WHEN 'IL' THEN '30'
            WHEN 'IM' THEN '30'
            WHEN 'IN' THEN '55'
            WHEN 'IO' THEN '55'
            WHEN 'IQ' THEN '30'
            WHEN 'IR' THEN '30'
            WHEN 'IS' THEN '20'
            WHEN 'IT' THEN '10'
            WHEN 'JE' THEN '30'
            WHEN 'JM' THEN '55'
            WHEN 'JO' THEN '30'
            WHEN 'JP' THEN '55'
            WHEN 'KE' THEN '30'
            WHEN 'KG' THEN '30'
            WHEN 'KH' THEN '55'
            WHEN 'KI' THEN '55'
            WHEN 'KM' THEN '30'
            WHEN 'KN' THEN '55'
            WHEN 'KP' THEN '55'
            WHEN 'KR' THEN '55'
            WHEN 'KW' THEN '30'
            WHEN 'KY' THEN '55'
            WHEN 'KZ' THEN '30'
            WHEN 'LA' THEN '55'
            WHEN 'LB' THEN '30'
            WHEN 'LC' THEN '55'
            WHEN 'LI' THEN '10'
            WHEN 'LK' THEN '55'
            WHEN 'LR' THEN '30'
            WHEN 'LS' THEN '30'
            WHEN 'LT' THEN '10'
            WHEN 'LU' THEN '10'
            WHEN 'LV' THEN '10'
            WHEN 'LY' THEN '30'
            WHEN 'MA' THEN '20'
            WHEN 'MC' THEN '30'
            WHEN 'MD' THEN '30'
            WHEN 'ME' THEN '30'
            WHEN 'MF' THEN '30'
            WHEN 'MG' THEN '30'
            WHEN 'MH' THEN '55'
            WHEN 'MK' THEN '30'
            WHEN 'ML' THEN '30'
            WHEN 'MM' THEN '55'
            WHEN 'MN' THEN '55'
            WHEN 'MO' THEN '55'
            WHEN 'MP' THEN '55'
            WHEN 'MQ' THEN '30'
            WHEN 'MR' THEN '30'
            WHEN 'MS' THEN '55'
            WHEN 'MT' THEN '10'
            WHEN 'MU' THEN '30'
            WHEN 'MV' THEN '55'
            WHEN 'MW' THEN '30'
            WHEN 'MX' THEN '55'
            WHEN 'MY' THEN '55'
            WHEN 'MZ' THEN '30'
            WHEN 'NA' THEN '30'
            WHEN 'NC' THEN '30'
            WHEN 'NE' THEN '30'
            WHEN 'NF' THEN '55'
            WHEN 'NG' THEN '30'
            WHEN 'NI' THEN '55'
            WHEN 'NL' THEN '10'
            WHEN 'NO' THEN '10'
            WHEN 'NP' THEN '55'
            WHEN 'NR' THEN '55'
            WHEN 'NU' THEN '55'
            WHEN 'NZ' THEN '55'
            WHEN 'OM' THEN '30'
            WHEN 'PA' THEN '55'
            WHEN 'PE' THEN '55'
            WHEN 'PF' THEN '30'
            WHEN 'PG' THEN '55'
            WHEN 'PH' THEN '55'
            WHEN 'PK' THEN '55'
            WHEN 'PL' THEN '10'
            WHEN 'PM' THEN '30'
            WHEN 'PN' THEN '55'
            WHEN 'PR' THEN '55'
            WHEN 'PS' THEN '30'
            WHEN 'PT' THEN '10'
            WHEN 'PW' THEN '55'
            WHEN 'PY' THEN '55'
            WHEN 'QA' THEN '30'
            WHEN 'RE' THEN '30'
            WHEN 'RO' THEN '10'
            WHEN 'RS' THEN '10'
            WHEN 'RU' THEN '30'
            WHEN 'RW' THEN '30'
            WHEN 'SA' THEN '30'
            WHEN 'SB' THEN '55'
            WHEN 'SC' THEN '30'
            WHEN 'SD' THEN '30'
            WHEN 'SE' THEN '10'
            WHEN 'SG' THEN '55'
            WHEN 'SH' THEN '30'
            WHEN 'SI' THEN '10'
            WHEN 'SJ' THEN '55'
            WHEN 'SK' THEN '10'
            WHEN 'SL' THEN '30'
            WHEN 'SM' THEN '30'
            WHEN 'SN' THEN '30'
            WHEN 'SO' THEN '30'
            WHEN 'SR' THEN '55'
            WHEN 'SS' THEN '30'
            WHEN 'ST' THEN '30'
            WHEN 'SV' THEN '55'
            WHEN 'SX' THEN '30'
            WHEN 'SY' THEN '30'
            WHEN 'SZ' THEN '30'
            WHEN 'TC' THEN '55'
            WHEN 'TD' THEN '30'
            WHEN 'TF' THEN '30'
            WHEN 'TG' THEN '30'
            WHEN 'TH' THEN '55'
            WHEN 'TJ' THEN '30'
            WHEN 'TK' THEN '55'
            WHEN 'TL' THEN '55'
            WHEN 'TM' THEN '30'
            WHEN 'TN' THEN '30'
            WHEN 'TO' THEN '55'
            WHEN 'TR' THEN '15'
            WHEN 'TT' THEN '55'
            WHEN 'TV' THEN '55'
            WHEN 'TW' THEN '55'
            WHEN 'TZ' THEN '30'
            WHEN 'UA' THEN '55'
            WHEN 'UG' THEN '30'
            WHEN 'UM' THEN '55'
            WHEN 'US' THEN '55'
            WHEN 'UY' THEN '55'
            WHEN 'UZ' THEN '30'
            WHEN 'VA' THEN '30'
            WHEN 'VC' THEN '55'
            WHEN 'VE' THEN '55'
            WHEN 'VG' THEN '55'
            WHEN 'VI' THEN '55'
            WHEN 'VN' THEN '55'
            WHEN 'VU' THEN '55'
            WHEN 'WF' THEN '30'
            WHEN 'WS' THEN '55'
            WHEN 'YE' THEN '30'
            WHEN 'YT' THEN '30'
            WHEN 'ZA' THEN '30'
            WHEN 'ZM' THEN '30'
            WHEN 'ZW' THEN '30'
            WHEN 'XK' THEN '55'
        END
FROM `country` c, `country_property_type` cpt
WHERE cpt.external_mapping_id = 'SAFETY_STOCK';

-- Air Freight Share Properties (0.03 for countries with safety stock 55, otherwise 0%)
INSERT INTO `country_property`
(`country_id`, `country_property_type_id`, `property_set_id`, `property_value`)
SELECT
    c.id,
    cpt.id,
    (SELECT ps.id FROM `property_set` ps
     WHERE ps.state = 'VALID'
       AND ps.start_date <= NOW()
       AND (ps.end_date IS NULL OR ps.end_date > NOW())
     ORDER BY ps.start_date DESC
     LIMIT 1),
    CASE
        WHEN cp_safety.property_value = '55' THEN '0.03'
        ELSE '0'
        END
FROM `country` c
         CROSS JOIN `country_property_type` cpt
         LEFT JOIN `country_property` cp_safety
                   ON cp_safety.country_id = c.id
                       AND cp_safety.country_property_type_id = (
                           SELECT id FROM `country_property_type`
                           WHERE external_mapping_id = 'SAFETY_STOCK'
                       )
                       AND cp_safety.property_set_id = (
                           SELECT ps.id FROM `property_set` ps
                           WHERE ps.state = 'VALID'
                             AND ps.start_date <= NOW()
                             AND (ps.end_date IS NULL OR ps.end_date > NOW())
                           ORDER BY ps.start_date DESC
                           LIMIT 1
                       )
WHERE cpt.external_mapping_id = 'AIR_SHARE';

-- Wage Factor Properties (only for countries with defined values)
-- Wage Factor Properties (only for countries with defined values)
INSERT INTO `country_property`
(`country_id`, `country_property_type_id`, `property_set_id`, `property_value`)
SELECT
    c.id,
    cpt.id,
    (SELECT ps.id FROM `property_set` ps
     WHERE ps.state = 'VALID'
       AND ps.start_date <= NOW()
       AND (ps.end_date IS NULL OR ps.end_date > NOW())
     ORDER BY ps.start_date DESC
     LIMIT 1),
    CASE c.iso_code
        WHEN 'AT' THEN '0.99'
        WHEN 'BE' THEN '1.14'
        WHEN 'BG' THEN '0.23'
        WHEN 'CZ' THEN '0.44'
        WHEN 'DE' THEN '1.00'
        WHEN 'DK' THEN '1.16'
        WHEN 'EE' THEN '0.60'
        WHEN 'ES' THEN '0.90'
        WHEN 'FI' THEN '1.02'
        WHEN 'FR' THEN '1.05'
        WHEN 'GR' THEN '0.35'
        WHEN 'HR' THEN '0.31'
        WHEN 'HU' THEN '0.35'
        WHEN 'IE' THEN '0.97'
        WHEN 'IT' THEN '0.72'
        WHEN 'LT' THEN '0.36'
        WHEN 'LU' THEN '1.31'
        WHEN 'LV' THEN '0.33'
        WHEN 'MT' THEN '0.41'
        WHEN 'NL' THEN '1.05'
        WHEN 'PL' THEN '0.27'
        WHEN 'PT' THEN '0.41'
        WHEN 'RO' THEN '0.27'
        WHEN 'SE' THEN '0.94'
        WHEN 'SI' THEN '0.62'
        WHEN 'SK' THEN '0.42'
        ELSE '1'
        END
FROM `country` c, `country_property_type` cpt
WHERE cpt.external_mapping_id = 'WAGE';

-- =============================================================================
-- VERIFICATION QUERIES (Optional - for testing)
-- =============================================================================

-- Verify country count
-- SELECT COUNT(*) as total_countries FROM country;

-- Verify property types
-- SELECT * FROM country_property_type;

-- Verify EU countries with all properties
-- SELECT
--     c.iso_code,
--     c.region_code,
--     MAX(CASE WHEN cpt.name = 'Customs Union' THEN cp.property_value END) as customs_union,
--     MAX(CASE WHEN cpt.name = 'Safety Stock' THEN cp.property_value END) as safety_stock,
--     MAX(CASE WHEN cpt.name = 'Air Freight Share' THEN cp.property_value END) as air_freight,
--     MAX(CASE WHEN cpt.name = 'Wage Factor' THEN cp.property_value END) as wage_factor
-- FROM country c
-- JOIN country_property cp ON c.id = cp.country_id
-- JOIN country_property_type cpt ON cp.country_property_type_id = cpt.id
-- WHERE c.iso_code IN ('DE', 'FR', 'AT', 'BE', 'NL')
-- GROUP BY c.id, c.iso_code, c.region_code
-- ORDER BY c.iso_code;