INSERT OR IGNORE INTO locations (id, slug, name, description) VALUES
  (1, 'ember-wastes', 'The Ember Wastes', 'A scorched frontier surrounding a buried citadel.'),
  (2, 'crown-caldera', 'The Crown Caldera', 'A volcanic basin surrounding the stronghold of the Ember Crown.');

INSERT OR IGNORE INTO dungeons
  (id, location_id, slug, name, recommended_level, content_type, party_size, completion_item_level, experience_reward, description)
VALUES
  (1, 1, 'ashen-halls', 'The Ashen Halls', 1, 'dungeon', 6, NULL, 125, 'Break the cinder cult before the old furnace awakens.'),
  (2, 2, 'citadel-of-the-ember-crown', 'Citadel of the Ember Crown', 1, 'raid', 18, 10, 175, 'Lead eighteen allies through the caldera and break the Ember Crown across three phases.');

UPDATE dungeons
SET slug = 'bulldrome-hunting-ground',
    name = 'Bulldrome Hunting Ground',
    location_id = 1,
    recommended_level = 1,
    content_type = 'dungeon',
    party_size = 6,
    completion_item_level = NULL,
    experience_reward = 125,
    description = 'A three-boss hunt featuring Bulldrome, Yian Kut-Ku, and Rathian.'
WHERE id = 1;
UPDATE dungeons SET party_size = 18, completion_item_level = NULL, experience_reward = 175
WHERE slug = 'citadel-of-the-ember-crown';

INSERT OR IGNORE INTO difficulties
  (id, slug, name, dropped_item_level, unlock_level, health_multiplier, damage_multiplier, experience_multiplier, description)
VALUES
  (1, 'initiate', 'Initiate', 1, 1, 0.8, 0.8, 1.0, 'Entry-level dungeon difficulty for crafting the first real set.'),
  (2, 'veteran', 'Veteran', 10, 10, 1.45, 1.25, 2.0, 'A major step up that rewards refined gear components.'),
  (3, 'champion', 'Champion', 15, 15, 1.7, 1.45, 2.2, 'Gear-only upgrade tier between Veteran and Mythic.'),
  (4, 'mythic', 'Mythic', 20, 20, 2.25, 1.85, 3.5, 'Endgame dungeon difficulty.'),
  (5, 'ascendant', 'Ascendant', 25, 25, 2.8, 2.25, 4.5, 'The current pinnacle difficulty.'),
  (101, 'raid-normal', 'Normal', 7, 1, 1.0, 1.0, 1.25, 'The opening raid difficulty, tuned for an eighteen-player party.');

UPDATE difficulties SET
  dropped_item_level = CASE slug
    WHEN 'initiate' THEN 1 WHEN 'raid-normal' THEN 10 ELSE dropped_item_level END,
  unlock_level = CASE slug
    WHEN 'initiate' THEN 1 WHEN 'veteran' THEN 10 WHEN 'champion' THEN 15
    WHEN 'mythic' THEN 20 WHEN 'ascendant' THEN 25 ELSE unlock_level END,
  health_multiplier = CASE slug
    WHEN 'initiate' THEN 0.8 WHEN 'veteran' THEN 1.45 WHEN 'champion' THEN 1.7
    WHEN 'mythic' THEN 2.25 WHEN 'ascendant' THEN 2.8 ELSE health_multiplier END,
  damage_multiplier = CASE slug
    WHEN 'initiate' THEN 0.8 WHEN 'veteran' THEN 1.25 WHEN 'champion' THEN 1.45
    WHEN 'mythic' THEN 1.85 WHEN 'ascendant' THEN 2.25 ELSE damage_multiplier END,
  experience_multiplier = CASE slug
    WHEN 'initiate' THEN 1.0 WHEN 'veteran' THEN 2.0 WHEN 'champion' THEN 2.2
    WHEN 'mythic' THEN 3.5 WHEN 'ascendant' THEN 4.5 ELSE experience_multiplier END,
  description = CASE slug
    WHEN 'initiate' THEN 'Entry-level dungeon difficulty for crafting the first real set.'
    WHEN 'veteran' THEN 'A major step up that rewards refined gear components.'
    WHEN 'champion' THEN 'Gear-only upgrade tier between Veteran and Mythic.'
    WHEN 'mythic' THEN 'Endgame dungeon difficulty.'
    WHEN 'ascendant' THEN 'The current pinnacle difficulty.'
    ELSE description END;

INSERT OR IGNORE INTO dungeon_difficulties (dungeon_id, difficulty_id) VALUES
  (1, 1),
  (1, 2),
  (1, 3),
  (1, 4),
  (1, 5),
  (2, 101);

INSERT OR IGNORE INTO encounters
  (id, dungeon_id, sequence, slug, name, encounter_type, max_health, base_damage, tank_damage, party_damage, description)
VALUES
  (1, 1, 1, 'ashfang-pack', 'Ashfang Pack', 'trash', 390, 13, 7, 24, 'Three beasts snap at random party members.'),
  (2, 1, 2, 'cinder-adepts', 'Cinder Adepts', 'trash', 470, 16, 10, 25, 'Cultists pressure the tank and throw embers into the group.'),
  (3, 1, 3, 'bulldrome', 'Bulldrome', 'boss', 820, 18, 13, 27, 'A charging wyvern tests the party with heavy tusk strikes.'),
  (10, 1, 4, 'sootborn-guard', 'Sootborn Guard', 'trash', 420, 14, 8, 24, 'A hulking guard forged from living soot.'),
  (11, 1, 5, 'ember-channelers', 'Ember Channelers', 'trash', 500, 17, 11, 25, 'Cultists channelling raw ember energy.'),
  (12, 1, 6, 'yian-kut-ku', 'Yian Kut-Ku', 'boss', 880, 20, 14, 28, 'A frantic bird wyvern scatters flame and wingbeats through the party.'),
  (20, 1, 7, 'cinder-colossus', 'Cinder Colossus', 'trash', 460, 15, 9, 25, 'An enormous construct of slag and anger.'),
  (21, 1, 8, 'flame-wardens', 'Flame Wardens', 'trash', 530, 18, 12, 26, 'Elite guards of the furnace sanctum.'),
  (22, 1, 9, 'rathian', 'Rathian', 'boss', 950, 22, 16, 30, 'The queen of the land punishes mistakes with poison and crushing sweeps.'),
  (100, 2, 1, 'scorched-vanguard', 'Scorched Vanguard', 'trash', 1050, 17, 10, 48, 'The citadel vanguard splits its assault across the raid.'),
  (101, 2, 2, 'pyrebinders', 'Pyrebinders', 'trash', 1220, 19, 12, 50, 'Battle-mages bind living flame to random allies.'),
  (102, 2, 3, 'gatekeeper-arkon', 'Gatekeeper Arkon', 'boss', 2150, 22, 15, 53, 'Arkon seals the caldera gate behind waves of burning force.'),
  (103, 2, 4, 'molten-behemoths', 'Molten Behemoths', 'trash', 1160, 18, 11, 49, 'Two molten beasts batter both tanks and trample the raid.'),
  (104, 2, 5, 'crown-zealots', 'Crown Zealots', 'trash', 1320, 20, 13, 51, 'Fanatics ignite themselves to empower their allies.'),
  (105, 2, 6, 'high-inquisitor-vael', 'High Inquisitor Vael', 'boss', 2320, 24, 16, 55, 'Vael brands the unworthy and calls down columns of fire.'),
  (106, 2, 7, 'emberwing-brood', 'Emberwing Brood', 'trash', 1260, 19, 12, 51, 'A flock of emberwings dives through the raid in sequence.'),
  (107, 2, 8, 'crownforged-sentinels', 'Crownforged Sentinels', 'trash', 1420, 21, 14, 53, 'Ancient sentinels awaken to defend the crown chamber.'),
  (108, 2, 9, 'the-ember-crown', 'The Ember Crown', 'boss', 2550, 26, 18, 57, 'The living crown burns above its throne, demanding perfect coordination.');

UPDATE encounters
SET slug = 'bulldrome',
    name = 'Bulldrome',
    description = 'A charging wyvern tests the party with heavy tusk strikes.',
    image_url = COALESCE(NULLIF(image_url, ''), '/boss-placeholder.svg')
WHERE id = 3;

UPDATE encounters
SET slug = 'yian-kut-ku',
    name = 'Yian Kut-Ku',
    description = 'A frantic bird wyvern scatters flame and wingbeats through the party.',
    image_url = COALESCE(NULLIF(image_url, ''), '/boss-placeholder.svg')
WHERE id = 12;

UPDATE encounters
SET slug = 'rathian',
    name = 'Rathian',
    description = 'The queen of the land punishes mistakes with poison and crushing sweeps.',
    image_url = COALESCE(NULLIF(image_url, ''), '/boss-placeholder.svg')
WHERE id = 22;

WITH mechanics_seed(id, encounter_id, name, mechanic_type, interval_seconds, power, description) AS (
  VALUES
  (1, 3, 'Cinder Pulse', 'party_damage', 4.9, 12, 'Deals damage to the full party.'),
  (2, 3, 'Searing Mark', 'dispellable_dot', 7.7, 7, 'Marks one target with recurring damage until cleansed.'),
  (10, 12, 'Ember Wave', 'party_damage', 5.6, 14, 'A wave of ember energy hits the entire party.'),
  (11, 12, 'Brand of Sacrifice', 'dispellable_dot', 8.4, 9, 'Brands a target with burning sigils.'),
  (20, 22, 'Furnace Blast', 'party_damage', 6.3, 16, 'The furnace vents superheated air across the party.'),
  (21, 22, 'Melting Touch', 'dispellable_dot', 9.1, 11, 'A target begins to melt from intense heat.'),
  (100, 102, 'Caldera Shockwave', 'party_damage', 5.6, 14, 'A shockwave rolls across all eighteen raiders.'),
  (101, 102, 'Gatebrand', 'dispellable_dot', 8.2, 9, 'Brands one raider with a seal of living fire.'),
  (102, 105, 'Pillar of Judgment', 'party_damage', 5.2, 16, 'Columns of flame erupt beneath the raid.'),
  (103, 105, 'Inquisitor''s Brand', 'dispellable_dot', 7.8, 10, 'A burning brand persists until cleansed.'),
  (104, 108, 'Crownflare', 'party_damage', 4.9, 18, 'The Ember Crown releases a raid-wide flare.'),
  (105, 108, 'Royal Decree', 'dispellable_dot', 7.1, 12, 'A lethal decree marks one raider for cleansing.')
)
INSERT OR IGNORE INTO mechanics
  (id, encounter_id, name, mechanic_type, interval_seconds, power, description)
SELECT id, encounter_id, name, mechanic_type, interval_seconds, power, description
FROM mechanics_seed
WHERE EXISTS (
  SELECT 1
  FROM encounters
  WHERE encounters.id = mechanics_seed.encounter_id
);

INSERT OR IGNORE INTO classes
  (id, slug, name, resource_name, max_resource, theme_color, description)
VALUES
  (1, 'dawnweaver', 'Dawnweaver', 'Mana', 100, '#e5b95f', 'A reactive healer using radiant restoration and protective wards.'),
  (2, 'lifebinder', 'Lifebinder', 'Bloom', 100, '#4fb978', 'A patient healer who grows powerful restorative effects over time.'),
  (3, 'runesage', 'Runesage', 'Focus', 100, '#708bd6', 'A tactical healer who prepares runes, barriers, and delayed recovery.');

INSERT OR IGNORE INTO spells
  (id, class_id, slug, name, spell_type, resource_cost, cooldown_seconds, power, unlock_level, glyph, description)
VALUES
  (1, 1, 'mend', 'Mend', 'direct_heal', 5, 0.5, 30, 1, '+', 'A fast, efficient single-target heal.'),
  (2, 1, 'renew', 'Renew', 'heal_over_time', 7, 0.5, 12, 1, '~', 'Heals now and continues healing over time.'),
  (3, 1, 'radiance', 'Radiance', 'party_heal', 12, 8, 18, 1, '*', 'Restores health to up to 4 injured party members.'),
  (4, 1, 'sun-ward', 'Sun Ward', 'absorb', 8, 7, 36, 1, 'O', 'Places a damage-absorbing shield on your target.'),
  (5, 1, 'purify', 'Purify', 'cleanse', 5, 5, 10, 1, 'x', 'Removes a harmful effect and restores health.'),
  (6, 1, 'dawn-burst', 'Dawn Burst', 'party_heal', 16, 12, 28, 5, 'D', 'A brilliant wave of healing for up to 4 injured allies.'),
  (7, 1, 'guardian-light', 'Guardian Light', 'absorb', 13, 14, 55, 10, 'G', 'A powerful ward reserved for moments of extreme danger.'),
  (8, 1, 'second-sun', 'Second Sun', 'direct_heal', 20, 20, 85, 15, 'S', 'Calls down a delayed surge of restorative light.'),
  (9, 1, 'daybreak', 'Daybreak', 'party_heal', 23, 30, 48, 20, 'A', 'Floods up to 4 injured allies with the full strength of dawn.'),
  (20, 2, 'verdant-touch', 'Verdant Touch', 'direct_heal', 5, 0.5, 28, 1, '+', 'A quick pulse of living energy.'),
  (21, 2, 'seed-of-life', 'Seed of Life', 'heal_over_time', 7, 0.5, 11, 1, 's', 'Plants a restorative seed that blooms over time.'),
  (22, 2, 'wild-bloom', 'Wild Bloom', 'party_heal', 12, 8, 17, 1, '*', 'Restorative growth spreads to up to 4 injured allies.'),
  (23, 2, 'barkskin', 'Barkskin', 'absorb', 8, 7, 34, 1, 'B', 'Wraps an ally in protective living bark.'),
  (24, 2, 'purging-sap', 'Purging Sap', 'cleanse', 5, 5, 10, 1, 'p', 'Draws a harmful effect out through enchanted sap.'),
  (25, 2, 'ancient-grove', 'Ancient Grove', 'party_heal', 17, 12, 31, 5, 'T', 'Briefly summons the shelter of an ancient grove.'),
  (30, 3, 'etched-mend', 'Etched Mend', 'direct_heal', 5, 0.5, 29, 1, '+', 'Completes a simple rune of restoration.'),
  (31, 3, 'echo-rune', 'Echo Rune', 'heal_over_time', 7, 0.5, 12, 1, 'e', 'Repeats a restorative rune over several moments.'),
  (32, 3, 'concordance', 'Concordance', 'party_heal', 12, 8, 18, 1, '*', 'Links up to 4 injured allies through a shared healing pattern.'),
  (33, 3, 'aegis-script', 'Aegis Script', 'absorb', 8, 7, 35, 1, 'O', 'Writes a temporary barrier around an ally.'),
  (34, 3, 'unravel', 'Unravel', 'cleanse', 5, 5, 10, 1, 'u', 'Unravels a hostile magical pattern.'),
  (35, 3, 'grand-design', 'Grand Design', 'party_heal', 16, 12, 30, 5, 'R', 'Activates a prepared network of restorative runes.');

UPDATE spells SET resource_cost = CASE slug
  WHEN 'mend' THEN 5
  WHEN 'renew' THEN 7
  WHEN 'radiance' THEN 12
  WHEN 'sun-ward' THEN 8
  WHEN 'purify' THEN 5
  WHEN 'dawn-burst' THEN 16
  WHEN 'guardian-light' THEN 13
  WHEN 'second-sun' THEN 20
  WHEN 'daybreak' THEN 23
  WHEN 'verdant-touch' THEN 5
  WHEN 'seed-of-life' THEN 7
  WHEN 'wild-bloom' THEN 12
  WHEN 'barkskin' THEN 8
  WHEN 'purging-sap' THEN 5
  WHEN 'ancient-grove' THEN 17
  WHEN 'etched-mend' THEN 5
  WHEN 'echo-rune' THEN 7
  WHEN 'concordance' THEN 12
  WHEN 'aegis-script' THEN 8
  WHEN 'unravel' THEN 5
  WHEN 'grand-design' THEN 16
  ELSE resource_cost
