-- Add singapore, austrialia, ireland

-- insert new currencies.
INSERT INTO "i18n-currencies"
(iso3char, symbol, fractional_parts, exchange_rate, locale)
VALUES ('AUD', '$', 0, 0.13, 'en_AU.UTF-8');

INSERT INTO "product-currencies" (iso3char, exchange_rate) VALUES ('AUD', 0.13);


-- Add locales
INSERT INTO locales (value, name, fallback_id) VALUES ('en_IE', 'English (Ireland)', 3);
INSERT INTO locales (value, name, fallback_id) VALUES ('en_SG', 'English (Singapore)', 3);
INSERT INTO locales (value, name, fallback_id) VALUES ('en_AU', 'English (Australia)', 3);

-- Add markets
INSERT INTO markets (name, vat, currency, price_adjustment) VALUES ('IE', 1.23, 'EUR', 1.15);
INSERT INTO markets (name, vat, currency, price_adjustment) VALUES ('SG', 1.07, 'USD', 1.13);
INSERT INTO markets (name, vat, currency, price_adjustment) VALUES ('AU', 1.10, 'AUD', 1.13);



-- insert kopplingstabell
INSERT INTO market_locales (market_id, locale_id, name)
    VALUES ((SELECT id FROM markets WHERE name = 'IE'),
            (SELECT id FROM locales WHERE value = 'en_IE'),
            'Ireland');

INSERT INTO market_locales (market_id, locale_id, name)
    VALUES ((SELECT id FROM markets WHERE name = 'SG'),
            (SELECT id FROM locales WHERE value = 'en_SG'),
            'Singapore');

INSERT INTO market_locales (market_id, locale_id, name)
    VALUES ((SELECT id FROM markets WHERE name = 'AU'),
            (SELECT id FROM locales WHERE value = 'en_AU'),
            'Australia');


-- collection_texts
INSERT INTO collections_texts
   (locale_id, collection_id, meta_title, meta_description, title, description, slug)
    SELECT
      (SELECT id FROM locales WHERE value = 'en_IE'), collection_id, meta_title, meta_description, title, description, slug
    FROM collections_texts WHERE locale_id = (SELECT id FROM locales WHERE value = 'en_GB');


INSERT INTO collections_texts
   (locale_id, collection_id, meta_title, meta_description, title, description, slug)
    SELECT
      (SELECT id FROM locales WHERE value = 'en_SG'), collection_id, meta_title, meta_description, title, description, slug
    FROM collections_texts WHERE locale_id = (SELECT id FROM locales WHERE value = 'en_GB');


INSERT INTO collections_texts
   (locale_id, collection_id, meta_title, meta_description, title, description, slug)
    SELECT
      (SELECT id FROM locales WHERE value = 'en_AU'), collection_id, meta_title, meta_description, title, description, slug
    FROM collections_texts WHERE locale_id = (SELECT id FROM locales WHERE value = 'en_GB');


-- designer texts
INSERT INTO designer_texts (locale_id, designerid, header, description)
  SELECT
    (SELECT id FROM locales WHERE value = 'en_IE'), designerid, header, description
    FROM designer_texts WHERE locale_id = (SELECT id FROM locales WHERE value = 'en_GB');

INSERT INTO designer_texts (locale_id, designerid, header, description)
  SELECT
    (SELECT id FROM locales WHERE value = 'en_SG'), designerid, header, description
    FROM designer_texts WHERE locale_id = (SELECT id FROM locales WHERE value = 'en_GB');

INSERT INTO designer_texts (locale_id, designerid, header, description)
  SELECT
    (SELECT id FROM locales WHERE value = 'en_AU'), designerid, header, description
    FROM designer_texts WHERE locale_id = (SELECT id FROM locales WHERE value = 'en_GB');




