-- WoWTetris Shop & Currency System
-- Buy classes, ultimates, and sabotage abilities

local addonName, addon = ...

-- Currency types
addon.CURRENCY = {
    COINS = "coins",           -- From quests, general gameplay
    PVP_TOKENS = "pvpTokens",  -- From PvP matches
}

-- Shop items
addon.SHOP_ITEMS = {
    -- Premium Classes (buy with coins)
    classes = {
        {id = "MONK", name = "Monk", type = "class", cost = 5000, currency = "coins", icon = "Interface\\Icons\\ClassIcon_Monk", description = "Master of chi and martial arts."},
        {id = "DEMONHUNTER", name = "Demon Hunter", type = "class", cost = 5000, currency = "coins", icon = "Interface\\Icons\\ClassIcon_DemonHunter", description = "Fel-infused vengeance incarnate."},
        {id = "EVOKER", name = "Evoker", type = "class", cost = 7500, currency = "coins", icon = "Interface\\Icons\\ClassIcon_Evoker", description = "Harness the power of dragons."},
    },
    -- Extra Ultimates per class (buy with coins)
    ultimates = {
        -- WARRIOR
        {id = "ult_war_1", name = "Colossus Smash", type = "ultimate", cost = 2000, currency = "coins", icon = "Interface\\Icons\\Ability_Warrior_ColossusSmash", description = "Clear 3x8 vertical strip!", forClass = "WARRIOR"},
        {id = "ult_war_2", name = "Thunderclap", type = "ultimate", cost = 2500, currency = "coins", icon = "Interface\\Icons\\Spell_Nature_ThunderClap", description = "Stun board: pause drops 4 sec!", forClass = "WARRIOR"},
        {id = "ult_war_3", name = "Whirlwind", type = "ultimate", cost = 3000, currency = "coins", icon = "Interface\\Icons\\Ability_Whirlwind", description = "Clear all blocks in rows 8-12!", forClass = "WARRIOR"},
        {id = "ult_war_4", name = "Odyn's Fury", type = "ultimate", cost = 4000, currency = "coins", icon = "Interface\\Icons\\INV_Sword_1H_ArtifactVigfus_D_01", description = "Lightning clears 6 random rows!", forClass = "WARRIOR"},
        -- PALADIN
        {id = "ult_pal_1", name = "Guardian of Light", type = "ultimate", cost = 2000, currency = "coins", icon = "Interface\\Icons\\Spell_Holy_GuardianSpirit", description = "Immune to garbage 12 sec!", forClass = "PALADIN"},
        {id = "ult_pal_2", name = "Holy Avenger", type = "ultimate", cost = 2500, currency = "coins", icon = "Interface\\Icons\\Spell_Holy_AvengerShield", description = "Next 5 pieces clear row on land!", forClass = "PALADIN"},
        {id = "ult_pal_3", name = "Avenging Crusader", type = "ultimate", cost = 3000, currency = "coins", icon = "Interface\\Icons\\Spell_Holy_CrusaderAura", description = "Fill bottom 6 row gaps!", forClass = "PALADIN"},
        {id = "ult_pal_4", name = "Final Reckoning", type = "ultimate", cost = 4000, currency = "coins", icon = "Interface\\Icons\\Spell_Holy_BlessingOfProtection", description = "Clear bottom 8 rows instantly!", forClass = "PALADIN"},
        -- HUNTER
        {id = "ult_hun_1", name = "Sniper Shot", type = "ultimate", cost = 2000, currency = "coins", icon = "Interface\\Icons\\Ability_Hunter_SniperShot", description = "Clear 2 most-filled rows!", forClass = "HUNTER"},
        {id = "ult_hun_2", name = "Stampede", type = "ultimate", cost = 2500, currency = "coins", icon = "Interface\\Icons\\Ability_Hunter_Stampede", description = "Beasts clear 25 random blocks!", forClass = "HUNTER"},
        {id = "ult_hun_3", name = "Volley", type = "ultimate", cost = 3000, currency = "coins", icon = "Interface\\Icons\\Ability_UpgradeMoonGlaive", description = "Arrow rain clears 4 columns!", forClass = "HUNTER"},
        {id = "ult_hun_4", name = "Wailing Arrow", type = "ultimate", cost = 4000, currency = "coins", icon = "Interface\\Icons\\Ability_Hunter_Resistanceisfutile", description = "Piercing shot clears entire column + adjacent!", forClass = "HUNTER"},
        -- ROGUE
        {id = "ult_rog_1", name = "Death from Above", type = "ultimate", cost = 2000, currency = "coins", icon = "Interface\\Icons\\Ability_Rogue_ShadowStrike", description = "Clear 4x4 at bottom center!", forClass = "ROGUE"},
        {id = "ult_rog_2", name = "Shadow Blades", type = "ultimate", cost = 2500, currency = "coins", icon = "Interface\\Icons\\Inv_Knife_1h_GarrisonVer2_B_01", description = "Next 3 pieces slice through!", forClass = "ROGUE"},
        {id = "ult_rog_3", name = "Kingsbane", type = "ultimate", cost = 3000, currency = "coins", icon = "Interface\\Icons\\Inv_Knife_1H_ArtifactKingslayers_D_01", description = "Poison destroys 30 blocks over 10 sec!", forClass = "ROGUE"},
        {id = "ult_rog_4", name = "Deathmark", type = "ultimate", cost = 4000, currency = "coins", icon = "Interface\\Icons\\Ability_Rogue_DeathMark", description = "Mark: Next clear is worth 5x points!", forClass = "ROGUE"},
        -- PRIEST
        {id = "ult_pri_1", name = "Divine Hymn", type = "ultimate", cost = 2000, currency = "coins", icon = "Interface\\Icons\\Spell_Holy_DivineHymn", description = "Fill 5 random gaps!", forClass = "PRIEST"},
        {id = "ult_pri_2", name = "Void Eruption", type = "ultimate", cost = 2500, currency = "coins", icon = "Interface\\Icons\\Spell_Priest_VoidEruption", description = "Void explodes: 5x5 clear at piece!", forClass = "PRIEST"},
        {id = "ult_pri_3", name = "Holy Word: Salvation", type = "ultimate", cost = 3000, currency = "coins", icon = "Interface\\Icons\\Ability_Priest_AngelicFeather", description = "Heal board: remove all garbage!", forClass = "PRIEST"},
        {id = "ult_pri_4", name = "Apotheosis", type = "ultimate", cost = 4000, currency = "coins", icon = "Interface\\Icons\\Spell_Holy_Apotheosis", description = "Godform: Fill all gaps in bottom 10!", forClass = "PRIEST"},
        -- DEATH KNIGHT
        {id = "ult_dk_1", name = "Summon Gargoyle", type = "ultimate", cost = 2000, currency = "coins", icon = "Interface\\Icons\\Ability_DeathKnight_SummonGargoyle", description = "Gargoyle clears 2/sec for 8 sec!", forClass = "DEATHKNIGHT"},
        {id = "ult_dk_2", name = "Army of the Dead", type = "ultimate", cost = 2500, currency = "coins", icon = "Interface\\Icons\\Spell_DeathKnight_ArmyOfTheDead", description = "Ghouls clear bottom 4 rows!", forClass = "DEATHKNIGHT"},
        {id = "ult_dk_3", name = "Frostwyrm's Fury", type = "ultimate", cost = 3000, currency = "coins", icon = "Interface\\Icons\\Achievement_Boss_Sindragosa", description = "Freeze + shatter 5 random rows!", forClass = "DEATHKNIGHT"},
        {id = "ult_dk_4", name = "Abomination Limb", type = "ultimate", cost = 4000, currency = "coins", icon = "Interface\\Icons\\Ability_Maldraxxus_DeathKnight", description = "Pull all blocks down, then clear bottom 3!", forClass = "DEATHKNIGHT"},
        -- SHAMAN
        {id = "ult_sha_1", name = "Stormkeeper", type = "ultimate", cost = 2000, currency = "coins", icon = "Interface\\Icons\\Ability_ThunderKing_LightningWhip", description = "Next 3 pieces strike with lightning!", forClass = "SHAMAN"},
        {id = "ult_sha_2", name = "Fire Elemental", type = "ultimate", cost = 2500, currency = "coins", icon = "Interface\\Icons\\Spell_Fire_Elemental_Totem", description = "Fire clears 3 blocks/sec for 10 sec!", forClass = "SHAMAN"},
        {id = "ult_sha_3", name = "Healing Tide", type = "ultimate", cost = 3000, currency = "coins", icon = "Interface\\Icons\\Spell_Nature_HealingTouch", description = "Fill 8 random gaps instantly!", forClass = "SHAMAN"},
        {id = "ult_sha_4", name = "Primordial Wave", type = "ultimate", cost = 4000, currency = "coins", icon = "Interface\\Icons\\Ability_Maldraxxus_Shaman", description = "Wave clears every 3rd row!", forClass = "SHAMAN"},
        -- MAGE
        {id = "ult_mag_1", name = "Icy Veins", type = "ultimate", cost = 2000, currency = "coins", icon = "Interface\\Icons\\Spell_Frost_ColdHearted", description = "Slow drops 50% for 15 sec!", forClass = "MAGE"},
        {id = "ult_mag_2", name = "Combustion", type = "ultimate", cost = 2500, currency = "coins", icon = "Interface\\Icons\\Spell_Fire_SealOfFire", description = "Burn: Clear 4x4 + spread to adjacent!", forClass = "MAGE"},
        {id = "ult_mag_3", name = "Arcane Power", type = "ultimate", cost = 3000, currency = "coins", icon = "Interface\\Icons\\Spell_Nature_Lightning", description = "Triple score for next 3 clears!", forClass = "MAGE"},
        {id = "ult_mag_4", name = "Meteor", type = "ultimate", cost = 4000, currency = "coins", icon = "Interface\\Icons\\Spell_Mage_Meteor", description = "METEOR: Clear 6x6 anywhere you choose!", forClass = "MAGE"},
        -- WARLOCK
        {id = "ult_wlk_1", name = "Summon Infernal", type = "ultimate", cost = 2000, currency = "coins", icon = "Interface\\Icons\\Spell_Shadow_SummonInfernal", description = "Infernal clears bottom 3 + stuns!", forClass = "WARLOCK"},
        {id = "ult_wlk_2", name = "Dark Soul", type = "ultimate", cost = 2500, currency = "coins", icon = "Interface\\Icons\\Spell_Warlock_DarkSoul", description = "Double score + ult charge for 12 sec!", forClass = "WARLOCK"},
        {id = "ult_wlk_3", name = "Summon Doomguard", type = "ultimate", cost = 3000, currency = "coins", icon = "Interface\\Icons\\Spell_Shadow_SummonDoomguard", description = "Doom clears 15 blocks + 4 rows!", forClass = "WARLOCK"},
        {id = "ult_wlk_4", name = "Ritual of Doom", type = "ultimate", cost = 4000, currency = "coins", icon = "Interface\\Icons\\Spell_Shadow_AntiMagicShell", description = "Sacrifice: Clear ALL but top 4 rows!", forClass = "WARLOCK"},
        -- DRUID
        {id = "ult_dru_1", name = "Incarnation", type = "ultimate", cost = 2000, currency = "coins", icon = "Interface\\Icons\\Spell_Druid_Incarnation", description = "Bear form: Immune + clear 3 rows!", forClass = "DRUID"},
        {id = "ult_dru_2", name = "Convoke Spirits", type = "ultimate", cost = 2500, currency = "coins", icon = "Interface\\Icons\\Ability_Ardenweald_Druid", description = "8 random effects: clears, fills, bonus!", forClass = "DRUID"},
        {id = "ult_dru_3", name = "Celestial Alignment", type = "ultimate", cost = 3000, currency = "coins", icon = "Interface\\Icons\\Spell_Nature_NatureBlessing", description = "Sun + Moon: Clear 2 columns each side!", forClass = "DRUID"},
        {id = "ult_dru_4", name = "Heart of the Wild", type = "ultimate", cost = 4000, currency = "coins", icon = "Interface\\Icons\\Spell_Holy_SymbolOfHope", description = "Nature heals: Fill ALL gaps in bottom 8!", forClass = "DRUID"},
        -- MONK
        {id = "ult_mon_1", name = "Invoke Xuen", type = "ultimate", cost = 2000, currency = "coins", icon = "Interface\\Icons\\Ability_Monk_SummonTigerStatue", description = "Tiger clears 3/sec for 8 sec!", forClass = "MONK"},
        {id = "ult_mon_2", name = "Thunder Focus Tea", type = "ultimate", cost = 2500, currency = "coins", icon = "Interface\\Icons\\Ability_Monk_ThunderFocusTea", description = "Next ultimate is FREE + instant!", forClass = "MONK"},
        {id = "ult_mon_3", name = "Weapons of Order", type = "ultimate", cost = 3000, currency = "coins", icon = "Interface\\Icons\\Ability_Bastion_Monk", description = "Order: Clear 5 rows in sequence!", forClass = "MONK"},
        {id = "ult_mon_4", name = "Invoke Yu'lon", type = "ultimate", cost = 4000, currency = "coins", icon = "Interface\\Icons\\Ability_Monk_ChiJi", description = "Jade dragon: Fill gaps + clear 4 rows!", forClass = "MONK"},
        -- DEMON HUNTER
        {id = "ult_dh_1", name = "The Hunt", type = "ultimate", cost = 2000, currency = "coins", icon = "Interface\\Icons\\Ability_Ardenweald_DemonHunter", description = "Hunt: Clear row at each board edge!", forClass = "DEMONHUNTER"},
        {id = "ult_dh_2", name = "Fel Devastation", type = "ultimate", cost = 2500, currency = "coins", icon = "Interface\\Icons\\Ability_DemonHunter_FelDevastation", description = "Beam: Clear 3 center columns!", forClass = "DEMONHUNTER"},
        {id = "ult_dh_3", name = "Darkness", type = "ultimate", cost = 3000, currency = "coins", icon = "Interface\\Icons\\Ability_DemonHunter_Darkness", description = "Immune: No blocks land for 5 sec!", forClass = "DEMONHUNTER"},
        {id = "ult_dh_4", name = "Demonic", type = "ultimate", cost = 4000, currency = "coins", icon = "Interface\\Icons\\Ability_DemonHunter_Demonic", description = "Transform: 6x6 clear + speed boost 15 sec!", forClass = "DEMONHUNTER"},
        -- EVOKER
        {id = "ult_evo_1", name = "Fire Breath", type = "ultimate", cost = 2000, currency = "coins", icon = "Interface\\Icons\\Ability_Evoker_FireBreath", description = "Cone of fire: Clear 5 rows!", forClass = "EVOKER"},
        {id = "ult_evo_2", name = "Dream Breath", type = "ultimate", cost = 2500, currency = "coins", icon = "Interface\\Icons\\Ability_Evoker_DreamBreath", description = "Heal: Fill 10 gaps instantly!", forClass = "EVOKER"},
        {id = "ult_evo_3", name = "Dragonrage", type = "ultimate", cost = 3000, currency = "coins", icon = "Interface\\Icons\\Ability_Evoker_DragonRage", description = "Rage: Double ult charge for 20 sec!", forClass = "EVOKER"},
        {id = "ult_evo_4", name = "Emerald Communion", type = "ultimate", cost = 4000, currency = "coins", icon = "Interface\\Icons\\Ability_Evoker_GreenSpellCast", description = "Emerald: Pause time + fill ALL gaps!", forClass = "EVOKER"},
    },
    -- Extra Sabotage (buy with PvP tokens)
    sabotage = {
        -- Universal sabotages
        {id = "sab_megafreeze", name = "Mega Freeze", type = "sabotage", cost = 500, currency = "pvpTokens", icon = "Interface\\Icons\\Spell_Frost_Glacier", description = "Freeze opponent for 6 seconds!", forClass = "ALL"},
        {id = "sab_earthquake", name = "Earthquake", type = "sabotage", cost = 750, currency = "pvpTokens", icon = "Interface\\Icons\\Spell_Nature_Earthquake", description = "Shake + 3 garbage rows!", forClass = "ALL"},
        {id = "sab_totalblackout", name = "Total Blackout", type = "sabotage", cost = 1000, currency = "pvpTokens", icon = "Interface\\Icons\\Spell_Shadow_Detectlesserinvisibility", description = "Black out + hide preview for 8 sec!", forClass = "ALL"},
        -- WARRIOR
        {id = "sab_war_1", name = "Intimidating Shout", type = "sabotage", cost = 400, currency = "pvpTokens", icon = "Interface\\Icons\\Ability_GolemThunderClap", description = "Opponent's controls reversed 8 sec!", forClass = "WARRIOR"},
        {id = "sab_war_2", name = "Shattering Throw", type = "sabotage", cost = 600, currency = "pvpTokens", icon = "Interface\\Icons\\Ability_Warrior_ShatteringThrow", description = "Destroy opponent's held piece!", forClass = "WARRIOR"},
        -- PALADIN
        {id = "sab_pal_1", name = "Blinding Light", type = "sabotage", cost = 400, currency = "pvpTokens", icon = "Interface\\Icons\\Ability_Paladin_BlindingLight", description = "White flash hides opponent board 5 sec!", forClass = "PALADIN"},
        {id = "sab_pal_2", name = "Divine Toll", type = "sabotage", cost = 600, currency = "pvpTokens", icon = "Interface\\Icons\\Ability_Bastion_Paladin", description = "3 random garbage rows!", forClass = "PALADIN"},
        -- HUNTER
        {id = "sab_hun_1", name = "Scatter Shot", type = "sabotage", cost = 400, currency = "pvpTokens", icon = "Interface\\Icons\\Ability_GolemStormBolt", description = "Scramble opponent's preview!", forClass = "HUNTER"},
        {id = "sab_hun_2", name = "Binding Shot", type = "sabotage", cost = 600, currency = "pvpTokens", icon = "Interface\\Icons\\Ability_Hunter_BindingShot", description = "Slow opponent speed 50% for 10 sec!", forClass = "HUNTER"},
        -- ROGUE
        {id = "sab_rog_1", name = "Blind", type = "sabotage", cost = 400, currency = "pvpTokens", icon = "Interface\\Icons\\Spell_Shadow_MindSteal", description = "Hide opponent's current piece 6 sec!", forClass = "ROGUE"},
        {id = "sab_rog_2", name = "Smoke Bomb", type = "sabotage", cost = 600, currency = "pvpTokens", icon = "Interface\\Icons\\Ability_Rogue_SmokeBomb", description = "Fog of war: opponent sees only current row!", forClass = "ROGUE"},
        -- PRIEST
        {id = "sab_pri_1", name = "Psychic Scream", type = "sabotage", cost = 400, currency = "pvpTokens", icon = "Interface\\Icons\\Spell_Shadow_PsychicScream", description = "Force opponent hard drop!", forClass = "PRIEST"},
        {id = "sab_pri_2", name = "Mind Control", type = "sabotage", cost = 600, currency = "pvpTokens", icon = "Interface\\Icons\\Spell_Shadow_ShadowWordDominate", description = "Control opponent: random moves 5 sec!", forClass = "PRIEST"},
        -- DEATHKNIGHT
        {id = "sab_dk_1", name = "Chains of Ice", type = "sabotage", cost = 400, currency = "pvpTokens", icon = "Interface\\Icons\\Spell_Frost_ChainsOfIce", description = "Slow opponent speed 75% for 6 sec!", forClass = "DEATHKNIGHT"},
        {id = "sab_dk_2", name = "Death Grip", type = "sabotage", cost = 600, currency = "pvpTokens", icon = "Interface\\Icons\\Spell_DeathKnight_Strangulate", description = "Pull up opponent's bottom 2 rows!", forClass = "DEATHKNIGHT"},
        -- SHAMAN
        {id = "sab_sha_1", name = "Hex", type = "sabotage", cost = 400, currency = "pvpTokens", icon = "Interface\\Icons\\Spell_Shaman_Hex", description = "Transform opponent's next 3 pieces to O!", forClass = "SHAMAN"},
        {id = "sab_sha_2", name = "Thunderstorm", type = "sabotage", cost = 600, currency = "pvpTokens", icon = "Interface\\Icons\\Spell_Nature_Thunderstorm", description = "Push opponent blocks up 2 rows!", forClass = "SHAMAN"},
        -- MAGE
        {id = "sab_mag_1", name = "Polymorph", type = "sabotage", cost = 400, currency = "pvpTokens", icon = "Interface\\Icons\\Spell_Nature_Polymorph", description = "Turn opponent's I-pieces into S/Z!", forClass = "MAGE"},
        {id = "sab_mag_2", name = "Dragon's Breath", type = "sabotage", cost = 600, currency = "pvpTokens", icon = "Interface\\Icons\\Inv_Misc_Head_Dragon_01", description = "Disorient: swap opponent controls 8 sec!", forClass = "MAGE"},
        -- WARLOCK
        {id = "sab_wlk_1", name = "Fear", type = "sabotage", cost = 400, currency = "pvpTokens", icon = "Interface\\Icons\\Spell_Shadow_Possession", description = "Random movements for 6 sec!", forClass = "WARLOCK"},
        {id = "sab_wlk_2", name = "Howl of Terror", type = "sabotage", cost = 600, currency = "pvpTokens", icon = "Interface\\Icons\\Spell_Shadow_DeathScream", description = "Drop 5 random garbage blocks!", forClass = "WARLOCK"},
        -- DRUID
        {id = "sab_dru_1", name = "Cyclone", type = "sabotage", cost = 400, currency = "pvpTokens", icon = "Interface\\Icons\\Spell_Nature_Cyclone", description = "Spin opponent's board 180 degrees!", forClass = "DRUID"},
        {id = "sab_dru_2", name = "Entangling Roots", type = "sabotage", cost = 600, currency = "pvpTokens", icon = "Interface\\Icons\\Spell_Nature_StrangleVines", description = "Root: Opponent can't rotate 8 sec!", forClass = "DRUID"},
        -- MONK
        {id = "sab_mon_1", name = "Paralysis", type = "sabotage", cost = 400, currency = "pvpTokens", icon = "Interface\\Icons\\Ability_Monk_Paralysis", description = "Stun opponent 4 sec!", forClass = "MONK"},
        {id = "sab_mon_2", name = "Leg Sweep", type = "sabotage", cost = 600, currency = "pvpTokens", icon = "Interface\\Icons\\Ability_Monk_LegSweep", description = "Knock down opponent's bottom row!", forClass = "MONK"},
        -- DEMON HUNTER
        {id = "sab_dh_1", name = "Imprison", type = "sabotage", cost = 400, currency = "pvpTokens", icon = "Interface\\Icons\\Ability_DemonHunter_Imprison", description = "Trap opponent in cage 5 sec!", forClass = "DEMONHUNTER"},
        {id = "sab_dh_2", name = "Chaos Nova", type = "sabotage", cost = 600, currency = "pvpTokens", icon = "Interface\\Icons\\Ability_DemonHunter_ChaosNova", description = "Chaos: scramble opponent's board!", forClass = "DEMONHUNTER"},
        -- EVOKER
        {id = "sab_evo_1", name = "Sleep Walk", type = "sabotage", cost = 400, currency = "pvpTokens", icon = "Interface\\Icons\\Ability_Evoker_SleepWalk", description = "Opponent moves randomly 6 sec!", forClass = "EVOKER"},
        {id = "sab_evo_2", name = "Landslide", type = "sabotage", cost = 600, currency = "pvpTokens", icon = "Interface\\Icons\\Ability_Evoker_Landslide", description = "Add 4 garbage rows with random gaps!", forClass = "EVOKER"},
    },
}