END;

UPDATE spells SET unlock_level = 1, glyph = '+' WHERE slug = 'mend';
UPDATE spells SET unlock_level = 1, glyph = '~' WHERE slug = 'renew';
UPDATE spells SET unlock_level = 1, glyph = '*' WHERE slug = 'radiance';
UPDATE spells SET unlock_level = 1, glyph = 'O' WHERE slug = 'sun-ward';
UPDATE spells SET unlock_level = 1, glyph = 'x' WHERE slug = 'purify';
UPDATE spells SET cooldown_seconds = 0.5
WHERE slug IN ('mend', 'renew', 'verdant-touch', 'seed-of-life', 'etched-mend', 'echo-rune');
UPDATE spells SET unlock_level = 5 WHERE slug IN ('dawn-burst', 'ancient-grove', 'grand-design');
UPDATE spells SET unlock_level = 10 WHERE slug = 'guardian-light';
UPDATE spells SET unlock_level = 15 WHERE slug = 'second-sun';
UPDATE spells SET unlock_level = 20 WHERE slug = 'daybreak';

UPDATE spells SET
  name = 'Verdant Touch',
  spell_type = 'direct_hot',
  resource_cost = 5,
  cooldown_seconds = 0.5,
  power = 20,
  glyph = '+',
  description = 'A weaker direct heal that also plants a stacking heal over time.'
WHERE slug = 'verdant-touch';

UPDATE spells SET
  name = 'Wild Growth',
  spell_type = 'party_hot',
  resource_cost = 12,
  cooldown_seconds = 8,
  power = 14,
  glyph = '*',
  description = 'Applies a stacking heal over time to up to 4 injured allies.'
WHERE slug = 'wild-bloom';

UPDATE spells SET
  name = 'Barkskin',
  spell_type = 'damage_reduction',
  resource_cost = 10,
  cooldown_seconds = 14,
  power = 0,
  glyph = 'B',
  description = 'Reduces the target ally''s damage taken by 50% for 8 seconds.'
WHERE slug = 'barkskin';

UPDATE spells SET
  name = 'Ancient Grove',
  spell_type = 'party_hot',
  resource_cost = 17,
  cooldown_seconds = 12,
  power = 24,
  glyph = 'T',
  description = 'Applies a stronger stacking heal over time to up to 4 injured allies.'
WHERE slug = 'ancient-grove';

UPDATE spells SET
  name = 'Mending Rune',
  spell_type = 'bounce_heal',
  resource_cost = 7,
  cooldown_seconds = 0.5,
  power = 18,
  glyph = 'e',
  description = 'Places a rune that heals when the ally takes damage, then jumps 4 times.'
WHERE slug = 'echo-rune';

UPDATE spells SET
  name = 'Concordance',
  spell_type = 'party_absorb',
  resource_cost = 12,
  cooldown_seconds = 8,
  power = 28,
  glyph = '*',
  description = 'Shields up to 4 injured allies through a shared barrier pattern.'
WHERE slug = 'concordance';

UPDATE spells SET
  name = 'Grand Design',
  spell_type = 'party_absorb',
  resource_cost = 16,
  cooldown_seconds = 12,
  power = 42,
  glyph = 'R',
  description = 'Raises a stronger shared barrier around up to 4 injured allies.'
WHERE slug = 'grand-design';

INSERT OR IGNORE INTO items
  (id, slug, name, slot, rarity, item_level, healing_power, max_resource_bonus, glyph, description)
VALUES
  (1, 'emberglass-sigil', 'Emberglass Sigil', 'ring', 'rare', 5, 4, 5, 'o', 'Warm light moves inside the cracked glass.'),
  (2, 'wardens-cinderwrap', 'Warden''s Cinderwrap', 'chest', 'uncommon', 5, 3, 0, 'C', 'Robes threaded with fire-resistant fibers.'),
  (3, 'ashwood-crook', 'Ashwood Crook', 'weapon', 'uncommon', 5, 5, 0, '/', 'A healer''s staff carved from fire-hardened wood.'),
  (4, 'cinderstep-boots', 'Cinderstep Boots', 'boots', 'uncommon', 5, 3, 0, 'b', 'Light boots made for crossing burning ground.'),
  (5, 'adepts-hood', 'Adept''s Hood', 'helmet', 'uncommon', 5, 3, 4, '^', 'The inner cloth is covered in careful annotations.'),
  (6, 'furnace-tenders-wraps', 'Furnace Tender''s Wraps', 'gloves', 'uncommon', 5, 3, 2, 'g', 'Heat-scored gloves that still hum with careful magic.'),
  (7, 'warden-ember', 'Warden''s Ember', 'trinket', 'rare', 5, 4, 4, '*', 'A coal from the old furnace that refuses to cool.'),
  (201, 'tempered-emberglass-sigil', 'Tempered Emberglass Sigil', 'ring', 'rare', 10, 7, 9, 'o', 'The glass has been tempered by Veteran flame.'),
  (202, 'tempered-cinderwrap', 'Tempered Cinderwrap', 'chest', 'uncommon', 10, 7, 2, 'C', 'Reinforced robes woven for Veteran expeditions.'),
  (203, 'tempered-ashwood-crook', 'Tempered Ashwood Crook', 'weapon', 'uncommon', 10, 10, 2, '/', 'A fire-hardened staff balanced for Veteran healers.'),
  (204, 'tempered-cinderstep-boots', 'Tempered Cinderstep Boots', 'boots', 'uncommon', 10, 6, 5, 'b', 'Sturdy boots for crossing hotter ground.'),
  (205, 'tempered-adepts-hood', 'Tempered Adept''s Hood', 'helmet', 'uncommon', 10, 6, 6, '^', 'Its annotations glow when danger approaches.'),
  (206, 'tempered-furnace-wraps', 'Tempered Furnace Wraps', 'gloves', 'uncommon', 10, 7, 4, 'g', 'Veteran wraps stitched with ember-resistant thread.'),
  (207, 'tempered-warden-ember', 'Tempered Warden''s Ember', 'trinket', 'rare', 10, 8, 7, '*', 'A stable ember pulsing with restorative heat.'),
  (301, 'runed-emberglass-sigil', 'Runed Emberglass Sigil', 'ring', 'rare', 15, 10, 13, 'o', 'Champion runes swim beneath the emberglass.'),
  (302, 'runed-cinderwrap', 'Runed Cinderwrap', 'chest', 'rare', 15, 11, 3, 'C', 'Champion robes covered in protective script.'),
  (303, 'runed-ashwood-crook', 'Runed Ashwood Crook', 'weapon', 'rare', 15, 15, 3, '/', 'A Champion focus carved with radiant channels.'),
  (304, 'runed-cinderstep-boots', 'Runed Cinderstep Boots', 'boots', 'rare', 15, 9, 8, 'b', 'Each step leaves a fading restorative rune.'),
  (305, 'runed-adepts-hood', 'Runed Adept''s Hood', 'helmet', 'rare', 15, 9, 9, '^', 'A Champion hood that sharpens restorative focus.'),
  (306, 'runed-furnace-wraps', 'Runed Furnace Wraps', 'gloves', 'rare', 15, 11, 6, 'g', 'Runes brighten as healing magic passes through them.'),
  (307, 'runed-warden-ember', 'Runed Warden''s Ember', 'trinket', 'rare', 15, 12, 10, '*', 'A Champion ember bound by concentric runes.'),
  (401, 'mythic-emberglass-sigil', 'Mythic Emberglass Sigil', 'ring', 'epic', 20, 14, 17, 'o', 'A flawless sigil carrying the furnace''s true name.'),
  (402, 'mythic-cinderwrap', 'Mythic Cinderwrap', 'chest', 'epic', 20, 15, 4, 'C', 'Mythic vestments untouched by ordinary flame.'),
  (403, 'mythic-ashwood-crook', 'Mythic Ashwood Crook', 'weapon', 'epic', 20, 20, 4, '/', 'A Mythic focus cut from the oldest ashwood.'),
  (404, 'mythic-cinderstep-boots', 'Mythic Cinderstep Boots', 'boots', 'epic', 20, 12, 11, 'b', 'The wearer walks safely across living fire.'),
  (405, 'mythic-adepts-hood', 'Mythic Adept''s Hood', 'helmet', 'epic', 20, 12, 12, '^', 'A Mythic hood filled with whispered formulae.'),
  (406, 'mythic-furnace-wraps', 'Mythic Furnace Wraps', 'gloves', 'epic', 20, 15, 8, 'g', 'Mythic gloves that shape healing into precise patterns.'),
  (407, 'mythic-warden-ember', 'Mythic Warden''s Ember', 'trinket', 'epic', 20, 16, 13, '*', 'The furnace heart answers this ember.'),
  (501, 'ascendant-emberglass-sigil', 'Ascendant Emberglass Sigil', 'ring', 'epic', 25, 18, 21, 'o', 'An Ascendant sigil radiant with contained dawn.'),
  (502, 'ascendant-cinderwrap', 'Ascendant Cinderwrap', 'chest', 'epic', 25, 19, 5, 'C', 'Ascendant robes woven from fire and light.'),
  (503, 'ascendant-ashwood-crook', 'Ascendant Ashwood Crook', 'weapon', 'epic', 25, 25, 5, '/', 'An Ascendant staff bearing an unending ember.'),
  (504, 'ascendant-cinderstep-boots', 'Ascendant Cinderstep Boots', 'boots', 'epic', 25, 15, 14, 'b', 'The ground cools before each Ascendant step.'),
  (505, 'ascendant-adepts-hood', 'Ascendant Adept''s Hood', 'helmet', 'epic', 25, 15, 15, '^', 'Every healing pattern appears obvious beneath this hood.'),
  (506, 'ascendant-furnace-wraps', 'Ascendant Furnace Wraps', 'gloves', 'epic', 25, 19, 10, 'g', 'Ascendant gloves threaded with concentrated daylight.'),
  (507, 'ascendant-warden-ember', 'Ascendant Warden''s Ember', 'trinket', 'epic', 25, 20, 16, '*', 'A perfect ember from the heart of the Ashen Halls.'),
  (701, 'caldera-signet', 'Caldera Signet', 'ring', 'rare', 7, 5, 6, 'o', 'A raid-forged signet warm with caldera light.'),
  (702, 'vanguard-mantle', 'Vanguard Mantle', 'chest', 'rare', 7, 5, 1, 'C', 'A reinforced mantle taken from the citadel vanguard.'),
  (703, 'pyrebinder-crook', 'Pyrebinder Crook', 'weapon', 'rare', 7, 7, 1, '/', 'A focus used to bend living flame toward restoration.'),
  (704, 'emberstep-treads', 'Emberstep Treads', 'boots', 'rare', 7, 4, 5, 'b', 'Raid treads made for crossing unstable volcanic stone.'),
  (705, 'gatekeeper-cowl', 'Gatekeeper Cowl', 'helmet', 'rare', 7, 4, 6, '^', 'A cowl inscribed with the wards of the outer gate.'),
  (706, 'crownward-wraps', 'Crownward Wraps', 'gloves', 'rare', 7, 5, 3, 'g', 'Precise wraps worn by the healers of the Ember Crown.'),
  (707, 'living-coal-charm', 'Living Coal Charm', 'trinket', 'rare', 7, 6, 5, '*', 'A coal that pulses in time with nearby heartbeats.'),
  (710, 'royal-caldera-signet', 'Royal Caldera Signet', 'ring', 'epic', 10, 8, 9, 'o', 'A royal signet claimed after breaking the Ember Crown.'),
  (711, 'ember-crown-vestment', 'Ember Crown Vestment', 'chest', 'epic', 10, 8, 2, 'C', 'The ceremonial vestment of the fallen crown court.'),
  (712, 'crownshard-crook', 'Crownshard Crook', 'weapon', 'epic', 10, 11, 2, '/', 'A healing focus set with a cooled shard of the crown.'),
  (713, 'caldera-walkers', 'Caldera Walkers', 'boots', 'epic', 10, 7, 8, 'b', 'Boots that remain cool even at the caldera''s heart.'),
  (714, 'inquisitors-cowl', 'Inquisitor''s Cowl', 'helmet', 'epic', 10, 7, 9, '^', 'The cowl of an inquisitor, stripped of its cruel sigils.'),
  (715, 'royal-flame-wraps', 'Royal Flame Wraps', 'gloves', 'epic', 10, 8, 6, 'g', 'Royal wraps that shape flame into restorative patterns.'),
  (716, 'extinguished-crown', 'Extinguished Crown', 'trinket', 'epic', 10, 9, 8, '*', 'A harmless fragment of the once-living Ember Crown.'),
  (100, 'novice-crook', 'Novice Crook', 'weapon', 'common', 1, 1, 0, '/', 'A simple focus carried by every new healer.'),
  (101, 'novice-cowl', 'Novice Cowl', 'helmet', 'common', 1, 0, 1, '^', 'Plain cloth that helps steady a beginner''s thoughts.'),
  (102, 'novice-vestment', 'Novice Vestment', 'chest', 'common', 1, 1, 0, 'C', 'Unadorned robes issued to apprentice healers.'),
  (103, 'novice-wraps', 'Novice Wraps', 'gloves', 'common', 1, 1, 0, 'g', 'Soft wraps that leave the fingers free for spellwork.'),
  (104, 'novice-slippers', 'Novice Slippers', 'boots', 'common', 1, 0, 1, 'b', 'Comfortable enough for a first expedition.'),
  (105, 'novice-band', 'Novice Band', 'ring', 'common', 1, 0, 1, 'o', 'A copper band etched with a minor restorative rune.'),
  (106, 'novice-token', 'Novice Token', 'trinket', 'common', 1, 1, 0, '*', 'A small charm commemorating the healer''s oath.'),
  (107, 'novice-wand', 'Novice Wand', 'weapon', 'common', 1, 0, 2, '!', 'A spare focus that favors a deeper resource pool.'),
  (600, 'minor-component', 'Minor Component', 'component', 'common', 1, 0, 0, '◆', 'A basic crafting component.'),
  (601, 'basic-component', 'Basic Component', 'component', 'common', 5, 0, 0, '◇', 'A standard crafting component.'),
  (602, 'refined-component', 'Refined Component', 'component', 'common', 10, 0, 0, '◈', 'A refined crafting component.'),
  (603, 'advanced-component', 'Advanced Component', 'component', 'common', 15, 0, 0, '◉', 'An advanced crafting component.'),
  (604, 'superior-component', 'Superior Component', 'component', 'common', 20, 0, 0, '◎', 'A superior crafting component.'),
  (605, 'primal-component', 'Primal Component', 'component', 'common', 25, 0, 0, '✦', 'A primal crafting component.');

INSERT OR IGNORE INTO items
  (id, slug, name, slot, rarity, item_level, healing_power, max_resource_bonus, glyph, description)
