using Theriapolis.Core.Data;
using Theriapolis.Core.Rules.Stats;

namespace Theriapolis.Core.Items;

/// <summary>
/// Body-size compatibility check for equipment. Per equipment.md:
///   "Most equipment comes in Small, Medium, and Large variants. Using
///    equipment not sized for your body imposes disadvantage on relevant
///    checks unless it has the Adaptive property."
///
/// Returns <see cref="MatchResult.Match"/> when the item lists the wearer's
/// size, <see cref="MatchResult.Adaptive"/> when not listed but the item has
/// the "adaptive" property (no penalty), and
/// <see cref="MatchResult.WrongSize"/> otherwise (wearer takes disadvantage).
/// </summary>
public static class SizeMatch
{
    public enum MatchResult : byte
    {
        Match     = 0,  // item explicitly fits the wearer's size
        Adaptive  = 1,  // item is universally adaptive — no disadvantage
        WrongSize = 2,  // wearer can equip it but suffers disadvantage
    }

    public static MatchResult Check(ItemDef def, SizeCategory wearerSize)
    {
        string wearerKey = wearerSize switch
        {
            SizeCategory.Tiny        => "tiny",
            SizeCategory.Small       => "small",
            SizeCategory.Medium      => "medium",
            SizeCategory.MediumLarge => "medium",   // M-Large uses Medium-sized gear
            SizeCategory.Large       => "large",
            SizeCategory.Huge        => "large",    // closest available
            _ => "medium",
        };

        foreach (var s in def.Sizes)
            if (string.Equals(s, wearerKey, StringComparison.OrdinalIgnoreCase))
                return MatchResult.Match;

        foreach (var p in def.Properties)
            if (string.Equals(p, "adaptive", StringComparison.OrdinalIgnoreCase))
                return MatchResult.Adaptive;

        return MatchResult.WrongSize;
    }
}