-- Currency rewards
addon.CURRENCY_REWARDS = {
    quest_complete = 50,      -- Coins per quest
    pvp_win = 100,            -- Coins for winning
    pvp_loss = 25,            -- Coins for losing
    pvp_win_tokens = 50,      -- PvP tokens for winning
    pvp_loss_tokens = 25,     -- PvP tokens for losing
    tetris = 25,              -- Coins per Tetris (4-line clear)
    high_score = 100,         -- Bonus for new high score
    level_up = 150,           -- Coins per class level up (casual mode)
}

-- Initialize shop data
function addon:InitializeShop()
    if not addon.db.shop then
        addon.db.shop = {
            coins = 0,
            pvpTokens = 0,
            ownedClasses = {},    -- {"MONK", "DEMONHUNTER"}
            ownedUltimates = {},  -- {"ult_timewarp"}
            ownedSabotage = {},   -- {"sab_megafreeze"}
        }
    end
    addon.shopData = addon.db.shop
end

-- Get currency amount
function addon:GetCurrency(currencyType)
    if not addon.shopData then addon:InitializeShop() end
    if currencyType == "coins" then
        return addon.shopData.coins or 0
    elseif currencyType == "pvpTokens" then
        return addon.shopData.pvpTokens or 0
    end
    return 0