VALUES
  (8, 'ashwalker-legwraps', 'Ashwalker Legwraps', 'pants', 'uncommon', 5, 3, 3, 'P', 'Layered legwraps brushed clean of furnace ash.'),
  (9, 'sootglass-pendant', 'Sootglass Pendant', 'necklace', 'rare', 5, 4, 4, 'n', 'A pendant made from glass cooled in living soot.'),
  (208, 'tempered-ashwalker-legwraps', 'Tempered Ashwalker Legwraps', 'pants', 'uncommon', 10, 6, 6, 'P', 'Veteran legwraps stitched with tempered ember thread.'),
  (209, 'tempered-sootglass-pendant', 'Tempered Sootglass Pendant', 'necklace', 'rare', 10, 8, 7, 'n', 'The sootglass holds a steadier restorative glow.'),
  (308, 'runed-ashwalker-legwraps', 'Runed Ashwalker Legwraps', 'pants', 'rare', 15, 9, 9, 'P', 'Champion runes keep each step measured and sure.'),
  (309, 'runed-sootglass-pendant', 'Runed Sootglass Pendant', 'necklace', 'rare', 15, 12, 10, 'n', 'Fine runes circle the sootglass pendant.'),
  (408, 'mythic-ashwalker-legwraps', 'Mythic Ashwalker Legwraps', 'pants', 'epic', 20, 12, 12, 'P', 'Mythic legwraps that shed molten grit.'),
  (409, 'mythic-sootglass-pendant', 'Mythic Sootglass Pendant', 'necklace', 'epic', 20, 16, 13, 'n', 'A flawless pendant carrying furnace-born warmth.'),
  (508, 'ascendant-ashwalker-legwraps', 'Ascendant Ashwalker Legwraps', 'pants', 'epic', 25, 15, 15, 'P', 'Ascendant legwraps woven from fireproof daylight.'),
  (509, 'ascendant-sootglass-pendant', 'Ascendant Sootglass Pendant', 'necklace', 'epic', 25, 20, 16, 'n', 'The sootglass burns with a contained dawn.'),
  (708, 'caldera-legwraps', 'Caldera Legwraps', 'pants', 'rare', 7, 4, 6, 'P', 'Raid legwraps made for unstable volcanic stone.'),
  (709, 'gateflame-pendant', 'Gateflame Pendant', 'necklace', 'rare', 7, 6, 5, 'n', 'A pendant bearing a cooled mote of gateflame.'),
  (717, 'royal-caldera-legwraps', 'Royal Caldera Legwraps', 'pants', 'epic', 10, 7, 9, 'P', 'Royal legwraps recovered from the Ember Crown court.'),
  (718, 'ember-crown-pendant', 'Ember Crown Pendant', 'necklace', 'epic', 10, 9, 8, 'n', 'A pendant set with a harmless shard of the Ember Crown.'),
  (108, 'novice-trousers', 'Novice Trousers', 'pants', 'common', 1, 0, 1, 'P', 'Plain trousers for a first expedition.'),
  (109, 'novice-pendant', 'Novice Pendant', 'necklace', 'common', 1, 1, 0, 'n', 'A simple charm worn by apprentice healers.'),
  (800, 'ashen-cowl-pattern', 'Ashen Cowl Pattern', 'component', 'common', 5, 0, 0, 'H', 'A Warden Vhal pattern used to craft helmets.'),
  (801, 'ashen-vestment-pattern', 'Ashen Vestment Pattern', 'component', 'common', 5, 0, 0, 'C', 'A Warden Vhal pattern used to craft chest pieces.'),
  (802, 'ashen-wrap-pattern', 'Ashen Wrap Pattern', 'component', 'common', 5, 0, 0, 'G', 'A Warden Vhal pattern used to craft gloves.'),
  (803, 'cinderstep-pattern', 'Cinderstep Pattern', 'component', 'common', 5, 0, 0, 'B', 'A Forge-Priestess Haela pattern used to craft boots.'),
  (804, 'emberglass-setting', 'Emberglass Setting', 'component', 'common', 5, 0, 0, 'R', 'A Forge-Priestess Haela setting used to craft rings.'),
  (805, 'warden-ember-core', 'Warden Ember Core', 'component', 'common', 5, 0, 0, 'T', 'A Forge-Priestess Haela core used to craft trinkets.'),
  (806, 'ashwood-focus-pattern', 'Ashwood Focus Pattern', 'component', 'common', 5, 0, 0, 'W', 'An Old Furnace pattern used to craft weapons.'),
  (807, 'furnace-legwrap-pattern', 'Furnace Legwrap Pattern', 'component', 'common', 5, 0, 0, 'L', 'An Old Furnace pattern used to craft pants.'),
  (808, 'sootglass-pendant-pattern', 'Sootglass Pendant Pattern', 'component', 'common', 5, 0, 0, 'N', 'An Old Furnace pattern used to craft necklaces.'),
  (820, 'vhal-emberplate', 'Vhal Emberplate', 'component', 'uncommon', 5, 0, 0, 'V', 'A boss material from Warden Vhal.'),
  (821, 'haela-forgebrand', 'Haela Forgebrand', 'component', 'uncommon', 5, 0, 0, 'F', 'A boss material from Forge-Priestess Haela.'),
  (822, 'old-furnace-heartshard', 'Old Furnace Heartshard', 'component', 'uncommon', 5, 0, 0, 'O', 'A boss material from the Old Furnace.'),
  (830, 'gatekeeper-cowl-pattern', 'Gatekeeper Cowl Pattern', 'component', 'rare', 10, 0, 0, 'H', 'A Gatekeeper Arkon pattern used to craft raid helmets.'),
  (831, 'crownward-vestment-pattern', 'Crownward Vestment Pattern', 'component', 'rare', 10, 0, 0, 'C', 'A Gatekeeper Arkon pattern used to craft raid chest pieces.'),
  (832, 'crownward-wrap-pattern', 'Crownward Wrap Pattern', 'component', 'rare', 10, 0, 0, 'G', 'A Gatekeeper Arkon pattern used to craft raid gloves.'),
  (833, 'caldera-tread-pattern', 'Caldera Tread Pattern', 'component', 'rare', 10, 0, 0, 'B', 'A High Inquisitor Vael pattern used to craft raid boots.'),
  (834, 'royal-signet-setting', 'Royal Signet Setting', 'component', 'rare', 10, 0, 0, 'R', 'A High Inquisitor Vael setting used to craft raid rings.'),
  (835, 'living-coal-vessel', 'Living Coal Vessel', 'component', 'rare', 10, 0, 0, 'T', 'A High Inquisitor Vael vessel used to craft raid trinkets.'),
  (836, 'crownshard-focus-pattern', 'Crownshard Focus Pattern', 'component', 'rare', 10, 0, 0, 'W', 'An Ember Crown pattern used to craft raid weapons.'),
  (837, 'inquisitor-legwrap-pattern', 'Inquisitor Legwrap Pattern', 'component', 'rare', 10, 0, 0, 'L', 'An Ember Crown pattern used to craft raid pants.'),
  (838, 'crown-pendant-pattern', 'Crown Pendant Pattern', 'component', 'rare', 10, 0, 0, 'N', 'An Ember Crown pattern used to craft raid necklaces.'),
  (850, 'arkon-gate-sigil', 'Arkon Gate Sigil', 'component', 'rare', 10, 0, 0, 'A', 'A raid boss material from Gatekeeper Arkon.'),
  (851, 'vael-brandseal', 'Vael Brandseal', 'component', 'rare', 10, 0, 0, 'I', 'A raid boss material from High Inquisitor Vael.'),
  (852, 'ember-crown-shard', 'Ember Crown Shard', 'component', 'rare', 10, 0, 0, 'E', 'A raid boss material from the Ember Crown.'),
  (860, 'bulldrome-hide', 'Bulldrome Hide', 'component', 'uncommon', 5, 0, 0, 'H', 'A rugged hide from Bulldrome.'),
  (861, 'bullfango-head', 'Bullfango Head', 'component', 'rare', 5, 0, 0, 'F', 'A rare head taken from a Bullfango.'),
  (862, 'bulldrome-tusk', 'Bulldrome Tusk', 'component', 'uncommon', 5, 0, 0, 'T', 'A heavy tusk from Bulldrome.'),
  (863, 'giant-bone', 'Giant Bone', 'component', 'uncommon', 5, 0, 0, 'B', 'A giant bone used in sturdy crafting.'),
  (864, 'kut-ku-webbing', 'Kut-Ku Webbing', 'component', 'uncommon', 5, 0, 0, 'W', 'Flexible webbing from Yian Kut-Ku.'),
  (865, 'kut-ku-shell', 'Kut-Ku Shell', 'component', 'uncommon', 5, 0, 0, 'S', 'A bright shell from Yian Kut-Ku.'),
  (866, 'kut-ku-scale', 'Kut-Ku Scale', 'component', 'uncommon', 5, 0, 0, 'K', 'A scale from Yian Kut-Ku.'),
  (867, 'giant-beak', 'Giant Beak', 'component', 'rare', 5, 0, 0, 'G', 'A rare giant beak from Yian Kut-Ku.'),
  (868, 'rathian-shell', 'Rathian Shell', 'component', 'uncommon', 5, 0, 0, 'R', 'A durable shell from Rathian.'),
  (869, 'rathian-scale-x2', 'Rathian Scale x2', 'component', 'uncommon', 5, 0, 0, '2', 'A paired Rathian scale reward.'),
  (870, 'rathian-spike', 'Rathian Spike', 'component', 'rare', 5, 0, 0, 'P', 'A sharp spike from Rathian.'),
  (871, 'rathian-plate', 'Rathian Plate', 'component', 'epic', 5, 0, 0, '*', 'A prized Rathian plate.');

UPDATE items SET slot = 'ring', item_level = 5 WHERE slug = 'emberglass-sigil';
UPDATE items SET item_level = 5 WHERE slug = 'wardens-cinderwrap';
UPDATE items SET glyph = 'o' WHERE slug = 'emberglass-sigil';
UPDATE items SET glyph = 'C' WHERE slug = 'wardens-cinderwrap';
UPDATE items SET glyph = '/' WHERE slug = 'ashwood-crook';
UPDATE items SET glyph = 'b' WHERE slug = 'cinderstep-boots';
UPDATE items SET glyph = '^' WHERE slug = 'adepts-hood';

INSERT OR IGNORE INTO encounters
  (id, dungeon_id, sequence, slug, name, encounter_type, max_health, base_damage, tank_damage, party_damage, description)
VALUES
  (3, 1, 9003, 'legacy-loot-encounter-3', 'Legacy Loot Encounter 3', 'boss', 1, 1, 1, 1, 'Temporary legacy seed row.'),
  (12, 1, 9012, 'legacy-loot-encounter-12', 'Legacy Loot Encounter 12', 'boss', 1, 1, 1, 1, 'Temporary legacy seed row.'),
  (22, 1, 9022, 'legacy-loot-encounter-22', 'Legacy Loot Encounter 22', 'boss', 1, 1, 1, 1, 'Temporary legacy seed row.'),
  (102, 2, 9102, 'legacy-loot-encounter-102', 'Legacy Loot Encounter 102', 'boss', 1, 1, 1, 1, 'Temporary legacy seed row.'),
  (105, 2, 9105, 'legacy-loot-encounter-105', 'Legacy Loot Encounter 105', 'boss', 1, 1, 1, 1, 'Temporary legacy seed row.'),
  (108, 2, 9108, 'legacy-loot-encounter-108', 'Legacy Loot Encounter 108', 'boss', 1, 1, 1, 1, 'Temporary legacy seed row.');

DELETE FROM encounter_loot;

INSERT INTO encounter_loot
  (encounter_id, item_id, difficulty_id, drop_weight, drop_chance)
VALUES
  (3, 860, 1, 50, 1.0), (3, 861, 1, 5, 1.0), (3, 862, 1, 15, 1.0), (3, 863, 1, 30, 1.0),
  (3, 860, 2, 50, 1.0), (3, 861, 2, 5, 1.0), (3, 862, 2, 15, 1.0), (3, 863, 2, 30, 1.0),
  (3, 860, 3, 50, 1.0), (3, 861, 3, 5, 1.0), (3, 862, 3, 15, 1.0), (3, 863, 3, 30, 1.0),
  (3, 860, 4, 50, 1.0), (3, 861, 4, 5, 1.0), (3, 862, 4, 15, 1.0), (3, 863, 4, 30, 1.0),
  (3, 860, 5, 50, 1.0), (3, 861, 5, 5, 1.0), (3, 862, 5, 15, 1.0), (3, 863, 5, 30, 1.0),
  (12, 864, 1, 32, 1.0), (12, 865, 1, 50, 1.0), (12, 866, 1, 13, 1.0), (12, 867, 1, 5, 1.0),
  (12, 864, 2, 32, 1.0), (12, 865, 2, 50, 1.0), (12, 866, 2, 13, 1.0), (12, 867, 2, 5, 1.0),
  (12, 864, 3, 32, 1.0), (12, 865, 3, 50, 1.0), (12, 866, 3, 13, 1.0), (12, 867, 3, 5, 1.0),
  (12, 864, 4, 32, 1.0), (12, 865, 4, 50, 1.0), (12, 866, 4, 13, 1.0), (12, 867, 4, 5, 1.0),
  (12, 864, 5, 32, 1.0), (12, 865, 5, 50, 1.0), (12, 866, 5, 13, 1.0), (12, 867, 5, 5, 1.0),
  (22, 868, 1, 36, 1.0), (22, 869, 1, 52, 1.0), (22, 870, 1, 10, 1.0), (22, 871, 1, 2, 1.0),
  (22, 868, 2, 36, 1.0), (22, 869, 2, 52, 1.0), (22, 870, 2, 10, 1.0), (22, 871, 2, 2, 1.0),
  (22, 868, 3, 36, 1.0), (22, 869, 3, 52, 1.0), (22, 870, 3, 10, 1.0), (22, 871, 3, 2, 1.0),
  (22, 868, 4, 36, 1.0), (22, 869, 4, 52, 1.0), (22, 870, 4, 10, 1.0), (22, 871, 4, 2, 1.0),
  (22, 868, 5, 36, 1.0), (22, 869, 5, 52, 1.0), (22, 870, 5, 10, 1.0), (22, 871, 5, 2, 1.0),
  (102, 602, 101, 24, 1.0), (102, 850, 101, 22, 1.0), (102, 830, 101, 18, 1.0), (102, 831, 101, 18, 1.0), (102, 832, 101, 18, 1.0),
  (105, 602, 101, 24, 1.0), (105, 851, 101, 22, 1.0), (105, 833, 101, 18, 1.0), (105, 834, 101, 18, 1.0), (105, 835, 101, 18, 1.0),
  (108, 602, 101, 24, 1.0), (108, 852, 101, 22, 1.0), (108, 836, 101, 18, 1.0), (108, 837, 101, 18, 1.0), (108, 838, 101, 18, 1.0);

DELETE FROM crafting_recipe_components;
DELETE FROM crafting_recipes;

INSERT INTO crafting_recipes
  (id, item_id, difficulty_id, source_dungeon_id, source_encounter_id)
VALUES
  (901, 101, 1, 1, 3), (902, 102, 1, 1, 3), (903, 103, 1, 1, 3),
  (904, 104, 1, 1, 12), (905, 105, 1, 1, 12), (906, 106, 1, 1, 12),
  (907, 100, 1, 1, 22), (908, 108, 1, 1, 22), (909, 109, 1, 1, 22),
  (1001, 5, 1, 1, 3), (1002, 2, 1, 1, 3), (1003, 6, 1, 1, 3),
  (1004, 4, 1, 1, 12), (1005, 1, 1, 1, 12), (1006, 7, 1, 1, 12),
  (1007, 3, 1, 1, 22), (1008, 8, 1, 1, 22), (1009, 9, 1, 1, 22),
  (1101, 205, 2, 1, 3), (1102, 202, 2, 1, 3), (1103, 206, 2, 1, 3),
  (1104, 204, 2, 1, 12), (1105, 201, 2, 1, 12), (1106, 207, 2, 1, 12),
  (1107, 203, 2, 1, 22), (1108, 208, 2, 1, 22), (1109, 209, 2, 1, 22),
  (1201, 305, 3, 1, 3), (1202, 302, 3, 1, 3), (1203, 306, 3, 1, 3),
  (1204, 304, 3, 1, 12), (1205, 301, 3, 1, 12), (1206, 307, 3, 1, 12),
  (1207, 303, 3, 1, 22), (1208, 308, 3, 1, 22), (1209, 309, 3, 1, 22),
  (1301, 405, 4, 1, 3), (1302, 402, 4, 1, 3), (1303, 406, 4, 1, 3),
  (1304, 404, 4, 1, 12), (1305, 401, 4, 1, 12), (1306, 407, 4, 1, 12),
  (1307, 403, 4, 1, 22), (1308, 408, 4, 1, 22), (1309, 409, 4, 1, 22),
  (1401, 505, 5, 1, 3), (1402, 502, 5, 1, 3), (1403, 506, 5, 1, 3),
  (1404, 504, 5, 1, 12), (1405, 501, 5, 1, 12), (1406, 507, 5, 1, 12),
  (1407, 503, 5, 1, 22), (1408, 508, 5, 1, 22), (1409, 509, 5, 1, 22),
  (2001, 714, 101, 2, 102), (2002, 711, 101, 2, 102), (2003, 715, 101, 2, 102),
  (2004, 713, 101, 2, 105), (2005, 710, 101, 2, 105), (2006, 716, 101, 2, 105),
  (2007, 712, 101, 2, 108), (2008, 717, 101, 2, 108), (2009, 718, 101, 2, 108);

