-- 000011: Achievements, Daily/Weekly Tasks, and Shared World foundation.

-- Hero stat tracking columns for achievement conditions.
ALTER TABLE heroes ADD COLUMN IF NOT EXISTS total_kills INT NOT NULL DEFAULT 0;
ALTER TABLE heroes ADD COLUMN IF NOT EXISTS elite_kills INT NOT NULL DEFAULT 0;
ALTER TABLE heroes ADD COLUMN IF NOT EXISTS total_deaths INT NOT NULL DEFAULT 0;
ALTER TABLE heroes ADD COLUMN IF NOT EXISTS kills_since_death INT NOT NULL DEFAULT 0;
ALTER TABLE heroes ADD COLUMN IF NOT EXISTS legendary_drops INT NOT NULL DEFAULT 0;

-- Shared world: track hero online status for nearby-heroes queries.
ALTER TABLE heroes ADD COLUMN IF NOT EXISTS last_online_at TIMESTAMPTZ;
CREATE INDEX IF NOT EXISTS idx_heroes_online ON heroes(last_online_at) WHERE last_online_at IS NOT NULL;

-- ============================================================
-- Achievements
-- ============================================================

CREATE TABLE IF NOT EXISTS achievements (
    id TEXT PRIMARY KEY,
    title TEXT NOT NULL,
    description TEXT NOT NULL,
    condition_type TEXT NOT NULL, -- 'level', 'kills', 'gold', 'elite_kills', 'deaths', 'loot_legendary', 'kills_no_death'
    condition_value INT NOT NULL DEFAULT 0,
    reward_type TEXT NOT NULL DEFAULT 'gold', -- 'gold', 'potion', 'title'
    reward_amount INT NOT NULL DEFAULT 0
);

CREATE TABLE IF NOT EXISTS hero_achievements (
    hero_id BIGINT NOT NULL REFERENCES heroes(id) ON DELETE CASCADE,
    achievement_id TEXT NOT NULL REFERENCES achievements(id),
    unlocked_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    PRIMARY KEY (hero_id, achievement_id)
);

-- Seed achievements.
INSERT INTO achievements (id, title, description, condition_type, condition_value, reward_type, reward_amount) VALUES
    ('first_blood', 'First Blood', 'Defeat your first enemy', 'kills', 1, 'gold', 50),
    ('warrior', 'Warrior', 'Reach level 50', 'level', 50, 'gold', 5000),
    ('legend', 'Legend', 'Reach level 100', 'level', 100, 'gold', 50000),
    ('hunter', 'Hunter', 'Defeat 100 enemies', 'kills', 100, 'gold', 500),
    ('slayer', 'Slayer', 'Defeat 1000 enemies', 'kills', 1000, 'gold', 5000),
    ('rich', 'Rich', 'Accumulate 10000 gold', 'gold', 10000, 'gold', 1000),
    ('lucky', 'Lucky', 'Find a Legendary item', 'loot_legendary', 1, 'potion', 5),
    ('undying', 'Undying', 'Defeat 50 enemies without dying', 'kills_no_death', 50, 'gold', 2000),
    ('elite_hunter', 'Elite Hunter', 'Defeat 10 elite enemies', 'elite_kills', 10, 'gold', 3000)
ON CONFLICT DO NOTHING;

-- ============================================================
-- Daily / Weekly Tasks
-- ============================================================

CREATE TABLE IF NOT EXISTS daily_tasks (
    id TEXT PRIMARY KEY,
    title TEXT NOT NULL,
    description TEXT NOT NULL,
    objective_type TEXT NOT NULL, -- 'kill_count', 'elite_kill', 'collect_gold', 'use_buff', 'reach_level'
    objective_count INT NOT NULL,
    reward_type TEXT NOT NULL DEFAULT 'gold',
    reward_amount INT NOT NULL DEFAULT 0,
    period TEXT NOT NULL DEFAULT 'daily' -- 'daily', 'weekly'
);

CREATE TABLE IF NOT EXISTS hero_daily_tasks (
    hero_id BIGINT NOT NULL REFERENCES heroes(id) ON DELETE CASCADE,
    task_id TEXT NOT NULL REFERENCES daily_tasks(id),
    progress INT NOT NULL DEFAULT 0,
    completed BOOLEAN NOT NULL DEFAULT false,
    claimed BOOLEAN NOT NULL DEFAULT false,
    period_start TIMESTAMPTZ NOT NULL,
    PRIMARY KEY (hero_id, task_id, period_start)
);

-- Seed daily tasks.
INSERT INTO daily_tasks VALUES
    ('daily_kill_10', 'Monster Slayer', 'Kill 10 enemies', 'kill_count', 10, 'gold', 100, 'daily'),
    ('daily_elite', 'Elite Hunter', 'Defeat an Elite enemy', 'elite_kill', 1, 'gold', 200, 'daily'),
    ('daily_gold_500', 'Gold Rush', 'Collect 500 Gold', 'collect_gold', 500, 'potion', 2, 'daily'),
    ('daily_buff_3', 'Buff Master', 'Use 3 Buffs', 'use_buff', 3, 'gold', 150, 'daily'),
    ('weekly_kill_100', 'Weekly Warrior', 'Kill 100 enemies', 'kill_count', 100, 'gold', 1000, 'weekly'),
    ('weekly_elite_5', 'Elite Slayer', 'Defeat 5 Elites', 'elite_kill', 5, 'gold', 2000, 'weekly'),
    ('weekly_gold_5000', 'Wealthy', 'Collect 5000 Gold', 'collect_gold', 5000, 'potion', 5, 'weekly')
ON CONFLICT DO NOTHING;