end

-- Add currency
function addon:AddCurrency(currencyType, amount)
    if not addon.shopData then addon:InitializeShop() end
    if currencyType == "coins" then
        addon.shopData.coins = (addon.shopData.coins or 0) + amount
        print("|cffffd700[Tetris]|r +" .. amount .. " |cffffd700Coins|r (Total: " .. addon.shopData.coins .. ")")
    elseif currencyType == "pvpTokens" then
        addon.shopData.pvpTokens = (addon.shopData.pvpTokens or 0) + amount
        print("|cffff0000[Tetris]|r +" .. amount .. " |cffff0000PvP Tokens|r (Total: " .. addon.shopData.pvpTokens .. ")")
    end
    addon:SaveShopData()
end

-- Spend currency
function addon:SpendCurrency(currencyType, amount)
    if not addon.shopData then addon:InitializeShop() end
    local current = addon:GetCurrency(currencyType)
    if current < amount then return false end
    
    if currencyType == "coins" then
        addon.shopData.coins = addon.shopData.coins - amount
    elseif currencyType == "pvpTokens" then
        addon.shopData.pvpTokens = addon.shopData.pvpTokens - amount
    end
    addon:SaveShopData()
    return true
end

-- Save shop data
function addon:SaveShopData()
    addon.db.shop = addon.shopData