WITH recipe_requirements(recipe_id, tier_component_id, tier_qty, boss_component_id, pattern_component_id) AS (
  VALUES
    (1001, 601, 1, 820, 800), (1002, 601, 1, 820, 801), (1003, 601, 1, 820, 802),
    (1004, 601, 1, 821, 803), (1005, 601, 1, 821, 804), (1006, 601, 1, 821, 805),
    (1007, 601, 1, 822, 806), (1008, 601, 1, 822, 807), (1009, 601, 1, 822, 808),
    (1101, 602, 2, 820, 800), (1102, 602, 2, 820, 801), (1103, 602, 2, 820, 802),
    (1104, 602, 2, 821, 803), (1105, 602, 2, 821, 804), (1106, 602, 2, 821, 805),
    (1107, 602, 2, 822, 806), (1108, 602, 2, 822, 807), (1109, 602, 2, 822, 808),
    (1201, 603, 3, 820, 800), (1202, 603, 3, 820, 801), (1203, 603, 3, 820, 802),
    (1204, 603, 3, 821, 803), (1205, 603, 3, 821, 804), (1206, 603, 3, 821, 805),
    (1207, 603, 3, 822, 806), (1208, 603, 3, 822, 807), (1209, 603, 3, 822, 808),
    (1301, 604, 4, 820, 800), (1302, 604, 4, 820, 801), (1303, 604, 4, 820, 802),
    (1304, 604, 4, 821, 803), (1305, 604, 4, 821, 804), (1306, 604, 4, 821, 805),
    (1307, 604, 4, 822, 806), (1308, 604, 4, 822, 807), (1309, 604, 4, 822, 808),
    (1401, 605, 5, 820, 800), (1402, 605, 5, 820, 801), (1403, 605, 5, 820, 802),
    (1404, 605, 5, 821, 803), (1405, 605, 5, 821, 804), (1406, 605, 5, 821, 805),
    (1407, 605, 5, 822, 806), (1408, 605, 5, 822, 807), (1409, 605, 5, 822, 808),
    (2001, 602, 3, 850, 830), (2002, 602, 3, 850, 831), (2003, 602, 3, 850, 832),
    (2004, 602, 3, 851, 833), (2005, 602, 3, 851, 834), (2006, 602, 3, 851, 835),
    (2007, 602, 3, 852, 836), (2008, 602, 3, 852, 837), (2009, 602, 3, 852, 838)
)
INSERT INTO crafting_recipe_components (recipe_id, item_id, quantity)
SELECT recipe_id, tier_component_id, tier_qty FROM recipe_requirements
UNION ALL
SELECT recipe_id, boss_component_id, 1 FROM recipe_requirements
UNION ALL
SELECT recipe_id, pattern_component_id, 1 FROM recipe_requirements;

DELETE FROM item_set_bonuses;
DELETE FROM item_set_items;
DELETE FROM item_sets;

INSERT INTO item_sets (id, slug, name, description) VALUES
  (1, 'ember-crown-regalia', 'Ember Crown Regalia', 'A raid set crafted from Citadel of the Ember Crown materials.');

INSERT INTO item_set_items (set_id, item_id) VALUES
  (1, 710), (1, 711), (1, 712), (1, 713), (1, 714), (1, 715), (1, 716), (1, 717), (1, 718);

INSERT INTO item_set_bonuses
  (id, set_id, required_pieces, effect_type, description)
VALUES
  (1, 1, 2, 'mend_extra_target', 'Mend heals 1 additional target.'),
  (2, 1, 4, 'renew_extra_target', 'Renew is applied to 1 additional target.'),
  (3, 1, 7, 'mend_applies_renew', 'Casting Mend on a target applies Renew.');

DELETE FROM dungeon_completion_loot;

INSERT OR IGNORE INTO characters (id, class_id, name, level, experience) VALUES
  (1, 1, 'Mira', 1, 0),
  (2, 2, 'Mira', 1, 0),
  (3, 3, 'Mira', 1, 0);

INSERT OR IGNORE INTO character_ability_slots (character_id, slot_number, spell_id) VALUES
  (1, 1, 1), (1, 2, 2), (1, 3, 3), (1, 4, 4), (1, 5, 5), (1, 6, NULL),
  (2, 1, 20), (2, 2, 21), (2, 3, 22), (2, 4, 23), (2, 5, 24), (2, 6, NULL),
  (3, 1, 30), (3, 2, 31), (3, 3, 32), (3, 4, 33), (3, 5, 34), (3, 6, NULL);

INSERT OR IGNORE INTO character_inventory (character_id, item_id, quantity, equipped) VALUES
  (1, 100, 1, 1), (1, 101, 1, 1), (1, 102, 1, 1), (1, 103, 1, 1),
  (1, 104, 1, 1), (1, 108, 1, 1), (1, 105, 1, 1), (1, 109, 1, 1), (1, 106, 1, 1), (1, 107, 1, 0),
  (2, 100, 1, 1), (2, 101, 1, 1), (2, 102, 1, 1), (2, 103, 1, 1),
  (2, 104, 1, 1), (2, 108, 1, 1), (2, 105, 1, 1), (2, 109, 1, 1), (2, 106, 1, 1), (2, 107, 1, 0),
  (3, 100, 1, 1), (3, 101, 1, 1), (3, 102, 1, 1), (3, 103, 1, 1),
  (3, 104, 1, 1), (3, 108, 1, 1), (3, 105, 1, 1), (3, 109, 1, 1), (3, 106, 1, 1), (3, 107, 1, 0);

DELETE FROM character_inventory
WHERE character_id IN (1, 2, 3)
  AND item_id BETWEEN 100 AND 109;

-- Coin gearing override: every boss/difficulty drops one boss coin, and each
-- craft costs the target item level in that source boss coin.
UPDATE crafting_recipes
SET source_dungeon_id = CASE WHEN EXISTS (SELECT 1 FROM encounters WHERE id = 3) THEN 1 ELSE NULL END,
    source_encounter_id = (SELECT id FROM encounters WHERE id = 3)
WHERE id BETWEEN 1001 AND 1409
  AND item_id IN (SELECT id FROM items WHERE slot IN ('helmet', 'chest', 'gloves'));

UPDATE crafting_recipes
SET source_dungeon_id = CASE WHEN EXISTS (SELECT 1 FROM encounters WHERE id = 12) THEN 1 ELSE NULL END,
    source_encounter_id = (SELECT id FROM encounters WHERE id = 12)
WHERE id BETWEEN 1001 AND 1409
  AND item_id IN (SELECT id FROM items WHERE slot IN ('boots', 'ring', 'trinket'));

UPDATE crafting_recipes
SET source_dungeon_id = CASE WHEN EXISTS (SELECT 1 FROM encounters WHERE id = 22) THEN 1 ELSE NULL END,
    source_encounter_id = (SELECT id FROM encounters WHERE id = 22)
WHERE id BETWEEN 1001 AND 1409
  AND item_id IN (SELECT id FROM items WHERE slot IN ('weapon', 'pants', 'necklace'));

UPDATE crafting_recipes
SET difficulty_id = CASE
  (SELECT item_level FROM items WHERE items.id = crafting_recipes.item_id)
    WHEN 1 THEN 1
    WHEN 5 THEN 1
    WHEN 10 THEN 2
    WHEN 15 THEN 2
    WHEN 20 THEN 4
    WHEN 25 THEN 5
    ELSE difficulty_id
  END
WHERE id BETWEEN 901 AND 1409;

UPDATE items
SET rarity = CASE item_level
    WHEN 1 THEN 'common'
    WHEN 10 THEN 'uncommon'
    WHEN 20 THEN 'epic'
    WHEN 25 THEN 'legendary'
    ELSE rarity
  END
WHERE id IN (SELECT item_id FROM crafting_recipes);

UPDATE items
SET name = COALESCE((
    SELECT
      CASE items.item_level
        WHEN 1 THEN 'Raw '
        WHEN 5 THEN 'Honed '
        WHEN 10 THEN 'Green '
        WHEN 15 THEN 'Blue '
        WHEN 20 THEN 'Purple '
        WHEN 25 THEN 'Orange '
        ELSE ''
      END
      || encounters.name || ' '
      || CASE items.slot
        WHEN 'weapon' THEN 'Weapon'
        WHEN 'helmet' THEN 'Helmet'
        WHEN 'chest' THEN 'Chest'
        WHEN 'gloves' THEN 'Gloves'
        WHEN 'boots' THEN 'Boots'
        WHEN 'pants' THEN 'Pants'
        WHEN 'ring' THEN 'Ring'
        WHEN 'necklace' THEN 'Necklace'
        WHEN 'trinket' THEN 'Trinket'
        ELSE items.name
      END
    FROM crafting_recipes
    JOIN encounters ON encounters.id = crafting_recipes.source_encounter_id
    WHERE crafting_recipes.item_id = items.id
    LIMIT 1
  ), name),
  description = COALESCE((
    SELECT 'Crafted with ' || encounters.name || ' coins.'
    FROM crafting_recipes
    JOIN encounters ON encounters.id = crafting_recipes.source_encounter_id
    WHERE crafting_recipes.item_id = items.id
    LIMIT 1
  ), description)
WHERE id IN (SELECT item_id FROM crafting_recipes);

CREATE TEMP TABLE IF NOT EXISTS coin_sources (
  item_id INTEGER PRIMARY KEY,
  encounter_id INTEGER NOT NULL,
  difficulty_id INTEGER NOT NULL,
  item_level INTEGER NOT NULL,
  slug TEXT NOT NULL,
  name TEXT NOT NULL,
  rarity TEXT NOT NULL,
  glyph TEXT NOT NULL,
  description TEXT NOT NULL
);

DELETE FROM coin_sources;

INSERT INTO coin_sources
SELECT
  280000 + encounters.id * 1000 + difficulties.id,
  encounters.id,
  difficulties.id,
  difficulties.dropped_item_level,
  encounters.slug || '-coin-diff-' || difficulties.id || '-ilvl-' || difficulties.dropped_item_level,
  CASE difficulties.dropped_item_level
    WHEN 1 THEN 'Raw '
    WHEN 5 THEN 'Honed '
    WHEN 10 THEN 'Green '
    WHEN 15 THEN 'Blue '
    WHEN 20 THEN 'Purple '
    WHEN 25 THEN 'Orange '
    ELSE ''
  END || encounters.name || ' Coin',
  CASE difficulties.dropped_item_level
    WHEN 1 THEN 'common'
    WHEN 5 THEN 'common'
    WHEN 10 THEN 'uncommon'
    WHEN 15 THEN 'rare'
    WHEN 20 THEN 'epic'
    WHEN 25 THEN 'legendary'
    ELSE 'common'
  END,
  '$',
  'A boss coin from ' || encounters.name || ' used for item level '
    || difficulties.dropped_item_level || ' crafting.'
FROM encounters
JOIN dungeon_difficulties ON dungeon_difficulties.dungeon_id = encounters.dungeon_id
JOIN difficulties ON difficulties.id = dungeon_difficulties.difficulty_id
WHERE encounters.encounter_type = 'boss';

INSERT OR IGNORE INTO items
  (id, slug, name, slot, rarity, item_level, healing_power, max_resource_bonus, glyph, description)
SELECT item_id, slug, name, 'component', rarity, item_level, 0, 0, glyph, description
FROM coin_sources;

UPDATE coin_sources
SET item_id = (
  SELECT items.id
  FROM items
  WHERE items.slug = coin_sources.slug
)
WHERE EXISTS (
  SELECT 1
  FROM items
  WHERE items.slug = coin_sources.slug
);

UPDATE items
SET slug = (SELECT slug FROM coin_sources WHERE coin_sources.item_id = items.id),
    name = (SELECT name FROM coin_sources WHERE coin_sources.item_id = items.id),
    slot = 'component',
    rarity = (SELECT rarity FROM coin_sources WHERE coin_sources.item_id = items.id),
    item_level = (SELECT item_level FROM coin_sources WHERE coin_sources.item_id = items.id),
    healing_power = 0,
    max_resource_bonus = 0,
    glyph = (SELECT glyph FROM coin_sources WHERE coin_sources.item_id = items.id),
    description = (SELECT description FROM coin_sources WHERE coin_sources.item_id = items.id)
WHERE id IN (SELECT item_id FROM coin_sources);

DELETE FROM encounter_loot;

INSERT INTO encounter_loot (encounter_id, item_id, difficulty_id, drop_weight, drop_chance)
SELECT encounter_id, item_id, difficulty_id, 100, 1.0
FROM coin_sources;

DELETE FROM crafting_recipe_components;

INSERT INTO crafting_recipe_components (recipe_id, item_id, quantity)
SELECT
  crafting_recipes.id,
  coin_sources.item_id,
  items.item_level
FROM crafting_recipes
JOIN items ON items.id = crafting_recipes.item_id
JOIN coin_sources
  ON coin_sources.encounter_id = crafting_recipes.source_encounter_id
  AND coin_sources.difficulty_id = crafting_recipes.difficulty_id;


DELETE FROM character_talents
WHERE talent_id IN (SELECT id FROM talents WHERE class_id = 1);

DELETE FROM talents WHERE class_id = 1;

INSERT OR IGNORE INTO talents
  (id, class_id, slug, name, max_rank, tier, branch, prerequisite_talent_id, prerequisite_rank, effect_type, effect_value_per_rank, glyph, description)
