@using AuthorBuddy.Web.Models
@inject IProjectService ProjectService

<div class="category-selector" @onclick:stopPropagation>
    @foreach (var cat in _allCategories)
    {
        var isActive = (Current & cat) == cat;
        var disabled = _disabledCategories.Contains(cat);
        <button class="category-btn @(isActive ? "active" : "") @(disabled ? "disabled" : "")"
                @onclick="() => ToggleCategory(cat)"
                disabled="@disabled"
                title="@CategoryName(cat)">
            @CategoryIcon(cat)
            <span class="cat-label">@CategoryName(cat)</span>
        </button>
    }
</div>

<style>
    .category-selector {
        display: inline-flex;
        gap: 3px;
        flex-wrap: wrap;
    }
    .category-btn {
        font-size: 11px;
        padding: 1px 6px;
        border: 1px solid var(--border-color, #ccc);
        border-radius: 4px;
        background: var(--bg-card, #f5f5f5);
        color: var(--text-main, #333);
        cursor: pointer;
        line-height: 1.5;
        display: inline-flex;
        align-items: center;
        gap: 2px;
        transition: all 0.15s;
    }
    .category-btn.active {
        background: var(--accent-color, #4a90d9);
        color: #fff;
        border-color: var(--accent-color, #4a90d9);
    }
    .category-btn.disabled {
        opacity: 0.35;
        cursor: not-allowed;
    }
    .category-btn:not(.disabled):hover {
        opacity: 0.85;
    }
    .cat-label {
        font-size: 10px;
    }
</style>

@code {
    private static readonly FileCategory[] _allCategories = new[]
    {
        FileCategory.Character,
        FileCategory.Exposee,
        FileCategory.Fact,
        FileCategory.Document
    };

    private HashSet<FileCategory> _disabledCategories = new();

    [Parameter] public FileCategory Current { get; set; }
    [Parameter] public int ProjectId { get; set; }
    [Parameter] public string FilePath { get; set; } = string.Empty;
    [Parameter] public EventCallback<FileCategory> OnCategoryChanged { get; set; }

    protected override void OnParametersSet()
    {
        UpdateDisabled();
    }

    private void UpdateDisabled()
    {
        _disabledCategories.Clear();
        foreach (var cat in _allCategories)
        {
            if (cat == Current) continue;
            var combined = Current | cat;
            if (!FileCategoryHelper.IsValid(combined))
                _disabledCategories.Add(cat);
        }
    }

    private async Task ToggleCategory(FileCategory cat)
    {
        if (_disabledCategories.Contains(cat)) return;

        var newCategory = Current.HasFlag(cat) ? Current & ~cat : Current | cat;

        if (!FileCategoryHelper.IsValid(newCategory)) return;

        await ProjectService.SetFileCategoryAsync(ProjectId, FilePath, newCategory);
        await OnCategoryChanged.InvokeAsync(newCategory);
    }

    private static string CategoryName(FileCategory cat) => cat switch
    {
        FileCategory.Character => "Charakter",
        FileCategory.Exposee => "Exposé",
        FileCategory.Fact => "Fakt",
        FileCategory.Document => "Dokument",
        _ => ""
    };

    private static string CategoryIcon(FileCategory cat) => cat switch
    {
        FileCategory.Character => "👤",
        FileCategory.Exposee => "📋",
        FileCategory.Fact => "📌",
        FileCategory.Document => "📄",
        _ => ""
    };
}
