@namespace Lumeo

<div class="@CssClass" @attributes="AdditionalAttributes">
    <div class="@InnerClasses">
        @ChildContent
    </div>
    @if (Status != AvatarStatus.None)
    {
        <span class="absolute bottom-0 right-0 block h-2.5 w-2.5 rounded-full ring-2 ring-background @StatusColor"></span>
    }
</div>

@code {
    [Parameter] public RenderFragment? ChildContent { get; set; }
    [Parameter] public AvatarSize Size { get; set; } = AvatarSize.Default;
    [Parameter] public AvatarShape Shape { get; set; } = AvatarShape.Circle;
    [Parameter] public AvatarStatus Status { get; set; } = AvatarStatus.None;
    [Parameter] public string? Class { get; set; }
    [Parameter(CaptureUnmatchedValues = true)] public Dictionary<string, object>? AdditionalAttributes { get; set; }

    // Outer wrapper inherits ShapeClasses too so consumer-supplied borders / rings
    // (e.g. border-2 border-background in an AvatarGroup) follow the avatar's
    // shape. Without this, the border landed on the rectangular outer div while
    // the inner div clipped the image to a circle — visible as square frames
    // around fallback initials.
    private string CssClass => $"relative flex shrink-0 {SizeClasses} {ShapeClasses} {Class}".Trim();

    private string InnerClasses => $"flex h-full w-full overflow-hidden {ShapeClasses}";

    private string ShapeClasses => Shape switch
    {
        AvatarShape.Square => "rounded-md",
        _ => "rounded-full"
    };

    private string SizeClasses => Size switch
    {
        AvatarSize.Sm => "h-8 w-8",
        AvatarSize.Default => "h-10 w-10",
        AvatarSize.Lg => "h-12 w-12",
        _ => "h-10 w-10"
    };

    private string StatusColor => Status switch
    {
        AvatarStatus.Online => "bg-success",
        AvatarStatus.Offline => "bg-muted-foreground",
        AvatarStatus.Away => "bg-warning",
        AvatarStatus.Busy => "bg-destructive",
        _ => ""
    };

    public enum AvatarSize { Sm, Default, Lg }
    public enum AvatarShape { Circle, Square }
    public enum AvatarStatus { None, Online, Offline, Away, Busy }
}