VALUES
  (1, 1, 'shield-applies-renew', 'Shield applies Renew', 1, 1, 1, NULL, 0, 'shield_applies_renew', 0, '~', 'Sun Ward also applies Renew to the target.'),
  (2, 1, 'mend-applies-renew', 'Mend applies Renew', 1, 1, 2, NULL, 0, 'mend_applies_renew', 0, '~', 'Mend also applies Renew to the target.'),
  (10, 1, 'mend-adds-shield', 'Mend adds Shield', 1, 1, 3, NULL, 0, 'mend_applies_shield', 0, 'O', 'Mend also applies a shield at 50% strength to the target.'),
  (11, 1, 'radiance-adds-shield', 'Radiance adds Shield', 1, 1, 4, NULL, 0, 'radiance_applies_shield', 0, 'O', 'Radiance applies a shield at 30% strength to affected party members.'),
  (12, 1, 'radiance-applies-renew', 'Radiance applies Renew', 1, 1, 5, NULL, 0, 'radiance_applies_renew', 0, '~', 'Radiance applies Renew at 50% duration to affected party members.'),
  (13, 1, 'shielded-damage-reduction', 'Shielded takes less', 1, 1, 6, NULL, 0, 'shielded_damage_reduction', 0, 'D', 'While shielded, the target receives 20% less damage.'),
  (14, 1, 'shielded-healing-bonus', 'Shielded healing boost', 1, 1, 7, NULL, 0, 'shielded_healing_bonus', 0, '+', 'While shielded, the target receives 20% more healing.'),
  (15, 1, 'mend-reduces-radiance', 'Mend lowers Radiance', 1, 1, 8, NULL, 0, 'mend_reduces_radiance_cooldown', 0, '*', 'Casting Mend reduces the cooldown of Radiance by 2 seconds.'),

  (3, 2, 'deep-roots', 'Deep Roots', 5, 1, 1, NULL, 0, 'max_resource', 2, 'R', 'Increases maximum Bloom by 2 per rank.'),
  (20, 2, 'patient-growth', 'Patient Growth', 5, 1, 2, NULL, 0, 'hot_power_percent', 2, 's', 'Increases healing-over-time power by 2% per rank.'),
  (21, 2, 'living-bark', 'Living Bark', 5, 1, 3, NULL, 0, 'absorb_power_percent', 2, 'B', 'Strengthens protective effects by 2% per rank.'),
  (22, 2, 'spring-rain', 'Spring Rain', 5, 2, 1, 3, 3, 'resource_regen_percent', 2, 'r', 'Improves Bloom regeneration by 2% per rank. Requires Deep Roots rank 3.'),
  (23, 2, 'perennial', 'Perennial', 5, 2, 2, 20, 3, 'hot_duration_percent', 4, 'P', 'Extends healing-over-time duration by 4% per rank. Requires Patient Growth rank 3.'),
  (24, 2, 'sheltering-canopy', 'Sheltering Canopy', 5, 2, 3, 21, 3, 'party_heal_percent', 2, 'C', 'Improves healing on injured allies by 2% per rank. Requires Living Bark rank 3.'),
  (25, 2, 'old-growth', 'Old Growth', 3, 3, 1, 22, 5, 'direct_heal_percent', 5, 'T', 'Strengthens direct healing by 5% per rank. Requires Spring Rain rank 5.'),
  (26, 2, 'endless-season', 'Endless Season', 3, 3, 2, 23, 5, 'hot_power_percent', 5, 'E', 'Further improves periodic healing by 5% per rank. Requires Perennial rank 5.'),
  (27, 2, 'heart-of-the-grove', 'Heart of the Grove', 1, 4, 2, 26, 3, 'cooldown_reduction_percent', 10, 'H', 'Reduces major healing cooldowns by 10%. Requires Endless Season rank 3.'),

  (4, 3, 'precise-script', 'Precise Script', 5, 1, 1, NULL, 0, 'direct_heal_percent', 2, '+', 'Increases direct healing by 2% per rank.'),
  (40, 3, 'deep-focus', 'Deep Focus', 5, 1, 2, NULL, 0, 'max_resource', 2, 'F', 'Increases maximum Focus by 2 per rank.'),
  (41, 3, 'reinforced-runes', 'Reinforced Runes', 5, 1, 3, NULL, 0, 'absorb_power_percent', 2, 'O', 'Strengthens barriers by 2% per rank.'),
  (42, 3, 'efficient-lines', 'Efficient Lines', 5, 2, 1, 4, 3, 'resource_cost_percent', -2, 'E', 'Reduces ability costs by 2% per rank. Requires Precise Script rank 3.'),
  (43, 3, 'resonant-pattern', 'Resonant Pattern', 5, 2, 2, 40, 3, 'party_heal_percent', 2, '*', 'Increases linked party healing by 2% per rank. Requires Deep Focus rank 3.'),
  (44, 3, 'layered-aegis', 'Layered Aegis', 5, 2, 3, 41, 3, 'absorb_power_percent', 3, 'A', 'Further improves barriers by 3% per rank. Requires Reinforced Runes rank 3.'),
  (45, 3, 'perfect-notation', 'Perfect Notation', 3, 3, 1, 42, 5, 'critical_heal_percent', 2, '!', 'Adds 2% healing critical chance per rank. Requires Efficient Lines rank 5.'),
  (46, 3, 'grand-concordance', 'Grand Concordance', 3, 3, 2, 43, 5, 'party_heal_percent', 5, 'G', 'Improves party-wide healing by 5% per rank. Requires Resonant Pattern rank 5.'),
  (47, 3, 'masterwork', 'Masterwork', 1, 4, 2, 46, 3, 'cooldown_reduction_percent', 10, 'M', 'Reduces major healing cooldowns by 10%. Requires Grand Concordance rank 3.');

UPDATE talents SET
  branch = CASE slug
    WHEN 'bright-reserves' THEN 1
    WHEN 'gentle-dawn' THEN 2
    WHEN 'deep-roots' THEN 1
    WHEN 'precise-script' THEN 1
    ELSE branch
  END,
  effect_type = CASE slug
    WHEN 'bright-reserves' THEN 'max_resource'
    WHEN 'gentle-dawn' THEN 'hot_power_percent'
    WHEN 'deep-roots' THEN 'max_resource'
    WHEN 'precise-script' THEN 'direct_heal_percent'
    ELSE effect_type
  END,
  effect_value_per_rank = 2,
  glyph = CASE slug
    WHEN 'bright-reserves' THEN 'M'
    WHEN 'gentle-dawn' THEN '~'
    WHEN 'deep-roots' THEN 'R'
    WHEN 'precise-script' THEN '+'
    ELSE glyph
  END
WHERE slug IN ('bright-reserves', 'gentle-dawn', 'deep-roots', 'precise-script');

UPDATE talents SET description = 'Increases maximum Mana by 2 per rank.'
WHERE slug = 'bright-reserves';
UPDATE talents SET description = 'Increases healing-over-time power by 2% per rank.'
WHERE slug = 'gentle-dawn';
UPDATE talents SET description = 'Increases maximum Bloom by 2 per rank.'
WHERE slug = 'deep-roots';
UPDATE talents SET description = 'Increases direct healing by 2% per rank.'
WHERE slug = 'precise-script';

INSERT INTO game_settings (key, value) VALUES
  ('max_level', '25'),
  ('max_talent_points', '25'),
  ('max_ability_slots', '6')
ON CONFLICT(key) DO UPDATE SET value = excluded.value;

DELETE FROM level_progression WHERE level > 25;

WITH RECURSIVE levels(level) AS (
  SELECT 1
  UNION ALL
  SELECT level + 1 FROM levels WHERE level < 25
)
INSERT OR IGNORE INTO level_progression (level, experience_required, talent_points_total)
SELECT level, (level - 1) * (level - 1) * 100, level
FROM levels;

UPDATE characters
SET
  level = MIN(level, 25),
  experience = MIN(
    experience,
    (SELECT experience_required FROM level_progression WHERE level = 25)
  ),
  talent_points = MIN(talent_points, 25);

UPDATE character_ability_slots
SET spell_id = NULL
WHERE spell_id IN (
  SELECT spells.id
  FROM spells
  JOIN characters ON characters.id = character_ability_slots.character_id
  WHERE spells.unlock_level > characters.level
);

-- Generated monster-part loot tiers. Dungeon 1 remains authored by hand above.
CREATE TEMP TABLE IF NOT EXISTS generated_loot_tiers (
  item_level INTEGER PRIMARY KEY,
  dungeon_id INTEGER NOT NULL,
  raid_id INTEGER NOT NULL,
  dungeon_difficulty_id INTEGER NOT NULL,
  raid_difficulty_id INTEGER NOT NULL,
  recipe_base INTEGER NOT NULL,
  craft_quantity INTEGER NOT NULL
);

DELETE FROM generated_loot_tiers;
INSERT INTO generated_loot_tiers
  (item_level, dungeon_id, raid_id, dungeon_difficulty_id, raid_difficulty_id, recipe_base, craft_quantity)
VALUES
  (10, 3, 2, 2, 101, 1100, 2),
  (15, 4, 5, 3, 103, 1200, 3),
  (20, 6, 7, 4, 104, 1300, 4),
  (25, 8, 9, 5, 105, 1400, 5);

CREATE TEMP TABLE IF NOT EXISTS generated_bosses (
  item_level INTEGER NOT NULL,
  boss_index INTEGER NOT NULL,
  boss_global_index INTEGER NOT NULL,
  boss_name TEXT NOT NULL,
  boss_slug TEXT NOT NULL,
  PRIMARY KEY (item_level, boss_index)
);

DELETE FROM generated_bosses;
INSERT INTO generated_bosses
  (item_level, boss_index, boss_global_index, boss_name, boss_slug)
VALUES
  (10, 0, 0, 'Tigrex', 'tigrex'),
  (10, 1, 1, 'Rathalos', 'rathalos'),
  (10, 2, 2, 'Gypceros', 'gypceros'),
  (15, 0, 3, 'Nargacuga', 'nargacuga'),
  (15, 1, 4, 'Azuros', 'azuros'),
  (15, 2, 5, 'Diablos', 'diablos'),
  (20, 0, 6, 'Barroth', 'barroth'),
  (20, 1, 7, 'Tobi Kadachi', 'tobi-kadachi'),
  (20, 2, 8, 'Monoblos', 'monoblos'),
  (25, 0, 9, 'Anjanath', 'anjanath'),
  (25, 1, 10, 'Bazelgeuse', 'bazelgeuse'),
  (25, 2, 11, 'Odogaron', 'odogaron');

CREATE TEMP TABLE IF NOT EXISTS generated_drop_patterns (
  boss_index INTEGER NOT NULL,
  drop_index INTEGER NOT NULL,
  drop_weight INTEGER NOT NULL,
  rarity TEXT NOT NULL,
  PRIMARY KEY (boss_index, drop_index)
);

DELETE FROM generated_drop_patterns;
INSERT INTO generated_drop_patterns
  (boss_index, drop_index, drop_weight, rarity)
VALUES
  (0, 1, 50, 'uncommon'),
  (0, 2, 5, 'rare'),
  (0, 3, 15, 'uncommon'),
  (0, 4, 30, 'uncommon'),
  (1, 1, 32, 'uncommon'),
  (1, 2, 50, 'uncommon'),
  (1, 3, 13, 'uncommon'),
  (1, 4, 5, 'rare'),
  (2, 1, 36, 'uncommon'),
  (2, 2, 52, 'uncommon'),
  (2, 3, 10, 'rare'),
  (2, 4, 2, 'epic');

INSERT OR IGNORE INTO locations (id, slug, name, description) VALUES
  (3, 'monster-frontier', 'The Monster Frontier', 'Hunting grounds used for tiered monster-part progression.');

UPDATE dungeons
SET slug = 'legacy-generated-raid-2',
    name = 'Legacy Generated Raid 2',
    location_id = 3,
    recommended_level = 5,
    content_type = 'raid',
    party_size = 18,
    completion_item_level = NULL,
    experience_reward = 275,
    description = 'A raid-scale hunt against Tigrex, Rathalos, and Gypceros.'
WHERE id = 2;

INSERT OR IGNORE INTO dungeons
  (id, location_id, slug, name, recommended_level, content_type, party_size, completion_item_level, experience_reward, description)
VALUES
  (3, 3, 'tigrex-hunting-ground', 'Tigrex Hunting Ground', 5, 'dungeon', 6, NULL, 205, 'A three-boss hunt featuring Tigrex, Rathalos, and Gypceros.'),
  (4, 3, 'nargacuga-hunting-ground', 'Nargacuga Hunting Ground', 10, 'dungeon', 6, NULL, 245, 'A three-boss hunt featuring Nargacuga, Azuros, and Diablos.'),
  (5, 3, 'nargacuga-raid', 'Nargacuga Raid', 10, 'raid', 18, NULL, 325, 'A raid-scale hunt against Nargacuga, Azuros, and Diablos.'),
  (6, 3, 'barroth-hunting-ground', 'Barroth Hunting Ground', 15, 'dungeon', 6, NULL, 285, 'A three-boss hunt featuring Barroth, Tobi Kadachi, and Monoblos.'),
  (7, 3, 'barroth-raid', 'Barroth Raid', 15, 'raid', 18, NULL, 375, 'A raid-scale hunt against Barroth, Tobi Kadachi, and Monoblos.'),
  (8, 3, 'anjanath-hunting-ground', 'Anjanath Hunting Ground', 20, 'dungeon', 6, NULL, 325, 'A three-boss hunt featuring Anjanath, Bazelgeuse, and Odogaron.'),
  (9, 3, 'anjanath-raid', 'Anjanath Raid', 20, 'raid', 18, NULL, 425, 'A raid-scale hunt against Anjanath, Bazelgeuse, and Odogaron.');

UPDATE difficulties
SET dropped_item_level = 10,
    unlock_level = 10,
    health_multiplier = 1.45,
    damage_multiplier = 1.25,
    experience_multiplier = 2.0,
    description = 'Veteran raid difficulty with extra monster-part drops.'
WHERE id = 101;

INSERT OR IGNORE INTO difficulties
  (id, slug, name, dropped_item_level, unlock_level, health_multiplier, damage_multiplier, experience_multiplier, description)
VALUES
  (103, 'raid-champion', 'Champion Raid', 15, 15, 1.7, 1.45, 2.4, 'Gear-only raid upgrade tier between Veteran and Mythic.'),
  (104, 'raid-mythic', 'Mythic Raid', 20, 20, 2.25, 1.85, 3.5, 'Mythic raid difficulty with extra monster-part drops.'),
  (105, 'raid-ascendant', 'Ascendant Raid', 25, 25, 2.8, 2.25, 4.5, 'Ascendant raid difficulty with extra monster-part drops.');

UPDATE difficulties
SET dropped_item_level = CASE id
    WHEN 103 THEN 15
    WHEN 104 THEN 20
    WHEN 105 THEN 25
    ELSE dropped_item_level
  END,
  unlock_level = CASE id
    WHEN 103 THEN 15
    WHEN 104 THEN 20
    WHEN 105 THEN 25
    ELSE unlock_level
  END,
  health_multiplier = CASE id
    WHEN 103 THEN 1.7
    WHEN 104 THEN 2.25
    WHEN 105 THEN 2.8
    ELSE health_multiplier
  END,
  damage_multiplier = CASE id
    WHEN 103 THEN 1.45
    WHEN 104 THEN 1.85
    WHEN 105 THEN 2.25
    ELSE damage_multiplier
  END,
  experience_multiplier = CASE id
    WHEN 103 THEN 2.4
    WHEN 104 THEN 3.5
    WHEN 105 THEN 4.5
    ELSE experience_multiplier
  END,
  description = CASE id
    WHEN 103 THEN 'Gear-only raid upgrade tier between Veteran and Mythic.'
    WHEN 104 THEN 'Mythic raid difficulty with extra monster-part drops.'
    WHEN 105 THEN 'Ascendant raid difficulty with extra monster-part drops.'
    ELSE description
  END
WHERE id IN (103, 104, 105);

DELETE FROM dungeon_difficulties WHERE dungeon_id = 2 AND difficulty_id <> 101;

INSERT OR IGNORE INTO dungeon_difficulties (dungeon_id, difficulty_id)
SELECT dungeon_id, dungeon_difficulty_id FROM generated_loot_tiers
UNION ALL
SELECT raid_id, raid_difficulty_id FROM generated_loot_tiers;

UPDATE crafting_recipes
SET source_dungeon_id = NULL,
    source_encounter_id = NULL
WHERE source_dungeon_id IN (
  SELECT dungeon_id FROM generated_loot_tiers
  UNION
  SELECT raid_id FROM generated_loot_tiers
)
OR source_encounter_id IN (
  SELECT id
  FROM encounters
  WHERE dungeon_id IN (
    SELECT dungeon_id FROM generated_loot_tiers
    UNION
    SELECT raid_id FROM generated_loot_tiers
  )
);

DELETE FROM encounter_loot_roll_items
WHERE roll_id IN (
  SELECT id
  FROM encounter_loot_rolls
  WHERE encounter_id IN (
    SELECT id
    FROM encounters
    WHERE dungeon_id IN (
      SELECT dungeon_id FROM generated_loot_tiers
      UNION
      SELECT raid_id FROM generated_loot_tiers
    )
  )
);

