-- T002: 扩展境界系统至9大境界（对齐GDD-02/GDD-08五层世界分层）
-- 更新realm模板数据，新增境界名称表

-- 1. 更新realms表数据为9大境界
DELETE FROM realms;

INSERT INTO realms (tier, minor_realm_max, name, world_tier, main_currency_code, is_tribulation_required, base_success_rate, attr_growth_template) VALUES
(1, 3, '炼气期', 1, 'copper', false, 1.0, '{"hp":10,"atk":2,"def":1}'),
(2, 3, '筑基期', 2, 'copper', true, 0.55, '{"hp":20,"atk":4,"def":2}'),
(3, 3, '金丹期', 2, 'spirit_stone_low', true, 0.50, '{"hp":35,"atk":7,"def":4}'),
(4, 3, '元婴期', 2, 'spirit_stone_mid', true, 0.45, '{"hp":55,"atk":11,"def":6}'),
(5, 3, '化神期', 3, 'soul_crystal', true, 0.40, '{"hp":80,"atk":16,"def":9}'),
(6, 3, '合体期', 3, 'soul_crystal', true, 0.35, '{"hp":110,"atk":22,"def":12}'),
(7, 3, '大乘期', 4, 'immortal_crystal', true, 0.30, '{"hp":150,"atk":30,"def":16}'),
(8, 3, '渡劫期', 4, 'immortal_crystal', true, 0.25, '{"hp":200,"atk":40,"def":22}'),
(9, 3, '飞升期', 5, 'chaos_crystal', true, 0.20, '{"hp":280,"atk":55,"def":30}')
ON CONFLICT (tier) DO UPDATE SET
    name = EXCLUDED.name,
    world_tier = EXCLUDED.world_tier,
    main_currency_code = EXCLUDED.main_currency_code,
    is_tribulation_required = EXCLUDED.is_tribulation_required,
    base_success_rate = EXCLUDED.base_success_rate,
    attr_growth_template = EXCLUDED.attr_growth_template;

-- 2. 创建种族境界名称表
CREATE TABLE IF NOT EXISTS realm_names (
    id              SERIAL PRIMARY KEY,
    realm_tier      SMALLINT NOT NULL,
    minor_realm     SMALLINT NOT NULL,
    race_id         VARCHAR(32) NOT NULL,
    name            VARCHAR(64) NOT NULL,
    description     TEXT,
    UNIQUE (realm_tier, minor_realm, race_id)
);

-- 3. 人族境界名称（修仙路线 - 唯一）
INSERT INTO realm_names (realm_tier, minor_realm, race_id, name, description) VALUES
(1, 1, 'human', '炼气初期', '感应天地灵气，开始修炼'),
(1, 2, 'human', '炼气中期', '灵气在体内循环，内力初成'),
(1, 3, 'human', '炼气圆满', '灵气充盈全身，准备筑基'),
(2, 1, 'human', '筑基初期', '道基初成，修炼根基稳固'),
(2, 2, 'human', '筑基中期', '道基渐固，可修炼更高功法'),
(2, 3, 'human', '筑基圆满', '道基稳固，可尝试结丹'),
(3, 1, 'human', '金丹初期', '凝结金丹，质变提升'),
(3, 2, 'human', '金丹中期', '金丹稳固，内力大增'),
(3, 3, 'human', '金丹圆满', '金丹圆满，可尝试元婴'),
(4, 1, 'human', '元婴初期', '孕育元婴，神识觉醒'),
(4, 2, 'human', '元婴中期', '元婴成长，神识增强'),
(4, 3, 'human', '元婴圆满', '元婴圆满，可尝试化神'),
(5, 1, 'human', '化神初期', '化神出窍，超凡入圣'),
(5, 2, 'human', '化神中期', '神识大增，掌控天地'),
(5, 3, 'human', '化神圆满', '化神圆满，可尝试合体'),
(6, 1, 'human', '合体初期', '与天地合一，掌控法则'),
(6, 2, 'human', '合体中期', '法则领悟加深'),
(6, 3, 'human', '合体圆满', '合体圆满，可尝试渡劫'),
(7, 1, 'human', '大乘初期', '大乘圆满，准备飞升'),
(7, 2, 'human', '大乘中期', '大乘境界稳固'),
(7, 3, 'human', '大乘圆满', '大乘圆满，等待天劫'),
(8, 1, 'human', '渡劫初期', '开始渡劫，接受天道考验'),
(8, 2, 'human', '渡劫中期', '渡劫进行中'),
(8, 3, 'human', '渡劫圆满', '渡劫成功，准备飞升'),
(9, 1, 'human', '飞升初期', '飞升仙界，成就大道'),
(9, 2, 'human', '飞升中期', '仙界修行'),
(9, 3, 'human', '飞升圆满', '大道圆满，超脱轮回')
ON CONFLICT (realm_tier, minor_realm, race_id) DO UPDATE SET
    name = EXCLUDED.name,
    description = EXCLUDED.description;

