-- P5-6694 Belgian fallback locales

-- Make Dutch the fallback locale of Belgian Dutch
UPDATE locales SET fallback_id = (SELECT id from locales where value = 'nl_NL') WHERE value = 'nl_BE';

-- Make French the fallback locale of Belgian French
UPDATE locales SET fallback_id = (SELECT id from locales where value = 'fr_FR') WHERE value = 'fr_BE';

-- Delete all text values with identical fallback versions
with rows_to_delete as (
	select
		texts_to_delete.textid,
		texts_to_delete.locale_id
	from
		"i18n-texts_data" texts_to_delete
		join locales locale_for_deleted on texts_to_delete.locale_id = locale_for_deleted.id
		join "i18n-texts_data" texts_fallback on texts_to_delete.textid = texts_fallback.textid and locale_for_deleted.fallback_id = texts_fallback.locale_id
		join "i18n-texts" on texts_to_delete.textid = "i18n-texts".textid
	where
		locale_for_deleted.fallback_id is not null
		and texts_to_delete.text = texts_fallback.text
		-- stock product titles are not fallback aware
		and not (category = 'productTitles' and "i18n-texts".name in (select path from "product-stockproducts" join "product-products" using (productid)))
		
)
DELETE
	FROM "i18n-texts_data" texts_data
	USING rows_to_delete
	WHERE texts_data.textid = rows_to_delete.textid AND texts_data.locale_id = rows_to_delete.locale_id
;