DELETE FROM encounter_loot_rolls
WHERE encounter_id IN (
  SELECT id
  FROM encounters
  WHERE dungeon_id IN (
    SELECT dungeon_id FROM generated_loot_tiers
    UNION
    SELECT raid_id FROM generated_loot_tiers
  )
);

DELETE FROM encounter_loot
WHERE encounter_id IN (
  SELECT id
  FROM encounters
  WHERE dungeon_id IN (
    SELECT dungeon_id FROM generated_loot_tiers
    UNION
    SELECT raid_id FROM generated_loot_tiers
  )
);

DELETE FROM encounters
WHERE dungeon_id IN (
  SELECT dungeon_id FROM generated_loot_tiers
  UNION
  SELECT raid_id FROM generated_loot_tiers
);

UPDATE encounters
SET slug = CASE id
      WHEN 100 THEN 'tigrex-raid-approach'
      WHEN 101 THEN 'tigrex-raid-guardians'
      WHEN 102 THEN 'tigrex-raid'
      WHEN 103 THEN 'rathalos-raid-approach'
      WHEN 104 THEN 'rathalos-raid-guardians'
      WHEN 105 THEN 'rathalos-raid'
      WHEN 106 THEN 'gypceros-raid-approach'
      WHEN 107 THEN 'gypceros-raid-guardians'
      WHEN 108 THEN 'gypceros-raid'
      ELSE slug
    END,
    name = CASE id
      WHEN 100 THEN 'Tigrex Approach'
      WHEN 101 THEN 'Tigrex Guardians'
      WHEN 102 THEN 'Tigrex'
      WHEN 103 THEN 'Rathalos Approach'
      WHEN 104 THEN 'Rathalos Guardians'
      WHEN 105 THEN 'Rathalos'
      WHEN 106 THEN 'Gypceros Approach'
      WHEN 107 THEN 'Gypceros Guardians'
      WHEN 108 THEN 'Gypceros'
      ELSE name
    END,
    encounter_type = CASE WHEN id IN (102, 105, 108) THEN 'boss' ELSE 'trash' END,
    description = CASE id
      WHEN 102 THEN 'Tigrex drops boss coins for item level 10 crafting.'
      WHEN 105 THEN 'Rathalos drops boss coins for item level 10 crafting.'
      WHEN 108 THEN 'Gypceros drops boss coins for item level 10 crafting.'
      ELSE 'Hunters clear the raid path.'
    END
WHERE id BETWEEN 100 AND 108
  AND NOT EXISTS (
    SELECT 1
    FROM encounters AS conflict
    WHERE conflict.id NOT BETWEEN 100 AND 108
      AND conflict.slug IN (
        'tigrex-raid-approach',
        'tigrex-raid-guardians',
        'tigrex-raid',
        'rathalos-raid-approach',
        'rathalos-raid-guardians',
        'rathalos-raid',
        'gypceros-raid-approach',
        'gypceros-raid-guardians',
        'gypceros-raid'
      )
  );

INSERT OR IGNORE INTO encounters
  (id, dungeon_id, sequence, slug, name, encounter_type, max_health, base_damage, tank_damage, party_damage, description)
SELECT
  generated_loot_tiers.dungeon_id * 100 + generated_bosses.boss_index * 3 + offset.value,
  generated_loot_tiers.dungeon_id,
  generated_bosses.boss_index * 3 + offset.value,
  generated_bosses.boss_slug || '-dungeon-' || offset.slug,
  CASE offset.encounter_type
    WHEN 'boss' THEN generated_bosses.boss_name
    ELSE generated_bosses.boss_name || ' ' || offset.name
  END,
  offset.encounter_type,
  offset.health + generated_loot_tiers.item_level * 45 + generated_bosses.boss_index * 120,
  offset.damage + generated_bosses.boss_index * 2 + generated_loot_tiers.item_level / 5,
  offset.tank_damage + generated_bosses.boss_index * 2 + generated_loot_tiers.item_level / 8,
  offset.party_damage + generated_bosses.boss_index * 3,
  CASE offset.encounter_type
    WHEN 'boss' THEN generated_bosses.boss_name || ' drops boss coins for item level ' || generated_loot_tiers.item_level || ' crafting.'
    ELSE 'Hunters clear the path before ' || generated_bosses.boss_name || '.'
  END
FROM generated_loot_tiers
JOIN generated_bosses ON generated_bosses.item_level = generated_loot_tiers.item_level
JOIN (
  SELECT 1 AS value, 'approach' AS slug, 'Approach' AS name, 'trash' AS encounter_type, 650 AS health, 15 AS damage, 9 AS tank_damage, 28 AS party_damage
  UNION ALL SELECT 2, 'guardians', 'Guardians', 'trash', 720, 16, 10, 30
  UNION ALL SELECT 3, 'boss', '', 'boss', 980, 20, 14, 34
) AS offset;

INSERT OR IGNORE INTO encounters
  (id, dungeon_id, sequence, slug, name, encounter_type, max_health, base_damage, tank_damage, party_damage, description)
SELECT
  generated_loot_tiers.raid_id * 100 + generated_bosses.boss_index * 3 + offset.value,
  generated_loot_tiers.raid_id,
  generated_bosses.boss_index * 3 + offset.value,
  generated_bosses.boss_slug || '-raid-' || offset.slug,
  CASE offset.encounter_type
    WHEN 'boss' THEN generated_bosses.boss_name
    ELSE generated_bosses.boss_name || ' ' || offset.name
  END,
  offset.encounter_type,
  offset.health + generated_loot_tiers.item_level * 45 + generated_bosses.boss_index * 120 + 900,
  offset.damage + generated_bosses.boss_index * 2 + generated_loot_tiers.item_level / 5,
  offset.tank_damage + generated_bosses.boss_index * 2 + generated_loot_tiers.item_level / 8,
  offset.party_damage + generated_bosses.boss_index * 3 + 24,
  CASE offset.encounter_type
    WHEN 'boss' THEN generated_bosses.boss_name || ' drops boss coins for item level ' || generated_loot_tiers.item_level || ' crafting.'
    ELSE 'Hunters clear the raid path before ' || generated_bosses.boss_name || '.'
  END
FROM generated_loot_tiers
JOIN generated_bosses ON generated_bosses.item_level = generated_loot_tiers.item_level
JOIN (
  SELECT 1 AS value, 'approach' AS slug, 'Approach' AS name, 'trash' AS encounter_type, 650 AS health, 15 AS damage, 9 AS tank_damage, 28 AS party_damage
  UNION ALL SELECT 2, 'guardians', 'Guardians', 'trash', 720, 16, 10, 30
  UNION ALL SELECT 3, 'boss', '', 'boss', 980, 20, 14, 34
) AS offset
WHERE generated_loot_tiers.raid_id <> 2;

INSERT OR IGNORE INTO items
  (id, slug, name, slot, rarity, item_level, healing_power, max_resource_bonus, glyph, description)
SELECT
  3000 + generated_loot_tiers.item_level * 10 + generated_bosses.boss_global_index * 10 + generated_drop_patterns.drop_index,
  generated_bosses.boss_slug || '-drop-' || generated_drop_patterns.drop_index || '-ilvl-' || generated_loot_tiers.item_level,
  generated_bosses.boss_name || ' Drop ' || generated_drop_patterns.drop_index,
  'component',
  generated_drop_patterns.rarity,
  generated_loot_tiers.item_level,
  0,
  0,
  CAST(generated_drop_patterns.drop_index AS TEXT),
  'A monster part from ' || generated_bosses.boss_name || '.'
FROM generated_loot_tiers
JOIN generated_bosses ON generated_bosses.item_level = generated_loot_tiers.item_level
JOIN generated_drop_patterns ON generated_drop_patterns.boss_index = generated_bosses.boss_index;

DELETE FROM encounter_loot WHERE encounter_id NOT IN (3, 12, 22);
DELETE FROM encounter_loot WHERE item_id IN (SELECT id FROM items WHERE slot <> 'component');

INSERT OR IGNORE INTO encounter_loot (encounter_id, item_id, difficulty_id, drop_weight, drop_chance)
SELECT
  generated_loot_tiers.dungeon_id * 100 + generated_bosses.boss_index * 3 + 3,
  3000 + generated_loot_tiers.item_level * 10 + generated_bosses.boss_global_index * 10 + generated_drop_patterns.drop_index,
  generated_loot_tiers.dungeon_difficulty_id,
  generated_drop_patterns.drop_weight,
  1.0
FROM generated_loot_tiers
JOIN generated_bosses ON generated_bosses.item_level = generated_loot_tiers.item_level
JOIN generated_drop_patterns ON generated_drop_patterns.boss_index = generated_bosses.boss_index
WHERE EXISTS (
  SELECT 1
  FROM encounters
  WHERE encounters.id = generated_loot_tiers.dungeon_id * 100 + generated_bosses.boss_index * 3 + 3
);

INSERT OR IGNORE INTO encounter_loot (encounter_id, item_id, difficulty_id, drop_weight, drop_chance)
SELECT
  CASE generated_loot_tiers.raid_id
    WHEN 2 THEN 102 + generated_bosses.boss_index * 3
    ELSE generated_loot_tiers.raid_id * 100 + generated_bosses.boss_index * 3 + 3
  END,
  3000 + generated_loot_tiers.item_level * 10 + generated_bosses.boss_global_index * 10 + generated_drop_patterns.drop_index,
  generated_loot_tiers.raid_difficulty_id,
  generated_drop_patterns.drop_weight,
  1.0
FROM generated_loot_tiers
JOIN generated_bosses ON generated_bosses.item_level = generated_loot_tiers.item_level
JOIN generated_drop_patterns ON generated_drop_patterns.boss_index = generated_bosses.boss_index
WHERE EXISTS (
  SELECT 1
  FROM encounters
  WHERE encounters.id = CASE generated_loot_tiers.raid_id
    WHEN 2 THEN 102 + generated_bosses.boss_index * 3
    ELSE generated_loot_tiers.raid_id * 100 + generated_bosses.boss_index * 3 + 3
  END
);

CREATE TEMP TABLE IF NOT EXISTS generated_recipe_offsets (
  recipe_offset INTEGER PRIMARY KEY,
  boss_index INTEGER NOT NULL
);

DELETE FROM generated_recipe_offsets;
INSERT INTO generated_recipe_offsets (recipe_offset, boss_index) VALUES
  (1, 0), (2, 0), (3, 0),
  (4, 1), (5, 1), (6, 1),
  (7, 2), (8, 2), (9, 2);

UPDATE crafting_recipes
SET source_dungeon_id = (
      SELECT generated_loot_tiers.dungeon_id
      FROM generated_loot_tiers
      WHERE crafting_recipes.id BETWEEN generated_loot_tiers.recipe_base + 1 AND generated_loot_tiers.recipe_base + 9
    ),
    source_encounter_id = (
      SELECT generated_loot_tiers.dungeon_id * 100 + generated_recipe_offsets.boss_index * 3 + 3
      FROM generated_loot_tiers
      JOIN generated_recipe_offsets
        ON crafting_recipes.id = generated_loot_tiers.recipe_base + generated_recipe_offsets.recipe_offset
    )
WHERE id BETWEEN 1101 AND 1409;

UPDATE crafting_recipes
SET source_dungeon_id = NULL,
    source_encounter_id = NULL
WHERE id BETWEEN 2001 AND 2009;

DELETE FROM crafting_recipe_components
WHERE recipe_id BETWEEN 1101 AND 1409
  OR recipe_id BETWEEN 2001 AND 2009;

INSERT OR IGNORE INTO crafting_recipe_components (recipe_id, item_id, quantity)
SELECT
  generated_loot_tiers.recipe_base + generated_recipe_offsets.recipe_offset,
  3000 + generated_loot_tiers.item_level * 10 + generated_bosses.boss_global_index * 10 + component.drop_index,
  CASE component.drop_index
    WHEN 1 THEN 5
    WHEN 2 THEN 3
    WHEN 4 THEN 1
  END
FROM generated_loot_tiers
JOIN generated_recipe_offsets
JOIN generated_bosses
  ON generated_bosses.item_level = generated_loot_tiers.item_level
  AND generated_bosses.boss_index = generated_recipe_offsets.boss_index
JOIN (
  SELECT 1 AS drop_index
  UNION ALL SELECT 2
  UNION ALL SELECT 4
) AS component;

INSERT OR IGNORE INTO crafting_recipe_components (recipe_id, item_id, quantity)
SELECT
  2000 + generated_recipe_offsets.recipe_offset,
  3000 + generated_loot_tiers.item_level * 10 + generated_bosses.boss_global_index * 10 + component.drop_index,
  CASE component.drop_index
    WHEN 1 THEN 5
    WHEN 2 THEN 3
    WHEN 4 THEN 1
  END
FROM generated_loot_tiers
JOIN generated_recipe_offsets
JOIN generated_bosses
  ON generated_bosses.item_level = generated_loot_tiers.item_level
  AND generated_bosses.boss_index = generated_recipe_offsets.boss_index
JOIN (
  SELECT 1 AS drop_index
  UNION ALL SELECT 2
  UNION ALL SELECT 4
) AS component
WHERE generated_loot_tiers.item_level = 10;

UPDATE items
SET name = CASE id
    WHEN 5 THEN 'Bulldrome Helmet'
    WHEN 2 THEN 'Bulldrome Chest'
    WHEN 6 THEN 'Bulldrome Gloves'
    WHEN 4 THEN 'Yian Kut-Ku Boots'
    WHEN 1 THEN 'Yian Kut-Ku Ring'
    WHEN 7 THEN 'Yian Kut-Ku Trinket'
    WHEN 3 THEN 'Rathian Weapon'
    WHEN 8 THEN 'Rathian Pants'
    WHEN 9 THEN 'Rathian Necklace'
    WHEN 205 THEN 'Tempered Tigrex Helmet'
    WHEN 202 THEN 'Tempered Tigrex Chest'
    WHEN 206 THEN 'Tempered Tigrex Gloves'
    WHEN 204 THEN 'Tempered Rathalos Boots'
    WHEN 201 THEN 'Tempered Rathalos Ring'
    WHEN 207 THEN 'Tempered Rathalos Trinket'
    WHEN 203 THEN 'Tempered Gypceros Weapon'
    WHEN 208 THEN 'Tempered Gypceros Pants'
    WHEN 209 THEN 'Tempered Gypceros Necklace'
    WHEN 305 THEN 'Runed Nargacuga Helmet'
    WHEN 302 THEN 'Runed Nargacuga Chest'
    WHEN 306 THEN 'Runed Nargacuga Gloves'
    WHEN 304 THEN 'Runed Azuros Boots'
    WHEN 301 THEN 'Runed Azuros Ring'
    WHEN 307 THEN 'Runed Azuros Trinket'
    WHEN 303 THEN 'Runed Diablos Weapon'
    WHEN 308 THEN 'Runed Diablos Pants'
    WHEN 309 THEN 'Runed Diablos Necklace'
    WHEN 405 THEN 'Mythic Barroth Helmet'
    WHEN 402 THEN 'Mythic Barroth Chest'
    WHEN 406 THEN 'Mythic Barroth Gloves'
    WHEN 404 THEN 'Mythic Tobi Kadachi Boots'
    WHEN 401 THEN 'Mythic Tobi Kadachi Ring'
    WHEN 407 THEN 'Mythic Tobi Kadachi Trinket'
    WHEN 403 THEN 'Mythic Monoblos Weapon'
    WHEN 408 THEN 'Mythic Monoblos Pants'
    WHEN 409 THEN 'Mythic Monoblos Necklace'
    WHEN 505 THEN 'Ascendant Anjanath Helmet'
    WHEN 502 THEN 'Ascendant Anjanath Chest'
    WHEN 506 THEN 'Ascendant Anjanath Gloves'
    WHEN 504 THEN 'Ascendant Bazelgeuse Boots'
    WHEN 501 THEN 'Ascendant Bazelgeuse Ring'
    WHEN 507 THEN 'Ascendant Bazelgeuse Trinket'
    WHEN 503 THEN 'Ascendant Odogaron Weapon'
    WHEN 508 THEN 'Ascendant Odogaron Pants'
    WHEN 509 THEN 'Ascendant Odogaron Necklace'
    WHEN 714 THEN 'Raid Tigrex Helmet'
    WHEN 711 THEN 'Raid Tigrex Chest'
    WHEN 715 THEN 'Raid Tigrex Gloves'
    WHEN 713 THEN 'Raid Rathalos Boots'
    WHEN 710 THEN 'Raid Rathalos Ring'
    WHEN 716 THEN 'Raid Rathalos Trinket'
    WHEN 712 THEN 'Raid Gypceros Weapon'
    WHEN 717 THEN 'Raid Gypceros Pants'
    WHEN 718 THEN 'Raid Gypceros Necklace'
    ELSE name
  END,
  description = CASE id
    WHEN 5 THEN 'Crafted from Bulldrome monster parts.'
    WHEN 2 THEN 'Crafted from Bulldrome monster parts.'
    WHEN 6 THEN 'Crafted from Bulldrome monster parts.'
    WHEN 4 THEN 'Crafted from Yian Kut-Ku monster parts.'
    WHEN 1 THEN 'Crafted from Yian Kut-Ku monster parts.'
    WHEN 7 THEN 'Crafted from Yian Kut-Ku monster parts.'
    WHEN 3 THEN 'Crafted from Rathian monster parts.'
    WHEN 8 THEN 'Crafted from Rathian monster parts.'
    WHEN 9 THEN 'Crafted from Rathian monster parts.'
    ELSE description
  END
