@using AliasVault.Client.Main.Models
@using AliasClientDb.Models
@using ItemTypeClass = AliasClientDb.Models.ItemType

@* ItemIcon component - displays contextually appropriate icons based on item type *@
@* Uses centralized ItemTypeIcons for SVG definitions (auto-generated from core/models) *@
@* For Login/Alias: Uses the LogoDataUri if available, falls back to key placeholder *@
@* For CreditCard: Shows card brand icons (Visa, MC, Amex, Discover) based on card number *@
@* For Note: Shows a document/note icon *@

@if (ItemType == ItemTypeClass.Note)
{
    @* Note icon - using centralized definition *@
    <span class="@SizeClass flex-shrink-0 inline-block [&>svg]:w-full [&>svg]:h-full">@((MarkupString)ItemTypeIcons.Note)</span>
}
else if (ItemType == ItemTypeClass.CreditCard)
{
    @* Credit card icon - detect brand and show appropriate icon from centralized definitions *@
    <span class="@SizeClass flex-shrink-0 inline-block [&>svg]:w-full [&>svg]:h-full">@((MarkupString)GetCardIconSvg())</span>
}
else if (LogoDataUri is { Length: > 0 })
{
    @* Login/Alias with logo *@
    <img src="@LogoDataUri" alt="@AltText" class="@SizeClass flex-shrink-0 rounded-lg" loading="lazy" decoding="async" @onerror="OnImageError" />
}
else
{
    @* Default placeholder - key icon for Login/Alias without logo *@
    <span class="@SizeClass flex-shrink-0 inline-block [&>svg]:w-full [&>svg]:h-full">@((MarkupString)ItemTypeIcons.Placeholder)</span>
}

@code {
    /// <summary>
    /// Gets or sets the item type (Login, Alias, CreditCard, Note).
    /// </summary>
    [Parameter]
    public string ItemType { get; set; } = ItemTypeClass.Login;

    /// <summary>
    /// Gets or sets the pre-computed data URI for the logo, used directly as the &lt;img src&gt; value.
    /// Callers with raw bytes should run them through <see cref="LogoConverter.ToDataUri"/> first.
    /// </summary>
    [Parameter]
    public string? LogoDataUri { get; set; }

    /// <summary>
    /// Gets or sets the card number for CreditCard items (used for brand detection).
    /// </summary>
    [Parameter]
    public string? CardNumber { get; set; }

    /// <summary>
    /// Gets or sets the alt text for the image.
    /// </summary>
    [Parameter]
    public string AltText { get; set; } = "Item";

    /// <summary>
    /// Gets or sets the size class for the icon (Tailwind CSS classes).
    /// </summary>
    [Parameter]
    public string SizeClass { get; set; } = "w-10 h-10";

    /// <summary>
    /// Gets or sets whether to show placeholder on image error.
    /// </summary>
    private bool ShowPlaceholder { get; set; }

    /// <summary>
    /// Handle image load error by showing placeholder.
    /// </summary>
    private void OnImageError()
    {
        ShowPlaceholder = true;
        StateHasChanged();
    }

    /// <summary>
    /// Gets the appropriate card icon SVG based on the card brand.
    /// Uses centralized ItemTypeIcons definitions.
    /// </summary>
    private string GetCardIconSvg()
    {
        return CardBrandDetector.Detect(CardNumber) switch
        {
            CardBrandDetector.CardBrand.Visa => ItemTypeIcons.Visa,
            CardBrandDetector.CardBrand.Mastercard => ItemTypeIcons.Mastercard,
            CardBrandDetector.CardBrand.Amex => ItemTypeIcons.Amex,
            CardBrandDetector.CardBrand.Discover => ItemTypeIcons.Discover,
            _ => ItemTypeIcons.CreditCard,
        };
    }
}