-- 4. 妖族血脉觉醒阶段名称
INSERT INTO realm_names (realm_tier, minor_realm, race_id, name, description) VALUES
(1, 1, 'tiger_yao', '血脉苏醒', '感应血脉力量，妖力初显'),
(1, 2, 'tiger_yao', '血脉觉醒', '觉醒血脉天赋，实力提升'),
(1, 3, 'tiger_yao', '血脉进化', '血脉进化，获得新能力'),
(2, 1, 'tiger_yao', '化形初期', '可短暂化为人形'),
(2, 2, 'tiger_yao', '化形中期', '化形时间延长'),
(2, 3, 'tiger_yao', '化形圆满', '可自由化形'),
(3, 1, 'tiger_yao', '本体觉醒初期', '觉醒本体形态，实力大增'),
(3, 2, 'tiger_yao', '本体觉醒中期', '本体形态稳固'),
(3, 3, 'tiger_yao', '本体觉醒圆满', '本体圆满，可尝试更高境界'),
(4, 1, 'tiger_yao', '妖丹初期', '凝结妖丹，质变提升'),
(4, 2, 'tiger_yao', '妖丹中期', '妖丹稳固，妖力大增'),
(4, 3, 'tiger_yao', '妖丹圆满', '妖丹圆满，可尝试化形'),
(5, 1, 'tiger_yao', '化形大成', '化形完全掌控'),
(5, 2, 'tiger_yao', '化形圆满', '化形圆满'),
(5, 3, 'tiger_yao', '化形极致', '化形极致，可尝试更高境界'),
(6, 1, 'tiger_yao', '妖王初期', '成为妖王，统领一方'),
(6, 2, 'tiger_yao', '妖王中期', '妖王实力稳固'),
(6, 3, 'tiger_yao', '妖王圆满', '妖王圆满，可尝试妖帝'),
(7, 1, 'tiger_yao', '妖帝初期', '成为妖帝，威震八方'),
(7, 2, 'tiger_yao', '妖帝中期', '妖帝实力大增'),
(7, 3, 'tiger_yao', '妖帝圆满', '妖帝圆满，可尝试渡劫'),
(8, 1, 'tiger_yao', '渡劫初期', '开始渡劫，接受天道考验'),
(8, 2, 'tiger_yao', '渡劫中期', '渡劫进行中'),
(8, 3, 'tiger_yao', '渡劫圆满', '渡劫成功，准备飞升'),
(9, 1, 'tiger_yao', '飞升初期', '飞升妖界，成就大道'),
(9, 2, 'tiger_yao', '飞升中期', '妖界修行'),
(9, 3, 'tiger_yao', '飞升圆满', '大道圆满，超脱轮回')
ON CONFLICT (realm_tier, minor_realm, race_id) DO UPDATE SET
    name = EXCLUDED.name,
    description = EXCLUDED.description;

