-- One resource row holds all languages (title / description / body per locale).

ALTER TABLE resources ADD COLUMN IF NOT EXISTS title_zh_tw TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS title_zh_cn TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS title_en TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS description_zh_tw TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS description_zh_cn TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS description_en TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS body_text_zh_tw TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS body_text_zh_cn TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS body_text_en TEXT;

UPDATE resources SET title_zh_tw = title WHERE COALESCE(title_zh_tw, '') = '' AND COALESCE(title, '') <> '';
UPDATE resources SET description_zh_tw = description WHERE description_zh_tw IS NULL AND description IS NOT NULL;
UPDATE resources SET body_text_zh_tw = body_text WHERE body_text_zh_tw IS NULL AND body_text IS NOT NULL;

UPDATE resources SET title_zh_cn = title WHERE language = 'zh-CN' AND COALESCE(title_zh_cn, '') = '';
UPDATE resources SET description_zh_cn = description WHERE language = 'zh-CN' AND description_zh_cn IS NULL;
UPDATE resources SET body_text_zh_cn = body_text WHERE language = 'zh-CN' AND body_text_zh_cn IS NULL;

UPDATE resources SET title_en = title WHERE language = 'en' AND COALESCE(title_en, '') = '';
UPDATE resources SET description_en = description WHERE language = 'en' AND description_en IS NULL;
UPDATE resources SET body_text_en = body_text WHERE language = 'en' AND body_text_en IS NULL;

UPDATE resources SET title = COALESCE(NULLIF(title_zh_tw, ''), title);
UPDATE resources SET description = description_zh_tw;
UPDATE resources SET body_text = body_text_zh_tw;