WHERE id IN (
  1, 2, 3, 4, 5, 6, 7, 8, 9,
  201, 202, 203, 204, 205, 206, 207, 208, 209,
  301, 302, 303, 304, 305, 306, 307, 308, 309,
  401, 402, 403, 404, 405, 406, 407, 408, 409,
  501, 502, 503, 504, 505, 506, 507, 508, 509,
  710, 711, 712, 713, 714, 715, 716, 717, 718
);

UPDATE items
SET slug = CASE id
    WHEN 860 THEN 'bulldrome-drop-1'
    WHEN 861 THEN 'bulldrome-drop-2'
    WHEN 862 THEN 'bulldrome-drop-3'
    WHEN 863 THEN 'bulldrome-drop-4'
    WHEN 864 THEN 'yian-kut-ku-drop-1'
    WHEN 865 THEN 'yian-kut-ku-drop-2'
    WHEN 866 THEN 'yian-kut-ku-drop-3'
    WHEN 867 THEN 'yian-kut-ku-drop-4'
    WHEN 868 THEN 'rathian-drop-1'
    WHEN 869 THEN 'rathian-drop-2'
    WHEN 870 THEN 'rathian-drop-3'
    WHEN 871 THEN 'rathian-drop-4'
    ELSE slug
  END,
  name = CASE id
    WHEN 860 THEN 'Bulldrome Drop 1'
    WHEN 861 THEN 'Bulldrome Drop 2'
    WHEN 862 THEN 'Bulldrome Drop 3'
    WHEN 863 THEN 'Bulldrome Drop 4'
    WHEN 864 THEN 'Yian Kut-Ku Drop 1'
    WHEN 865 THEN 'Yian Kut-Ku Drop 2'
    WHEN 866 THEN 'Yian Kut-Ku Drop 3'
    WHEN 867 THEN 'Yian Kut-Ku Drop 4'
    WHEN 868 THEN 'Rathian Drop 1'
    WHEN 869 THEN 'Rathian Drop 2'
    WHEN 870 THEN 'Rathian Drop 3'
    WHEN 871 THEN 'Rathian Drop 4'
    ELSE name
  END,
  glyph = CASE id
    WHEN 860 THEN '1'
    WHEN 861 THEN '2'
    WHEN 862 THEN '3'
    WHEN 863 THEN '4'
    WHEN 864 THEN '1'
    WHEN 865 THEN '2'
    WHEN 866 THEN '3'
    WHEN 867 THEN '4'
    WHEN 868 THEN '1'
    WHEN 869 THEN '2'
    WHEN 870 THEN '3'
    WHEN 871 THEN '4'
    ELSE glyph
  END,
  description = CASE
    WHEN id BETWEEN 860 AND 863 THEN 'A monster part from Bulldrome.'
    WHEN id BETWEEN 864 AND 867 THEN 'A monster part from Yian Kut-Ku.'
    WHEN id BETWEEN 868 AND 871 THEN 'A monster part from Rathian.'
    ELSE description
  END
WHERE id BETWEEN 860 AND 871;

DELETE FROM dungeon_difficulties;
INSERT OR IGNORE INTO dungeon_difficulties (dungeon_id, difficulty_id) VALUES
  (1, 1),
  (1, 2),
  (1, 4),
  (1, 5),
  (3, 2),
  (6, 4),
  (8, 5),
  (2, 101),
  (7, 104),
  (9, 105);

DELETE FROM crafting_recipe_components WHERE recipe_id BETWEEN 1001 AND 1009;

INSERT OR IGNORE INTO crafting_recipe_components (recipe_id, item_id, quantity) VALUES
  (1001, 860, 5), (1001, 861, 3), (1001, 863, 1),
  (1002, 860, 5), (1002, 861, 3), (1002, 863, 1),
  (1003, 860, 5), (1003, 861, 3), (1003, 863, 1),
  (1004, 864, 5), (1004, 865, 3), (1004, 867, 1),
  (1005, 864, 5), (1005, 865, 3), (1005, 867, 1),
  (1006, 864, 5), (1006, 865, 3), (1006, 867, 1),
  (1007, 868, 5), (1007, 869, 3), (1007, 871, 1),
  (1008, 868, 5), (1008, 869, 3), (1008, 871, 1),
  (1009, 868, 5), (1009, 869, 3), (1009, 871, 1);

-- Final coin gearing override. Keep this after legacy loot edits.
UPDATE crafting_recipes
SET source_dungeon_id = CASE WHEN EXISTS (SELECT 1 FROM encounters WHERE id = 3) THEN 1 ELSE NULL END,
    source_encounter_id = (SELECT id FROM encounters WHERE id = 3)
WHERE id BETWEEN 1001 AND 1409
  AND item_id IN (SELECT id FROM items WHERE slot IN ('helmet', 'chest', 'gloves'));

UPDATE crafting_recipes
SET source_dungeon_id = CASE WHEN EXISTS (SELECT 1 FROM encounters WHERE id = 12) THEN 1 ELSE NULL END,
    source_encounter_id = (SELECT id FROM encounters WHERE id = 12)
WHERE id BETWEEN 1001 AND 1409
  AND item_id IN (SELECT id FROM items WHERE slot IN ('boots', 'ring', 'trinket'));

UPDATE crafting_recipes
SET source_dungeon_id = CASE WHEN EXISTS (SELECT 1 FROM encounters WHERE id = 22) THEN 1 ELSE NULL END,
    source_encounter_id = (SELECT id FROM encounters WHERE id = 22)
WHERE id BETWEEN 1001 AND 1409
  AND item_id IN (SELECT id FROM items WHERE slot IN ('weapon', 'pants', 'necklace'));

UPDATE crafting_recipes
SET difficulty_id = CASE
  (SELECT item_level FROM items WHERE items.id = crafting_recipes.item_id)
    WHEN 1 THEN 1
    WHEN 5 THEN 1
    WHEN 10 THEN 2
    WHEN 15 THEN 2
    WHEN 20 THEN 4
    WHEN 25 THEN 5
    ELSE difficulty_id
  END
WHERE id BETWEEN 901 AND 1409;

DELETE FROM crafting_recipe_components
WHERE recipe_id IN (
  SELECT crafting_recipes.id
  FROM crafting_recipes
  JOIN items ON items.id = crafting_recipes.item_id
  WHERE items.item_level NOT IN (1, 5, 10, 15, 20, 25)
);

DELETE FROM crafting_recipes
WHERE item_id IN (
  SELECT id FROM items WHERE item_level NOT IN (1, 5, 10, 15, 20, 25)
);

UPDATE items
SET rarity = CASE item_level
    WHEN 1 THEN 'common'
    WHEN 5 THEN 'uncommon'
    WHEN 10 THEN 'uncommon'
    WHEN 15 THEN 'rare'
    WHEN 20 THEN 'epic'
    WHEN 25 THEN 'legendary'
    ELSE rarity
  END
WHERE id IN (SELECT item_id FROM crafting_recipes);

UPDATE items
SET name = COALESCE((
    SELECT
      CASE items.item_level
        WHEN 1 THEN 'Raw '
        WHEN 5 THEN 'Honed '
        WHEN 10 THEN 'Green '
        WHEN 15 THEN 'Blue '
        WHEN 20 THEN 'Purple '
        WHEN 25 THEN 'Orange '
        ELSE ''
      END
      || encounters.name || ' '
      || CASE items.slot
        WHEN 'weapon' THEN 'Weapon'
        WHEN 'helmet' THEN 'Helmet'
        WHEN 'chest' THEN 'Chest'
        WHEN 'gloves' THEN 'Gloves'
        WHEN 'boots' THEN 'Boots'
        WHEN 'pants' THEN 'Pants'
        WHEN 'ring' THEN 'Ring'
        WHEN 'necklace' THEN 'Necklace'
        WHEN 'trinket' THEN 'Trinket'
        ELSE items.name
      END
    FROM crafting_recipes
    JOIN encounters ON encounters.id = crafting_recipes.source_encounter_id
    WHERE crafting_recipes.item_id = items.id
    LIMIT 1
  ), name),
  description = COALESCE((
    SELECT 'Crafted with ' || encounters.name || ' coins.'
    FROM crafting_recipes
    JOIN encounters ON encounters.id = crafting_recipes.source_encounter_id
    WHERE crafting_recipes.item_id = items.id
    LIMIT 1
  ), description)
WHERE id IN (SELECT item_id FROM crafting_recipes);

-- Single-boss hunting grounds. Keep this late so legacy three-boss hunt
-- seed data and generated monster tiers are normalized into separate runs.
CREATE TEMP TABLE IF NOT EXISTS single_boss_dungeons (
  dungeon_id INTEGER PRIMARY KEY,
  boss_name TEXT NOT NULL,
  boss_slug TEXT NOT NULL,
  item_level INTEGER NOT NULL,
  difficulty_floor INTEGER NOT NULL,
  location_id INTEGER NOT NULL,
  experience_reward INTEGER NOT NULL
);

DELETE FROM single_boss_dungeons;
INSERT INTO single_boss_dungeons
  (dungeon_id, boss_name, boss_slug, item_level, difficulty_floor, location_id, experience_reward)
VALUES
  (1, 'Bulldrome', 'bulldrome', 1, 1, 1, 125),
  (2, 'Yian Kut-Ku', 'yian-kut-ku', 1, 1, 1, 125),
  (3, 'Rathian', 'rathian', 1, 1, 1, 125),
  (4, 'Tigrex', 'tigrex', 10, 2, 3, 205),
  (5, 'Rathalos', 'rathalos', 10, 2, 3, 205),
  (6, 'Gypceros', 'gypceros', 10, 2, 3, 205),
  (7, 'Nargacuga', 'nargacuga', 15, 3, 3, 245),
  (8, 'Azuros', 'azuros', 15, 3, 3, 245),
  (9, 'Diablos', 'diablos', 15, 3, 3, 245),
  (10, 'Barroth', 'barroth', 20, 4, 3, 285),
  (11, 'Tobi Kadachi', 'tobi-kadachi', 20, 4, 3, 285),
  (12, 'Monoblos', 'monoblos', 20, 4, 3, 285),
  (13, 'Anjanath', 'anjanath', 25, 5, 3, 325),
  (14, 'Bazelgeuse', 'bazelgeuse', 25, 5, 3, 325),
  (15, 'Odogaron', 'odogaron', 25, 5, 3, 325);

CREATE TEMP TABLE IF NOT EXISTS single_boss_raids (
  raid_id INTEGER PRIMARY KEY,
  dungeon_id INTEGER NOT NULL,
  difficulty_id INTEGER NOT NULL,
  experience_reward INTEGER NOT NULL
);

DELETE FROM single_boss_raids;
INSERT INTO single_boss_raids (raid_id, dungeon_id, difficulty_id, experience_reward)
VALUES
  (20, 4, 101, 275),
  (21, 5, 101, 275),
  (22, 6, 101, 275),
  (23, 7, 103, 325),
  (24, 8, 103, 325),
  (25, 9, 103, 325),
  (26, 10, 104, 375),
  (27, 11, 104, 375),
  (28, 12, 104, 375),
  (29, 13, 105, 425),
  (30, 14, 105, 425),
  (31, 15, 105, 425);

UPDATE dungeons
SET slug = 'retired-dungeon-' || id
WHERE id BETWEEN 1 AND 31;

INSERT OR IGNORE INTO dungeons
  (id, location_id, slug, name, recommended_level, content_type, party_size, completion_item_level, experience_reward, description)
SELECT
  dungeon_id,
  location_id,
  boss_slug || '-hunting-ground',
  boss_name || ' Hunting Ground',
  CASE difficulty_floor WHEN 1 THEN 1 ELSE item_level END,
  'dungeon',
  6,
  NULL,
  experience_reward,
  'A focused hunt through ' || boss_name || ' territory.'
FROM single_boss_dungeons;

UPDATE dungeons
SET location_id = (SELECT location_id FROM single_boss_dungeons WHERE dungeon_id = dungeons.id),
    slug = (SELECT boss_slug || '-hunting-ground' FROM single_boss_dungeons WHERE dungeon_id = dungeons.id),
    name = (SELECT boss_name || ' Hunting Ground' FROM single_boss_dungeons WHERE dungeon_id = dungeons.id),
    recommended_level = (SELECT CASE difficulty_floor WHEN 1 THEN 1 ELSE item_level END FROM single_boss_dungeons WHERE dungeon_id = dungeons.id),
    content_type = 'dungeon',
    party_size = 6,
    completion_item_level = NULL,
    experience_reward = (SELECT experience_reward FROM single_boss_dungeons WHERE dungeon_id = dungeons.id),
    description = (SELECT 'A focused hunt through ' || boss_name || ' territory.' FROM single_boss_dungeons WHERE dungeon_id = dungeons.id)
WHERE id IN (SELECT dungeon_id FROM single_boss_dungeons);

INSERT OR IGNORE INTO dungeons
  (id, location_id, slug, name, recommended_level, content_type, party_size, completion_item_level, experience_reward, description)
SELECT
  single_boss_raids.raid_id,
  single_boss_dungeons.location_id,
  'apex-' || single_boss_dungeons.boss_slug || '-raid',
  'Apex ' || single_boss_dungeons.boss_name || ' Raid',
  single_boss_dungeons.item_level,
  'raid',
  18,
  NULL,
  single_boss_raids.experience_reward,
  'A raid-scale hunt against Apex ' || single_boss_dungeons.boss_name || '.'
FROM single_boss_raids
JOIN single_boss_dungeons ON single_boss_dungeons.dungeon_id = single_boss_raids.dungeon_id;