end

-- Check if item is owned
function addon:OwnsItem(itemId, itemType)
    if not addon.shopData then addon:InitializeShop() end
    
    if itemType == "class" then
        for _, c in ipairs(addon.shopData.ownedClasses or {}) do
            if c == itemId then return true end
        end
    elseif itemType == "ultimate" then
        for _, u in ipairs(addon.shopData.ownedUltimates or {}) do
            if u == itemId then return true end
        end
    elseif itemType == "sabotage" then
        for _, s in ipairs(addon.shopData.ownedSabotage or {}) do
            if s == itemId then return true end
        end
    end
    return false
end

-- Purchase item
function addon:PurchaseItem(item)
    if not addon.shopData then addon:InitializeShop() end
    
    -- Check if already owned
    if addon:OwnsItem(item.id, item.type) then
        print("|cffff0000[Tetris]|r You already own " .. item.name .. "!")
        return false
    end
    
    -- Check currency
    if not addon:SpendCurrency(item.currency, item.cost) then
        print("|cffff0000[Tetris]|r Not enough " .. (item.currency == "coins" and "Coins" or "PvP Tokens") .. "!")
        return false
    end
    
    -- Add to owned
    if item.type == "class" then
        table.insert(addon.shopData.ownedClasses, item.id)
    elseif item.type == "ultimate" then
        table.insert(addon.shopData.ownedUltimates, item.id)
    elseif item.type == "sabotage" then
        table.insert(addon.shopData.ownedSabotage, item.id)
    end
    
    addon:SaveShopData()
    print("|cff00ff00[Tetris]|r Purchased " .. item.name .. "!")
    return true
