-- =============================================================================
-- Inventory Lua API Examples
-- =============================================================================
-- This file demonstrates how to manage entity inventories using the
-- ecs.inventory API. Inventories are stored as InventoryComponent on
-- ECS entities.
--
-- Prerequisites:
--   - Items must be registered via ecs.items.register() first
--   - Entities must have an InventoryComponent (added via ecs.add_component)
-- =============================================================================

-- =============================================================================
-- Setup: Create an entity with an inventory
-- =============================================================================

-- Create a player entity
local player = ecs.create_entity()
ecs.set_entity_name(player, "player")

-- Add an InventoryComponent with 20 slots and 50.0 max weight
ecs.set_component(player, "Inventory", {
    maxSlots = 20,
    maxWeight = 50.0,
    isContainer = false,
    containerId = ""
})

print("Created player entity with inventory (ID: " .. player .. ")")

-- =============================================================================
-- Adding Items to Inventory
-- =============================================================================

-- Add 5 health potions
local added = ecs.inventory.add(player, "potion_health", 5)
print("Added 5 health potions: " .. tostring(added))

-- Add 3 arrows
ecs.inventory.add(player, "arrow", 3)
print("Added 3 arrows")

-- Add 1 iron sword
ecs.inventory.add(player, "sword_iron", 1)
print("Added 1 iron sword")

-- Add 50 gold coins
ecs.inventory.add(player, "gold_coin", 50)
print("Added 50 gold coins")

-- =============================================================================
-- Checking Inventory Contents
-- =============================================================================

-- Check if the player has a specific item
if ecs.inventory.has(player, "potion_health") then
    print("Player has health potions")
end

-- Count how many of a specific item
local potion_count = ecs.inventory.count(player, "potion_health")
print("Health potion count: " .. potion_count)

local gold_count = ecs.inventory.count(player, "gold_coin")
print("Gold coin count: " .. gold_count)

-- =============================================================================
-- Listing Inventory Slots
-- =============================================================================

-- Get all non-empty slots
local slots = ecs.inventory.get_slots(player)
print("Inventory slots (" .. #slots .. " non-empty):")
for i, slot in ipairs(slots) do
    print("  [" .. i .. "] " .. slot.itemId .. " x" .. slot.stackSize)
end

-- =============================================================================
-- Removing Items from Inventory
-- =============================================================================

-- Remove 2 health potions
local removed = ecs.inventory.remove(player, "potion_health", 2)
print("Removed " .. removed .. " health potions")

-- Check remaining count
local remaining = ecs.inventory.count(player, "potion_health")
print("Remaining health potions: " .. remaining)

-- =============================================================================
-- Setting Inventory Slots Directly
-- =============================================================================

-- Replace the entire inventory contents
ecs.inventory.set_slots(player, {
    { itemId = "potion_health", stackSize = 3 },
    { itemId = "sword_iron", stackSize = 1 },
    { itemId = "gold_coin", stackSize = 100 }
})

print("Inventory slots replaced via set_slots")

-- Verify the new contents
local new_slots = ecs.inventory.get_slots(player)
print("New inventory slots (" .. #new_slots .. "):")
for i, slot in ipairs(new_slots) do
    print("  [" .. i .. "] " .. slot.itemId .. " x" .. slot.stackSize)
end

-- =============================================================================
-- Practical: Gold Management
-- =============================================================================

function add_gold(entity, amount)
    local current = ecs.inventory.count(entity, "gold_coin")
    ecs.inventory.add(entity, "gold_coin", amount)
    print("Gold: " .. current .. " -> " .. (current + amount))
end

function remove_gold(entity, amount)
    local current = ecs.inventory.count(entity, "gold_coin")
    if current >= amount then
        ecs.inventory.remove(entity, "gold_coin", amount)
        print("Gold: " .. current .. " -> " .. (current - amount))
        return true
    else
        print("Not enough gold! Have " .. current .. ", need " .. amount)
        return false
    end
end

add_gold(player, 50)
remove_gold(player, 30)
remove_gold(player, 200)  -- Should fail

-- =============================================================================
-- Practical: Item Transfer Between Entities
-- =============================================================================

-- Create a chest entity
local chest = ecs.create_entity()
ecs.set_entity_name(chest, "chest_wooden")
ecs.set_component(chest, "Inventory", {
    maxSlots = 10,
    maxWeight = 100.0,
    isContainer = true,
    containerId = "chest_wooden_001"
})

-- Add items to chest
ecs.inventory.add(chest, "bow_wood", 1)
ecs.inventory.add(chest, "arrow", 20)
ecs.inventory.add(chest, "potion_health", 2)

-- Transfer function: move items from one entity to another
function transfer_item(from_entity, to_entity, item_id, count)
    local available = ecs.inventory.count(from_entity, item_id)
    if available < count then
        print("Not enough " .. item_id .. " to transfer (have " .. available .. ")")
        return false
    end

    ecs.inventory.remove(from_entity, item_id, count)
    ecs.inventory.add(to_entity, item_id, count)
    print("Transferred " .. count .. " " .. item_id .. " from " ..
          ecs.get_entity_name(from_entity) .. " to " ..
          ecs.get_entity_name(to_entity))
    return true
end

-- Transfer arrows from chest to player
transfer_item(chest, player, "arrow", 10)

-- =============================================================================
-- API Reference
-- =============================================================================
--
-- ecs.inventory.add(entity_id, item_id, count) -> bool
--   Adds items to the entity's inventory. Returns true if successful.
--
-- ecs.inventory.remove(entity_id, item_id, count) -> int
--   Removes items and returns the number actually removed.
--
-- ecs.inventory.has(entity_id, item_id) -> bool
--   Returns true if the entity has at least one of the item.
--
-- ecs.inventory.count(entity_id, item_id) -> int
--   Returns the total count of the item across all slots.
--
-- ecs.inventory.get_slots(entity_id) -> table of { itemId, stackSize }
--   Returns all non-empty inventory slots.
--
-- ecs.inventory.set_slots(entity_id, slots_table) -> nil
--   Replaces the entire inventory with the given slots.
--   Each slot: { itemId = "...", stackSize = N }
-- =============================================================================

print("Inventory examples completed successfully!")
