namespace NeonSprawl.Server.Game.Items;

/// <summary>Fixed-size bag + equipment slot arrays for one player (NEO-54).</summary>
public sealed class PlayerInventorySnapshot
{
    public const int BagSlotCount = 24;
    public const int EquipmentSlotCount = 1;

    public required InventorySlotState[] BagSlots { get; init; }
    public required InventorySlotState[] EquipmentSlots { get; init; }

    public static PlayerInventorySnapshot Empty()
    {
        return new PlayerInventorySnapshot
        {
            BagSlots = CreateEmptySlots(BagSlotCount),
            EquipmentSlots = CreateEmptySlots(EquipmentSlotCount),
        };
    }

    public static InventorySlotState[] CreateEmptySlots(int count)
    {
        var slots = new InventorySlotState[count];
        for (var i = 0; i < count; i++)
        {
            slots[i] = new InventorySlotState(i, null, 0);
        }

        return slots;
    }

    public static InventorySlotState[] CloneSlots(InventorySlotState[] slots)
    {
        var copy = new InventorySlotState[slots.Length];
        for (var i = 0; i < slots.Length; i++)
        {
            var s = slots[i];
            copy[i] = new InventorySlotState(s.SlotIndex, s.ItemId, s.Quantity);
        }

        return copy;
    }

    public InventorySlotState[] GetSlots(InventoryContainerKind container) =>
        container == InventoryContainerKind.Equipment ? EquipmentSlots : BagSlots;

    public PlayerInventorySnapshot WithSlots(InventoryContainerKind container, InventorySlotState[] slots) =>
        container == InventoryContainerKind.Equipment
            ? new PlayerInventorySnapshot { BagSlots = BagSlots, EquipmentSlots = slots }
            : new PlayerInventorySnapshot { BagSlots = slots, EquipmentSlots = EquipmentSlots };

    public static InventoryContainerKind ContainerFromSlotKind(string inventorySlotKind) =>
        string.Equals(inventorySlotKind, "equipment", StringComparison.OrdinalIgnoreCase)
            ? InventoryContainerKind.Equipment
            : InventoryContainerKind.Bag;

    public static string ContainerKindToPersistence(InventoryContainerKind container) =>
        container == InventoryContainerKind.Equipment ? "equipment" : "bag";

    public static InventoryContainerKind ContainerKindFromPersistence(string value) =>
        string.Equals(value, "equipment", StringComparison.OrdinalIgnoreCase)
            ? InventoryContainerKind.Equipment
            : InventoryContainerKind.Bag;
}
