-- Adventure log structured events (client-localized).
ALTER TABLE adventure_log
    ADD COLUMN IF NOT EXISTS event_code TEXT,
    ADD COLUMN IF NOT EXISTS event_args JSONB;

-- Stable keys for UI localization (towns / NPCs).
ALTER TABLE towns
    ADD COLUMN IF NOT EXISTS name_key TEXT;

UPDATE towns SET name_key = v.k
FROM (VALUES
    (1, 'town.willowdale.v1'),
    (2, 'town.thornwatch.v1'),
    (3, 'town.ashengard.v1'),
    (4, 'town.redcliff.v1'),
    (5, 'town.boghollow.v1'),
    (6, 'town.cinderkeep.v1'),
    (7, 'town.starfall.v1'),
    (8, 'town.mossharbor.v1'),
    (9, 'town.emberwell.v1'),
    (10, 'town.frostmark.v1'),
    (11, 'town.duskwatch.v1')
) AS v(id, k)
WHERE towns.id = v.id AND towns.name_key IS NULL;

ALTER TABLE npcs
    ADD COLUMN IF NOT EXISTS name_key TEXT;

UPDATE npcs SET name_key = v.k
FROM (VALUES
    (1, 'npc.elder_maren.v1'),
    (2, 'npc.peddler_finn.v1'),
    (3, 'npc.sister_asha.v1'),
    (4, 'npc.guard_halric.v1'),
    (5, 'npc.trader_wynn.v1'),
    (6, 'npc.scholar_orin.v1'),
    (7, 'npc.bone_merchant.v1'),
    (8, 'npc.priestess_liora.v1')
) AS v(id, k)
WHERE npcs.id = v.id AND npcs.name_key IS NULL;

-- Quest localization keys (fallback: quest.<id> set in app if null).
ALTER TABLE quests
    ADD COLUMN IF NOT EXISTS quest_key TEXT;

UPDATE quests SET quest_key = 'quest.' || id::text WHERE quest_key IS NULL;

CREATE UNIQUE INDEX IF NOT EXISTS idx_quests_quest_key ON quests (quest_key);
