-- P5-6138: populate locale and market for old orders

-- austria
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 257 AS fieldid, 'de_AT' AS locale FROM orders WHERE locale IS NULL AND country_code = 'AT';
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 256 AS fieldid, 'AT' AS market FROM orders WHERE market IS NULL AND country_code = 'AT';

-- denmark
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 257 AS fieldid, 'da_DK' AS locale FROM orders WHERE locale IS NULL AND country_code = 'DK';
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 256 AS fieldid, 'DK' AS market FROM orders WHERE market IS NULL AND country_code = 'DK';

-- finland
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 257 AS fieldid, 'fi_FI' AS locale FROM orders WHERE locale IS NULL AND country_code = 'FI';
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 256 AS fieldid, 'FI' AS market FROM orders WHERE market IS NULL AND country_code = 'FI';

-- france
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 257 AS fieldid, 'fr_FR' AS locale FROM orders WHERE locale IS NULL AND country_code = 'FR';
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 256 AS fieldid, 'FR' AS market FROM orders WHERE market IS NULL AND country_code = 'FR';

-- germany
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 257 AS fieldid, 'de_DE' AS locale FROM orders WHERE locale IS NULL AND country_code = 'DE';
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 256 AS fieldid, 'DE' AS market FROM orders WHERE market IS NULL AND country_code = 'DE';

-- netherlands
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 257 AS fieldid, 'nl_NL' AS locale FROM orders WHERE locale IS NULL AND country_code = 'NL';
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 256 AS fieldid, 'NL' AS market FROM orders WHERE market IS NULL AND country_code = 'NL';

-- norway
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 257 AS fieldid, 'nn_NO' AS locale FROM orders WHERE locale IS NULL AND country_code = 'NO';
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 256 AS fieldid, 'NO' AS market FROM orders WHERE market IS NULL AND country_code = 'NO';

-- poland
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 257 AS fieldid, 'pl_PL' AS locale FROM orders WHERE locale IS NULL AND country_code = 'PL';
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 256 AS fieldid, 'PL' AS market FROM orders WHERE market IS NULL AND country_code = 'PL';

-- sweden
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 257 AS fieldid, 'sv_SE' AS locale FROM orders WHERE locale IS NULL AND country_code = 'SE';
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 256 AS fieldid, 'SE' AS market FROM orders WHERE market IS NULL AND country_code = 'SE';

-- spain
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 257 AS fieldid, 'es_ES' AS locale FROM orders WHERE locale IS NULL AND country_code = 'ES';
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 256 AS fieldid, 'ES' AS market FROM orders WHERE market IS NULL AND country_code = 'ES';

-- UK
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 257 AS fieldid, 'en_GB' AS locale FROM orders WHERE locale IS NULL AND country_code = 'GB';
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 256 AS fieldid, 'GB' AS market FROM orders WHERE market IS NULL AND country_code = 'GB';

-- US
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 257 AS fieldid, 'en_US' AS locale FROM orders WHERE locale IS NULL AND country_code = 'US';
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 256 AS fieldid, 'US' AS market FROM orders WHERE market IS NULL AND country_code = 'US';

-- italy after launch
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 257 AS fieldid, 'it_IT' AS locale FROM orders WHERE locale IS NULL AND country_code = 'IT' AND inserted >= '2020-09-17'::DATE;
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 256 AS fieldid, 'IT' AS market FROM orders WHERE market IS NULL AND country_code = 'IT' AND inserted >= '2020-09-17'::DATE;

-- rest is global
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 257 AS fieldid, 'en_EU' AS locale FROM orders WHERE locale IS NULL AND country_code IS NOT NULL;
INSERT INTO "order-orders_fields" (orderid, fieldid, value) SELECT id, 256 AS fieldid, 'GLOBAL' AS market FROM orders WHERE market IS NULL AND country_code IS NOT NULL;
