-- Add Switzerland sites

-- Add currency
INSERT INTO "i18n-currencies" (iso3char, symbol, fractional_parts, exchange_rate, locale)
    VALUES ('CHF', 'fr.', 0, 0.0985, 'de_CH.UTF-8');
INSERT INTO "product-currencies" (iso3char, exchange_rate) VALUES ('CHF', 0.0985);

-- Add locales, market, market locales
INSERT INTO locales (value, name, fallback_id) VALUES
    ('de_CH', 'German (Switzerland)', (select id from locales where value = 'de_DE')),
    ('fr_CH', 'French (Switzerland)', (select id from locales where value = 'fr_FR')),
    ('it_CH', 'Italian (Switzerland)', (select id from locales where value = 'it_IT'));

INSERT INTO markets (name, vat, currency, price_adjustment) VALUES ('CH', 1.077, 'CHF', 1.1);

INSERT INTO market_locales (market_id, locale_id, name) VALUES
    ((SELECT id FROM markets WHERE name = 'CH'), (SELECT id FROM locales WHERE value = 'de_CH'), 'Switzerland (German)'),
    ((SELECT id FROM markets WHERE name = 'CH'), (SELECT id FROM locales WHERE value = 'fr_CH'), 'Switzerland (French)'),
    ((SELECT id FROM markets WHERE name = 'CH'), (SELECT id FROM locales WHERE value = 'it_CH'), 'Switzerland (Italian)');


-- Copy collections
INSERT INTO collections_texts (collection_id, meta_title, meta_description, title, description, slug, locale_id)
    SELECT collection_id, meta_title, meta_description, title, description, slug, (SELECT id FROM locales WHERE value = 'de_CH')
    FROM collections_texts WHERE locale_id = (SELECT id FROM locales WHERE value = 'de_DE');

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

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


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

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

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


-- Copy category data (page titles, meta descriptions), and category texts
INSERT INTO categorydata (category_id, datakey_id, text, locale_id)
    SELECT category_id, datakey_id, text, (SELECT id FROM locales WHERE value = 'de_CH')
    FROM categorydata WHERE locale_id = (SELECT id FROM locales WHERE value = 'de_DE');

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

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

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 = 'de_CH'), 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 = 'de_DE');

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 = 'fr_CH'), 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 = 'fr_FR');

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 = 'it_CH'), 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 = 'it_IT');


-- Copy blacklisted products
INSERT INTO product_blacklist (product_id, group_id, market_id)
    SELECT product_id, group_id, (select id from markets where name = 'CH')
    FROM product_blacklist WHERE market_id = (select id from markets where name = 'GLOBAL');


-- Add delivery methods
INSERT INTO "delivery-methods_prices" (territory, currency, option, option_value, price, method) VALUES
    ('CH', 'CHF', '', '', 0, (select id from "delivery-methods" where name = 'samplesDelivery')),
    ('CH', 'CHF', '', '', 0, (select id from "delivery-methods" where name = 'dhlEconomyWorld')),
    ('CH', 'CHF', '', '', (SELECT 35 / vat FROM markets WHERE name = 'CH'), (select id from "delivery-methods" where name = 'dhl'));


-- Add invoice sequence
CREATE SEQUENCE "order-invoiceIdCH" start 1000;


-- Copy popular links text tags to override the fallback values (because the prefix will be wrong)
INSERT INTO "i18n-texts_data" (textid, text, locale_id)
    SELECT textid, replace(text, 'href="/', 'href="/ch-de/'), (select id from locales where value = 'de_CH')
    FROM "i18n-texts" join "i18n-texts_data" using (textid)
    where name ilike 'popular-links-%' and locale_id = (select id from locales where value = 'de_DE');

INSERT INTO "i18n-texts_data" (textid, text, locale_id)
    SELECT textid, replace(text, 'href="/', 'href="/ch-fr/'), (select id from locales where value = 'fr_CH')
    FROM "i18n-texts" join "i18n-texts_data" using (textid)
    where name ilike 'popular-links-%' and locale_id = (select id from locales where value = 'fr_FR');

INSERT INTO "i18n-texts_data" (textid, text, locale_id)
    SELECT textid, replace(text, '/it/', '/ch-it/'), (select id from locales where value = 'it_CH')
    FROM "i18n-texts" join "i18n-texts_data" using (textid)
    where name ilike 'popular-links-%' and locale_id = (select id from locales where value = 'it_IT');