-- categorydata
INSERT INTO categorydata (locale_id, category_id, datakey_id, text)
  SELECT
    (SELECT id FROM locales WHERE value = 'en_IE'), category_id, datakey_id, text
    FROM categorydata WHERE locale_id = (SELECT id FROM locales WHERE value = 'en_GB');


INSERT INTO categorydata (locale_id, category_id, datakey_id, text)
  SELECT
    (SELECT id FROM locales WHERE value = 'en_SG'), category_id, datakey_id, text
    FROM categorydata WHERE locale_id = (SELECT id FROM locales WHERE value = 'en_GB');

INSERT INTO categorydata (locale_id, category_id, datakey_id, text)
  SELECT
    (SELECT id FROM locales WHERE value = 'en_AU'), category_id, datakey_id, text
    FROM categorydata WHERE locale_id = (SELECT id FROM locales WHERE value = 'en_GB');


-- category_texts
INSERT INTO category_texts (locale_id, category_id, wallpaper_name, wallpaper_h1, wallpaper_description, canvas_name, canvas_h1, canvas_description, name, design_wallpaper_h1, design_wallpaper_description, poster_name, poster_h1, poster_description, framed_print_name, framed_print_h1, framed_print_description)
  SELECT
    (SELECT id FROM locales WHERE value = 'en_IE'), category_id, wallpaper_name, wallpaper_h1, wallpaper_description, canvas_name, canvas_h1, canvas_description, name, design_wallpaper_h1, design_wallpaper_description, poster_name, poster_h1, poster_description, framed_print_name, framed_print_h1, framed_print_description
    FROM category_texts WHERE locale_id = (SELECT id FROM locales WHERE value = 'en_GB');

INSERT INTO category_texts (locale_id, category_id, wallpaper_name, wallpaper_h1, wallpaper_description, canvas_name, canvas_h1, canvas_description, name, design_wallpaper_h1, design_wallpaper_description, poster_name, poster_h1, poster_description, framed_print_name, framed_print_h1, framed_print_description)
  SELECT
    (SELECT id FROM locales WHERE value = 'en_SG'), category_id, wallpaper_name, wallpaper_h1, wallpaper_description, canvas_name, canvas_h1, canvas_description, name, design_wallpaper_h1, design_wallpaper_description, poster_name, poster_h1, poster_description, framed_print_name, framed_print_h1, framed_print_description
    FROM category_texts WHERE locale_id = (SELECT id FROM locales WHERE value = 'en_GB');

INSERT INTO category_texts (locale_id, category_id, wallpaper_name, wallpaper_h1, wallpaper_description, canvas_name, canvas_h1, canvas_description, name, design_wallpaper_h1, design_wallpaper_description, poster_name, poster_h1, poster_description, framed_print_name, framed_print_h1, framed_print_description)
  SELECT
    (SELECT id FROM locales WHERE value = 'en_AU'), category_id, wallpaper_name, wallpaper_h1, wallpaper_description, canvas_name, canvas_h1, canvas_description, name, design_wallpaper_h1, design_wallpaper_description, poster_name, poster_h1, poster_description, framed_print_name, framed_print_h1, framed_print_description
    FROM category_texts WHERE locale_id = (SELECT id FROM locales WHERE value = 'en_GB');




-- Blacklist from global
delete from product_blacklist where market_id = (select id from markets where name = 'IE');
delete from product_blacklist where market_id = (select id from markets where name = 'SG');
delete from product_blacklist where market_id = (select id from markets where name = 'AU');

INSERT INTO product_blacklist (product_id, group_id, market_id)
    SELECT product_id, group_id, (select id from markets where name = 'IE')
    FROM product_blacklist
    where market_id = (select id from markets where name = 'GLOBAL');

INSERT INTO product_blacklist (product_id, group_id, market_id)
    SELECT product_id, group_id, (select id from markets where name = 'SG')
    FROM product_blacklist
    where market_id = (select id from markets where name = 'GLOBAL');