end

-- Check if class is unlocked (base classes always unlocked)
function addon:IsClassUnlocked(classId)
    -- Base classes are always unlocked
    for _, c in ipairs(addon.CLASS_ORDER) do
        if c == classId then return true end
    end
    
    -- Check if premium class is owned
    return addon:OwnsItem(classId, "class")
end

-- Award currency for various actions
function addon:AwardCurrencyForQuest()
    addon:AddCurrency("coins", addon.CURRENCY_REWARDS.quest_complete)
end

function addon:AwardCurrencyForPvPWin()
    addon:AddCurrency("coins", addon.CURRENCY_REWARDS.pvp_win)
    addon:AddCurrency("pvpTokens", addon.CURRENCY_REWARDS.pvp_win_tokens)
end

function addon:AwardCurrencyForPvPLoss()
    addon:AddCurrency("coins", addon.CURRENCY_REWARDS.pvp_loss)
    addon:AddCurrency("pvpTokens", addon.CURRENCY_REWARDS.pvp_loss_tokens)
end

function addon:AwardCurrencyForTetris()
    addon:AddCurrency("coins", addon.CURRENCY_REWARDS.tetris)
end

function addon:AwardCurrencyForHighScore()
    addon:AddCurrency("coins", addon.CURRENCY_REWARDS.high_score)
end

function addon:AwardCurrencyForLevelUp()
    addon:AddCurrency("coins", addon.CURRENCY_REWARDS.level_up)
end