-- 5. 神族法则领悟阶段名称
INSERT INTO realm_names (realm_tier, minor_realm, race_id, name, description) VALUES
(1, 1, 'divine', '法则感知', '感应天地法则，神力初显'),
(1, 2, 'divine', '法则领悟', '领悟特定法则，实力提升'),
(1, 3, 'divine', '法则掌控', '掌控法则之力'),
(2, 1, 'divine', '神力凝聚', '凝聚神力，质变提升'),
(2, 2, 'divine', '神力稳固', '神力稳固，可修炼更高法则'),
(2, 3, 'divine', '神力圆满', '神力圆满，可尝试神格'),
(3, 1, 'divine', '神格初期', '凝聚神格，成就神位'),
(3, 2, 'divine', '神格中期', '神格稳固，神力大增'),
(3, 3, 'divine', '神格圆满', '神格圆满，可尝试更高境界'),
(4, 1, 'divine', '天神初期', '成为天神，掌控一方'),
(4, 2, 'divine', '天神中期', '天神实力稳固'),
(4, 3, 'divine', '天神圆满', '天神圆满，可尝试神王'),
(5, 1, 'divine', '神王初期', '成为神王，统领神族'),
(5, 2, 'divine', '神王中期', '神王实力大增'),
(5, 3, 'divine', '神王圆满', '神王圆满，可尝试神帝'),
(6, 1, 'divine', '神帝初期', '成为神帝，威震神界'),
(6, 2, 'divine', '神帝中期', '神帝实力大增'),
(6, 3, 'divine', '神帝圆满', '神帝圆满，可尝试渡劫'),
(7, 1, 'divine', '大乘初期', '大乘圆满，准备飞升'),
(7, 2, 'divine', '大乘中期', '大乘境界稳固'),
(7, 3, 'divine', '大乘圆满', '大乘圆满，等待天劫'),
(8, 1, 'divine', '渡劫初期', '开始渡劫，接受天道考验'),
(8, 2, 'divine', '渡劫中期', '渡劫进行中'),
(8, 3, 'divine', '渡劫圆满', '渡劫成功，准备飞升'),
(9, 1, 'divine', '飞升初期', '飞升神界，成就大道'),
(9, 2, 'divine', '飞升中期', '神界修行'),
(9, 3, 'divine', '飞升圆满', '大道圆满，超脱轮回')
ON CONFLICT (realm_tier, minor_realm, race_id) DO UPDATE SET
    name = EXCLUDED.name,
    description = EXCLUDED.description;

-- 6. 巫族血肉淬炼阶段名称
INSERT INTO realm_names (realm_tier, minor_realm, race_id, name, description) VALUES
(1, 1, 'witch', '淬体初期', '淬炼肉身，提升体质'),
(1, 2, 'witch', '淬体中期', '肉身强化，力量增长'),
(1, 3, 'witch', '淬体圆满', '淬体圆满，可尝试炼血'),
(2, 1, 'witch', '炼血初期', '炼化血脉，提升血脉强度'),
(2, 2, 'witch', '炼血中期', '血脉强化，巫力增长'),
(2, 3, 'witch', '炼血圆满', '炼血圆满，可尝试祖巫觉醒'),
(3, 1, 'witch', '祖巫觉醒初期', '觉醒祖巫真身，实力大增'),
(3, 2, 'witch', '祖巫觉醒中期', '祖巫真身稳固'),
(3, 3, 'witch', '祖巫觉醒圆满', '祖巫圆满，可尝试肉身成圣'),
(4, 1, 'witch', '肉身成圣初期', '肉身成圣，超凡入圣'),
(4, 2, 'witch', '肉身成圣中期', '肉身成圣稳固'),
(4, 3, 'witch', '肉身成圣圆满', '肉身成圣圆满，可尝试更高境界'),
(5, 1, 'witch', '祖巫大成', '祖巫真身大成，实力大增'),
(5, 2, 'witch', '祖巫圆满', '祖巫圆满'),
(5, 3, 'witch', '祖巫极致', '祖巫极致，可尝试更高境界'),
(6, 1, 'witch', '巫皇初期', '成为巫皇，统领巫族'),
(6, 2, 'witch', '巫皇中期', '巫皇实力稳固'),
(6, 3, 'witch', '巫皇圆满', '巫皇圆满，可尝试巫帝'),
(7, 1, 'witch', '巫帝初期', '成为巫帝，威震巫界'),
(7, 2, 'witch', '巫帝中期', '巫帝实力大增'),
(7, 3, 'witch', '巫帝圆满', '巫帝圆满，可尝试渡劫'),
(8, 1, 'witch', '渡劫初期', '开始渡劫，接受天道考验'),
(8, 2, 'witch', '渡劫中期', '渡劫进行中'),
(8, 3, 'witch', '渡劫圆满', '渡劫成功，准备飞升'),
(9, 1, 'witch', '飞升初期', '飞升巫界，成就大道'),
(9, 2, 'witch', '飞升中期', '巫界修行'),
(9, 3, 'witch', '飞升圆满', '大道圆满，超脱轮回')
ON CONFLICT (realm_tier, minor_realm, race_id) DO UPDATE SET
    name = EXCLUDED.name,
    description = EXCLUDED.description;

