using Theriapolis.Core.Data;

namespace Theriapolis.Core.Items;

/// <summary>
/// One stack of items in an <see cref="Inventory"/>. Holds a reference to the
/// immutable <see cref="ItemDef"/> plus per-instance state: how many in the
/// stack, current condition, and (optionally) the slot the item is equipped
/// into.
///
/// Phase 5 ships condition as a no-op (always 100); it's reserved for damage,
/// repairs, and weapon breaking that arrive in Phase 5.5+.
/// </summary>
public sealed class ItemInstance
{
    public ItemDef Def { get; }
    public int     Qty { get; set; }

    /// <summary>Condition, 0..100. 100 = pristine. Phase 5 always uses 100.</summary>
    public int Condition { get; set; } = 100;

    /// <summary>Null while in the inventory bag; set when the item is equipped.</summary>
    public EquipSlot? EquippedAt { get; set; }

    public ItemInstance(ItemDef def, int qty = 1)
    {
        Def = def ?? throw new ArgumentNullException(nameof(def));
        if (qty < 1) throw new ArgumentOutOfRangeException(nameof(qty), "qty must be ≥ 1");
        Qty = qty;
    }

    public float TotalWeightLb => Def.WeightLb * Qty;

    public override string ToString() =>
        EquippedAt is null
            ? $"{Def.Name}{(Qty > 1 ? $" ×{Qty}" : "")}"
            : $"{Def.Name} (equipped: {EquippedAt})";
}