INSERT INTO product_blacklist (product_id, group_id, market_id)
    SELECT product_id, group_id, (select id from markets where name = 'AU')
    FROM product_blacklist
    where market_id = (select id from markets where name = 'GLOBAL');


-- Populate listprices from global (will later be regenerated by update listprice script)
INSERT INTO listprices (printproduct_id, market_id, listprice)
    SELECT printproduct_id, (select id from markets where name = 'IE'), listprice
    FROM listprices
    WHERE market_id = (select id from markets where name = 'GLOBAL') ON CONFLICT DO NOTHING;

INSERT INTO listprices (printproduct_id, market_id, listprice)
    SELECT printproduct_id, (select id from markets where name = 'SG'), listprice
    FROM listprices
    WHERE market_id = (select id from markets where name = 'GLOBAL') ON CONFLICT DO NOTHING;

INSERT INTO listprices (printproduct_id, market_id, listprice)
    SELECT printproduct_id, (select id from markets where name = 'AU'), listprice
    FROM listprices
    WHERE market_id = (select id from markets where name = 'GLOBAL') ON CONFLICT DO NOTHING;


-- Review existing delivery methods before deleting:
-- select method, name, territory, currency, price from "delivery-methods_prices" join "delivery-methods" on method=id where territory = 'IE' and currency = 'EUR';

-- Delete existing methods that may no longer be relevant
DELETE FROM "delivery-methods_prices" WHERE territory = 'AU' AND currency = 'AUD';
DELETE FROM "delivery-methods_prices" WHERE territory = 'IE' AND currency = 'EUR';
DELETE FROM "delivery-methods_prices" WHERE territory = 'SG' AND currency = 'USD';

-- Add delivery methods
INSERT INTO "delivery-methods_prices" (territory, currency, option, option_value, price, method) VALUES
    ('AU', 'AUD', '', '', 0, (select id from "delivery-methods" where name = 'samplesDelivery')),
    ('AU', 'AUD', '', '', (SELECT 9 / vat FROM markets WHERE name = 'AU'), 10), -- dhl
    ('SG', 'USD', '', '', 0, (select id from "delivery-methods" where name = 'samplesDelivery')),
    ('SG', 'USD', '', '', (SELECT 42 / vat FROM markets WHERE name = 'SG'), 10), -- dhl
    ('IE', 'EUR', '', '', 0, (select id from "delivery-methods" where name = 'samplesDelivery')),
    ('IE', 'EUR', '', '', (SELECT 7 / vat FROM markets WHERE name = 'IE'), 14), -- dhlExpress
    ('IE', 'EUR', '', '', 0, 15) -- dhlEconomyEU
;


-- Invoice sequences
CREATE SEQUENCE "order-invoiceIdIE" start 1000;
CREATE SEQUENCE "order-invoiceIdSG" start 1000;
CREATE SEQUENCE "order-invoiceIdAU" start 1000;


-- Copy popular links text tags to override the fallback values with incorrect prefixes
INSERT INTO "i18n-texts_data" (textid, text, locale_id)
    SELECT textid, replace(text, '/us/', '/au/'), (select id from locales where value = 'en_AU')
    from "i18n-texts" join "i18n-texts_data" using (textid)
    where name ilike 'popular-links-%' and locale_id = (select id from locales where value = 'en_US');

INSERT INTO "i18n-texts_data" (textid, text, locale_id)
    SELECT textid, replace(text, '/us/', '/sg/'), (select id from locales where value = 'en_SG')
    from "i18n-texts" join "i18n-texts_data" using (textid)
    where name ilike 'popular-links-%' and locale_id = (select id from locales where value = 'en_US');

INSERT INTO "i18n-texts_data" (textid, text, locale_id)
    SELECT textid, replace(text, '/us/', '/ie/'), (select id from locales where value = 'en_IE')
    from "i18n-texts" join "i18n-texts_data" using (textid)
    where name ilike 'popular-links-%' and locale_id = (select id from locales where value = 'en_US');
