-- PW-1609: clean up state order field


-- delete state from non US-orders
DELETE FROM "order-orders_fields"
WHERE fieldid = 235 AND orderid IN (
    SELECT orders.orderid FROM "order-orders" orders
    JOIN "order-orders_fields" countryCode ON countryCode.fieldid = 151 AND countryCode.orderid = orders.orderid
    WHERE countryCode.value != 'US'
);


UPDATE "order-orders_fields" SET value = 'Arizona' WHERE fieldid = 235 AND value IN ('az', 'AZ');
UPDATE "order-orders_fields" SET value = 'California' WHERE fieldid = 235 AND value IN ('ca', 'Ca', 'CA', 'california', 'CALIFORNIA', 'California ');
UPDATE "order-orders_fields" SET value = 'Colorado' WHERE fieldid = 235 AND value IN ('co', 'CO', 'CO - Colorado', 'colorado', 'Colorado  U.S.A.');
UPDATE "order-orders_fields" SET value = 'Connecticut' WHERE fieldid = 235 AND value IN ('CT');
UPDATE "order-orders_fields" SET value = 'D.C' WHERE fieldid = 235 AND value IN ('DC', 'District of Columbia');
UPDATE "order-orders_fields" SET value = 'Florida' WHERE fieldid = 235 AND value IN ('Fl', 'FL', 'florida', 'FLORIDA', 'Florida USA', 'Florida ');
UPDATE "order-orders_fields" SET value = 'Georgia' WHERE fieldid = 235 AND value IN ('GA', 'Gerogia');
UPDATE "order-orders_fields" SET value = 'Idaho' WHERE fieldid = 235 AND value IN ('ID');
UPDATE "order-orders_fields" SET value = 'Illinois' WHERE fieldid = 235 AND value IN ('IL', 'illinois');
UPDATE "order-orders_fields" SET value = 'Indiana' WHERE fieldid = 235 AND value IN ('IN', 'INDIANA');
UPDATE "order-orders_fields" SET value = 'Iowa' WHERE fieldid = 235 AND value IN ('iowa');
UPDATE "order-orders_fields" SET value = 'Kansas' WHERE fieldid = 235 AND value IN ('Ks', 'KS');
UPDATE "order-orders_fields" SET value = 'Kentucky' WHERE fieldid = 235 AND value IN ('KY');
UPDATE "order-orders_fields" SET value = 'Louisiana' WHERE fieldid = 235 AND value IN ('LA');
UPDATE "order-orders_fields" SET value = 'Maryland' WHERE fieldid = 235 AND value IN ('maryland', 'MD');
UPDATE "order-orders_fields" SET value = 'Massachusetts' WHERE fieldid = 235 AND value IN ('ma', 'Ma', 'MA', 'Massachusetts ');
UPDATE "order-orders_fields" SET value = 'Michigan' WHERE fieldid = 235 AND value IN ('MI', 'MI 48073', 'michigan', 'Michigan [MI]');
UPDATE "order-orders_fields" SET value = 'Minnesota' WHERE fieldid = 235 AND value IN ('Mn', 'MN');
UPDATE "order-orders_fields" SET value = 'Missouri' WHERE fieldid = 235 AND value IN ('missouri,  USA', 'mo', 'MO');
UPDATE "order-orders_fields" SET value = 'Montana' WHERE fieldid = 235 AND value IN ('MONTANA');
UPDATE "order-orders_fields" SET value = 'New Hampshire' WHERE fieldid = 235 AND value IN ('new Hamshire');
UPDATE "order-orders_fields" SET value = 'New Jersey' WHERE fieldid = 235 AND value IN ('New jersey', 'nj', 'NJ', 'New Jersey ');
UPDATE "order-orders_fields" SET value = 'New York' WHERE fieldid = 235 AND value IN ('new york', 'New york', 'NEW YORK', 'ny', 'Ny', 'NY', 'New York ');
UPDATE "order-orders_fields" SET value = 'North Carolina' WHERE fieldid = 235 AND value IN ('NC');
UPDATE "order-orders_fields" SET value = 'Ohio' WHERE fieldid = 235 AND value IN ('ohio', 'Ohio 45014', 'OH', 'Oh');
UPDATE "order-orders_fields" SET value = 'Oregon' WHERE fieldid = 235 AND value IN ('OR');
UPDATE "order-orders_fields" SET value = 'Pennsylvania' WHERE fieldid = 235 AND value IN ('PENNSYLVANIA', 'Pa', 'PA', 'Pa ');
UPDATE "order-orders_fields" SET value = 'South Carolina' WHERE fieldid = 235 AND value IN ('SC');
UPDATE "order-orders_fields" SET value = 'Texas' WHERE fieldid = 235 AND value IN ('texas', 'tx', 'Tx', 'TX');
UPDATE "order-orders_fields" SET value = 'Utah' WHERE fieldid = 235 AND value IN ('UT');
UPDATE "order-orders_fields" SET value = 'Vermont' WHERE fieldid = 235 AND value IN ('VT');
UPDATE "order-orders_fields" SET value = 'Virginia' WHERE fieldid = 235 AND value IN ('va', 'VA');
UPDATE "order-orders_fields" SET value = 'Washington' WHERE fieldid = 235 AND value IN ('WA', 'Wa');
UPDATE "order-orders_fields" SET value = 'West Virginia' WHERE fieldid = 235 AND value IN ('WI');


-- --delete unknown states
DELETE FROM "order-orders_fields" WHERE fieldid = 235 AND value IN (
    '',
    ' ',
    'Auckland',
    'AE',
    'amazonas',
    'DURANGO',
    'IA',
    'Lagos',
    'Lagos ',
    'New Brunswick',
    'Nuevo León',
    'NV',
    'ON',
    'ontario',
    'Ontario',
    'Pondicherry',
    'Puerto Rico',
    'Sverige',
    'Suffolk'
);