-- 7. 深潜裔旧日铭印阶段名称
INSERT INTO realm_names (realm_tier, minor_realm, race_id, name, description) VALUES
(1, 1, 'deep_spawn', '旧日低语', '听到旧日低语，SAN值开始波动'),
(1, 2, 'deep_spawn', '旧日铭印', '接受旧神铭印，获得邪能'),
(1, 3, 'deep_spawn', '疯狂觉醒', '疯狂觉醒，实力提升但SAN下降'),
(2, 1, 'deep_spawn', '邪能凝聚', '凝聚邪能，质变提升'),
(2, 2, 'deep_spawn', '邪能稳固', '邪能稳固，可修炼更高邪术'),
(2, 3, 'deep_spawn', '邪能圆满', '邪能圆满，可尝试旧神眷顾'),
(3, 1, 'deep_spawn', '旧神眷顾初期', '获得旧神眷顾，实力大增'),
(3, 2, 'deep_spawn', '旧神眷顾中期', '旧神眷顾稳固'),
(3, 3, 'deep_spawn', '旧神眷顾圆满', '旧神眷顾圆满，可尝试更高境界'),
(4, 1, 'deep_spawn', '域外邪能初期', '掌控域外邪能，超凡入圣'),
(4, 2, 'deep_spawn', '域外邪能中期', '域外邪能稳固'),
(4, 3, 'deep_spawn', '域外邪能圆满', '域外邪能圆满，可尝试更高境界'),
(5, 1, 'deep_spawn', '旧神化身初期', '化为旧神化身，实力大增'),
(5, 2, 'deep_spawn', '旧神化身中期', '旧神化身稳固'),
(5, 3, 'deep_spawn', '旧神化身圆满', '旧神化身圆满，可尝试更高境界'),
(6, 1, 'deep_spawn', '深渊领主初期', '成为深渊领主，统领一方'),
(6, 2, 'deep_spawn', '深渊领主中期', '深渊领主实力稳固'),
(6, 3, 'deep_spawn', '深渊领主圆满', '深渊领主圆满，可尝试更高境界'),
(7, 1, 'deep_spawn', '旧神降临初期', '旧神降临，实力大增'),
(7, 2, 'deep_spawn', '旧神降临中期', '旧神降临稳固'),
(7, 3, 'deep_spawn', '旧神降临圆满', '旧神降临圆满，可尝试渡劫'),
(8, 1, 'deep_spawn', '混沌渡劫初期', '开始混沌渡劫，接受旧神考验'),
(8, 2, 'deep_spawn', '混沌渡劫中期', '混沌渡劫进行中'),
(8, 3, 'deep_spawn', '混沌渡劫圆满', '混沌渡劫成功，准备飞升'),
(9, 1, 'deep_spawn', '旧日飞升初期', '飞升旧日界，成就大道'),
(9, 2, 'deep_spawn', '旧日飞升中期', '旧日界修行'),
(9, 3, 'deep_spawn', '旧日飞升圆满', '大道圆满，超脱轮回')
ON CONFLICT (realm_tier, minor_realm, race_id) DO UPDATE SET
    name = EXCLUDED.name,
    description = EXCLUDED.description;

-- 8. 更新world_layers为5层世界
DELETE FROM world_layers;

INSERT INTO world_layers (tier, name, display_layer, realm_tier, is_pvp_enabled, cross_tier_rules) VALUES
(1, '凡界', 0, 1, false, '{"description":"种族完全隔离，炼气期"}'),
(2, '灵界', 1, 2, true, '{"description":"首次天启，跨种族相遇，筑基/金丹/元婴"}'),
(3, '道界', 2, 5, true, '{"description":"法则领悟，大道融合，化神/合体/大乘"}'),
(4, '圣界', 3, 8, true, '{"description":"顶级争夺，界域争霸，渡劫/飞升"}'),
(5, '神域', 4, 9, true, '{"description":"界域系统，域主竞争"}')
ON CONFLICT (tier) DO UPDATE SET
    name = EXCLUDED.name,
    display_layer = EXCLUDED.display_layer,
    realm_tier = EXCLUDED.realm_tier,
    is_pvp_enabled = EXCLUDED.is_pvp_enabled,
    cross_tier_rules = EXCLUDED.cross_tier_rules;
