-- Seven resource locales: zh (Simplified Chinese), en, ja, ko, vi, id, ms.

ALTER TABLE resources ADD COLUMN IF NOT EXISTS title_zh TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS title_ja TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS title_ko TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS title_vi TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS title_id TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS title_ms TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS description_zh TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS description_ja TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS description_ko TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS description_vi TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS description_id TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS description_ms TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS body_text_zh TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS body_text_ja TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS body_text_ko TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS body_text_vi TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS body_text_id TEXT;
ALTER TABLE resources ADD COLUMN IF NOT EXISTS body_text_ms TEXT;

UPDATE resources SET title_zh = COALESCE(NULLIF(title_zh, ''), NULLIF(title_zh_cn, ''), NULLIF(title_zh_tw, ''), title)
  WHERE COALESCE(title_zh, '') = '';
UPDATE resources SET description_zh = COALESCE(description_zh, description_zh_cn, description_zh_tw, description)
  WHERE description_zh IS NULL;
UPDATE resources SET body_text_zh = COALESCE(body_text_zh, body_text_zh_cn, body_text_zh_tw, body_text)
  WHERE body_text_zh IS NULL;

UPDATE resources SET title = COALESCE(NULLIF(title_zh, ''), title);
UPDATE resources SET description = description_zh;
UPDATE resources SET body_text = body_text_zh;
UPDATE resources SET language = 'zh' WHERE language IN ('zh-TW', 'zh-CN', 'zh-tw', 'zh-cn');