UPDATE dungeons
SET slug = (SELECT 'apex-' || single_boss_dungeons.boss_slug || '-raid' FROM single_boss_raids JOIN single_boss_dungeons ON single_boss_dungeons.dungeon_id = single_boss_raids.dungeon_id WHERE single_boss_raids.raid_id = dungeons.id),
    name = (SELECT 'Apex ' || single_boss_dungeons.boss_name || ' Raid' FROM single_boss_raids JOIN single_boss_dungeons ON single_boss_dungeons.dungeon_id = single_boss_raids.dungeon_id WHERE single_boss_raids.raid_id = dungeons.id),
    location_id = (SELECT single_boss_dungeons.location_id FROM single_boss_raids JOIN single_boss_dungeons ON single_boss_dungeons.dungeon_id = single_boss_raids.dungeon_id WHERE single_boss_raids.raid_id = dungeons.id),
    recommended_level = (SELECT single_boss_dungeons.item_level FROM single_boss_raids JOIN single_boss_dungeons ON single_boss_dungeons.dungeon_id = single_boss_raids.dungeon_id WHERE single_boss_raids.raid_id = dungeons.id),
    content_type = 'raid',
    party_size = 18,
    completion_item_level = NULL,
    experience_reward = (SELECT experience_reward FROM single_boss_raids WHERE single_boss_raids.raid_id = dungeons.id),
    description = (SELECT 'A raid-scale hunt against Apex ' || single_boss_dungeons.boss_name || '.' FROM single_boss_raids JOIN single_boss_dungeons ON single_boss_dungeons.dungeon_id = single_boss_raids.dungeon_id WHERE single_boss_raids.raid_id = dungeons.id)
WHERE id IN (SELECT raid_id FROM single_boss_raids);

UPDATE crafting_recipes
SET source_dungeon_id = NULL,
    source_encounter_id = NULL
WHERE id BETWEEN 1001 AND 1409;

DELETE FROM encounter_loot_roll_items
WHERE roll_id IN (
  SELECT id
  FROM encounter_loot_rolls
  WHERE encounter_id IN (
    SELECT id FROM encounters
    WHERE dungeon_id IN (SELECT dungeon_id FROM single_boss_dungeons)
       OR dungeon_id IN (SELECT raid_id FROM single_boss_raids)
  )
);

DELETE FROM encounter_loot_rolls
WHERE encounter_id IN (
  SELECT id FROM encounters
  WHERE dungeon_id IN (SELECT dungeon_id FROM single_boss_dungeons)
     OR dungeon_id IN (SELECT raid_id FROM single_boss_raids)
);

DELETE FROM encounters WHERE dungeon_id IN (SELECT dungeon_id FROM single_boss_dungeons);
DELETE FROM encounters WHERE dungeon_id IN (SELECT raid_id FROM single_boss_raids);

INSERT INTO encounters
  (id, dungeon_id, sequence, slug, name, encounter_type, max_health, base_damage, tank_damage, party_damage, description)
SELECT
  single_boss_dungeons.dungeon_id * 100 + offset.value,
  single_boss_dungeons.dungeon_id,
  offset.value,
  single_boss_dungeons.boss_slug || '-' || offset.slug,
  CASE offset.encounter_type
    WHEN 'boss' THEN single_boss_dungeons.boss_name
    ELSE single_boss_dungeons.boss_name || ' ' || offset.name
  END,
  offset.encounter_type,
  offset.health + single_boss_dungeons.item_level * 35,
  offset.damage + single_boss_dungeons.difficulty_floor,
  offset.tank_damage + single_boss_dungeons.difficulty_floor,
  offset.party_damage + single_boss_dungeons.difficulty_floor * 2,
  CASE offset.encounter_type
    WHEN 'boss' THEN single_boss_dungeons.boss_name || ' drops boss coins for crafting.'
    ELSE 'Hunters clear the path before ' || single_boss_dungeons.boss_name || '.'
  END
FROM single_boss_dungeons
JOIN (
  SELECT 1 AS value, 'approach' AS slug, 'Approach' AS name, 'trash' AS encounter_type, 430 AS health, 13 AS damage, 8 AS tank_damage, 24 AS party_damage
  UNION ALL SELECT 2, 'guardians', 'Guardians', 'trash', 520, 15, 9, 26
  UNION ALL SELECT 3, 'boss', '', 'boss', 860, 19, 14, 29
) AS offset;

INSERT INTO encounters
  (id, dungeon_id, sequence, slug, name, encounter_type, max_health, base_damage, tank_damage, party_damage, description)
SELECT
  single_boss_raids.raid_id * 100 + offset.value,
  single_boss_raids.raid_id,
  offset.value,
  single_boss_dungeons.boss_slug || '-raid-' || offset.slug,
  CASE offset.encounter_type
    WHEN 'boss' THEN 'Apex ' || single_boss_dungeons.boss_name
    ELSE 'Apex ' || single_boss_dungeons.boss_name || ' ' || offset.name
  END,
  offset.encounter_type,
  offset.health + single_boss_dungeons.item_level * 35 + 900,
  offset.damage + single_boss_dungeons.difficulty_floor,
  offset.tank_damage + single_boss_dungeons.difficulty_floor,
  offset.party_damage + single_boss_dungeons.difficulty_floor * 2 + 22,
  CASE offset.encounter_type
    WHEN 'boss' THEN 'Apex ' || single_boss_dungeons.boss_name || ' drops raid coins for crafting.'
    ELSE 'Hunters clear the raid path before Apex ' || single_boss_dungeons.boss_name || '.'
  END
FROM single_boss_raids
JOIN single_boss_dungeons
  ON single_boss_dungeons.dungeon_id = single_boss_raids.dungeon_id
JOIN (
  SELECT 1 AS value, 'approach' AS slug, 'Approach' AS name, 'trash' AS encounter_type, 650 AS health, 15 AS damage, 9 AS tank_damage, 28 AS party_damage
  UNION ALL SELECT 2, 'guardians', 'Guardians', 'trash', 720, 16, 10, 30
  UNION ALL SELECT 3, 'boss', '', 'boss', 980, 20, 14, 34
) AS offset;

DELETE FROM dungeon_difficulties;
INSERT OR IGNORE INTO dungeon_difficulties (dungeon_id, difficulty_id)
SELECT dungeon_id, difficulties.id
FROM single_boss_dungeons
JOIN difficulties ON difficulties.id BETWEEN single_boss_dungeons.difficulty_floor AND 5
WHERE difficulties.id BETWEEN 1 AND 5;

INSERT OR IGNORE INTO dungeon_difficulties (dungeon_id, difficulty_id)
SELECT raid_id, difficulty_id
FROM single_boss_raids;

CREATE TEMP TABLE IF NOT EXISTS recipe_source_override (
  recipe_id INTEGER PRIMARY KEY,
  dungeon_id INTEGER NOT NULL,
  encounter_id INTEGER NOT NULL
);

DELETE FROM recipe_source_override;
INSERT INTO recipe_source_override (recipe_id, dungeon_id, encounter_id) VALUES
  (1001, 1, 103), (1002, 1, 103), (1003, 1, 103),
  (1004, 2, 203), (1005, 2, 203), (1006, 2, 203),
  (1007, 3, 303), (1008, 3, 303), (1009, 3, 303),
  (1101, 4, 403), (1102, 4, 403), (1103, 4, 403),
  (1104, 5, 503), (1105, 5, 503), (1106, 5, 503),
  (1107, 6, 603), (1108, 6, 603), (1109, 6, 603),
  (1201, 7, 703), (1202, 7, 703), (1203, 7, 703),
  (1204, 8, 803), (1205, 8, 803), (1206, 8, 803),
  (1207, 9, 903), (1208, 9, 903), (1209, 9, 903),
  (1301, 10, 1003), (1302, 10, 1003), (1303, 10, 1003),
  (1304, 11, 1103), (1305, 11, 1103), (1306, 11, 1103),
  (1307, 12, 1203), (1308, 12, 1203), (1309, 12, 1203),
  (1401, 13, 1303), (1402, 13, 1303), (1403, 13, 1303),
  (1404, 14, 1403), (1405, 14, 1403), (1406, 14, 1403),
  (1407, 15, 1503), (1408, 15, 1503), (1409, 15, 1503);

UPDATE crafting_recipes
SET source_dungeon_id = (SELECT dungeon_id FROM recipe_source_override WHERE recipe_id = crafting_recipes.id),
    source_encounter_id = (SELECT encounter_id FROM recipe_source_override WHERE recipe_id = crafting_recipes.id)
WHERE id IN (SELECT recipe_id FROM recipe_source_override);

UPDATE crafting_recipes
SET source_dungeon_id = (
      SELECT single_boss_raids.raid_id
      FROM generated_recipe_offsets
      JOIN single_boss_raids
        ON single_boss_raids.dungeon_id = 4 + generated_recipe_offsets.boss_index
      WHERE crafting_recipes.id = 2000 + generated_recipe_offsets.recipe_offset
    ),
    source_encounter_id = (
      SELECT single_boss_raids.raid_id * 100 + 3
      FROM generated_recipe_offsets
      JOIN single_boss_raids
        ON single_boss_raids.dungeon_id = 4 + generated_recipe_offsets.boss_index
      WHERE crafting_recipes.id = 2000 + generated_recipe_offsets.recipe_offset
    ),
    difficulty_id = 101
WHERE id BETWEEN 2001 AND 2009;

UPDATE items
SET name = COALESCE((
    SELECT
      CASE items.item_level
        WHEN 1 THEN 'Raw '
        WHEN 5 THEN 'Honed '
        WHEN 10 THEN 'Green '
        WHEN 15 THEN 'Blue '
        WHEN 20 THEN 'Purple '
        WHEN 25 THEN 'Orange '
        ELSE ''
      END
      || encounters.name || ' '
      || CASE items.slot
        WHEN 'weapon' THEN 'Weapon'
        WHEN 'helmet' THEN 'Helmet'
        WHEN 'chest' THEN 'Chest'
        WHEN 'gloves' THEN 'Gloves'
        WHEN 'boots' THEN 'Boots'
        WHEN 'pants' THEN 'Pants'
        WHEN 'ring' THEN 'Ring'
        WHEN 'necklace' THEN 'Necklace'
        WHEN 'trinket' THEN 'Trinket'
        ELSE items.name
      END
    FROM crafting_recipes
    JOIN encounters ON encounters.id = crafting_recipes.source_encounter_id
    WHERE crafting_recipes.item_id = items.id
    LIMIT 1
  ), name),
  description = COALESCE((
    SELECT 'Crafted with ' || encounters.name || ' coins.'
    FROM crafting_recipes
    JOIN encounters ON encounters.id = crafting_recipes.source_encounter_id
    WHERE crafting_recipes.item_id = items.id
    LIMIT 1
  ), description)
WHERE id IN (SELECT item_id FROM crafting_recipes);

DELETE FROM coin_sources;

INSERT INTO coin_sources
SELECT
  280000 + encounters.id * 1000 + difficulties.id,
  encounters.id,
  difficulties.id,
  difficulties.dropped_item_level,
  encounters.slug || '-coin-diff-' || difficulties.id || '-ilvl-' || difficulties.dropped_item_level,
  CASE difficulties.dropped_item_level
    WHEN 1 THEN 'Raw '
    WHEN 5 THEN 'Honed '
    WHEN 10 THEN 'Green '
    WHEN 15 THEN 'Blue '
    WHEN 20 THEN 'Purple '
    WHEN 25 THEN 'Orange '
    ELSE ''
  END || encounters.name || ' Coin',
  CASE difficulties.dropped_item_level
    WHEN 1 THEN 'common'
    WHEN 5 THEN 'common'
    WHEN 10 THEN 'uncommon'
    WHEN 15 THEN 'rare'
    WHEN 20 THEN 'epic'
    WHEN 25 THEN 'legendary'
    ELSE 'common'
  END,
  '$',
  'A boss coin from ' || encounters.name || ' used for item level '
    || difficulties.dropped_item_level || ' crafting.'
FROM encounters
JOIN dungeon_difficulties ON dungeon_difficulties.dungeon_id = encounters.dungeon_id
JOIN difficulties ON difficulties.id = dungeon_difficulties.difficulty_id
WHERE encounters.encounter_type = 'boss';

INSERT OR IGNORE INTO items
  (id, slug, name, slot, rarity, item_level, healing_power, max_resource_bonus, glyph, description)
SELECT item_id, slug, name, 'component', rarity, item_level, 0, 0, glyph, description
FROM coin_sources;

UPDATE coin_sources
SET item_id = (
  SELECT items.id
  FROM items
  WHERE items.slug = coin_sources.slug
)
WHERE EXISTS (
  SELECT 1
  FROM items
  WHERE items.slug = coin_sources.slug
);

UPDATE items
SET slug = (SELECT slug FROM coin_sources WHERE coin_sources.item_id = items.id),
    name = (SELECT name FROM coin_sources WHERE coin_sources.item_id = items.id),
    slot = 'component',
    rarity = (SELECT rarity FROM coin_sources WHERE coin_sources.item_id = items.id),
    item_level = (SELECT item_level FROM coin_sources WHERE coin_sources.item_id = items.id),
    healing_power = 0,
    max_resource_bonus = 0,
    glyph = (SELECT glyph FROM coin_sources WHERE coin_sources.item_id = items.id),
    description = (SELECT description FROM coin_sources WHERE coin_sources.item_id = items.id)
WHERE id IN (SELECT item_id FROM coin_sources);

DELETE FROM encounter_loot;

INSERT INTO encounter_loot (encounter_id, item_id, difficulty_id, drop_weight, drop_chance)
SELECT encounter_id, item_id, difficulty_id, 100, 1.0
FROM coin_sources;

DELETE FROM crafting_recipe_components;

INSERT INTO crafting_recipe_components (recipe_id, item_id, quantity)
SELECT
  crafting_recipes.id,
  coin_sources.item_id,
  items.item_level
FROM crafting_recipes
JOIN items ON items.id = crafting_recipes.item_id
JOIN coin_sources
  ON coin_sources.encounter_id = crafting_recipes.source_encounter_id
  AND coin_sources.difficulty_id = crafting_recipes.difficulty_id;

DELETE FROM crafting_recipe_components
WHERE recipe_id IN (1101, 1102, 1103);

INSERT INTO crafting_recipe_components (recipe_id, item_id, quantity)
SELECT recipe_id, items.id, quantity
FROM (
  SELECT 1101 AS recipe_id, 'tigrex-boss-coin-diff-2-ilvl-10' AS item_slug, 5 AS quantity
  UNION ALL SELECT 1101, 'bulldrome-boss-coin-diff-2-ilvl-10', 5
  UNION ALL SELECT 1102, 'tigrex-boss-coin-diff-2-ilvl-10', 5
  UNION ALL SELECT 1102, 'bulldrome-boss-coin-diff-2-ilvl-10', 5
  UNION ALL SELECT 1103, 'tigrex-boss-coin-diff-2-ilvl-10', 5
  UNION ALL SELECT 1103, 'bulldrome-boss-coin-diff-2-ilvl-10', 5
) AS requirements
JOIN items ON items.slug = requirements.item_slug;

DELETE FROM gear_upgrade_paths;

INSERT INTO gear_upgrade_paths (from_item_id, to_item_id)
WITH gear_items AS (
  SELECT
    items.id,
    items.slot,
    items.item_level AS item_level
  FROM items
  WHERE items.slot <> 'component'
    AND items.item_level IN (1, 5, 10, 15, 20, 25)
),
next_levels AS (
  SELECT
    source.id AS from_item_id,
    source.slot,
    MIN(target.item_level) AS next_item_level
  FROM gear_items AS source
  JOIN gear_items AS target
    ON target.slot = source.slot
    AND target.item_level > source.item_level
  WHERE source.item_level < 25
  GROUP BY source.id, source.slot
)
SELECT from_item_id, to_item_id
FROM (
  SELECT
    next_levels.from_item_id,
    (
    SELECT target.id
    FROM gear_items AS target
    WHERE target.slot = next_levels.slot
      AND target.item_level = next_levels.next_item_level
    ORDER BY target.id
    LIMIT 1
    ) AS to_item_id
  FROM next_levels
)
WHERE to_item_id IS NOT NULL;
