@page "/user-management"
@attribute [Authorize]
@inject IAuthService AuthService
@inject ILocalizationService Loc
@inject NavigationManager Navigation

<PageTitle>@Loc["usermanagement.title"] - Author Buddy</PageTitle>

<div class="usermanagement-page">
    <h3>@Loc["usermanagement.title"]</h3>

    <table class="usermanagement-table">
        <thead>
            <tr>
                <th>@Loc["usermanagement.col.name"]</th>
                <th>@Loc["usermanagement.col.role"]</th>
                <th>@Loc["usermanagement.col.status"]</th>
                <th>@Loc["usermanagement.col.actions"]</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var u in users)
            {
                <tr class="@(u.Id == currentUserId ? "usermanagement-current" : "") @(!u.IsActive ? "usermanagement-inactive" : "")">
                    <td>@u.Name @(u.Id == currentUserId ? "(Du)" : "")</td>
                    <td>@(u.Role == UserRole.Administrator ? Loc["settings.user.role.admin"] : Loc["settings.user.role.author"])</td>
                    <td>
                        @if (u.IsActive)
                        {
                            <span class="usermanagement-status-active">@Loc["usermanagement.status.active"]</span>
                        }
                        else
                        {
                            <span class="usermanagement-status-inactive">@Loc["usermanagement.status.inactive"]</span>
                        }
                    </td>
                    <td class="usermanagement-actions">
                        @if (u.Id != currentUserId)
                        {
                            @if (u.IsActive)
                            {
                                <button class="btn btn-sm" @onclick="() => DeactivateUser(u.Id)" title="@Loc["usermanagement.action.deactivate"]">@Loc["usermanagement.action.deactivate"]</button>
                            }
                            else
                            {
                                <button class="btn btn-sm btn-primary" @onclick="() => ActivateUser(u.Id)" title="@Loc["usermanagement.action.activate"]">@Loc["usermanagement.action.activate"]</button>
                            }
                            <button class="btn btn-sm btn-danger" @onclick="() => DeleteUser(u.Id)" title="@Loc["usermanagement.action.delete"]">@Loc["usermanagement.action.delete"]</button>
                        }
                        else
                        {
                            <span style="font-size:11px;color:var(--border-color);">@Loc["usermanagement.cannot_self"]</span>
                        }
                    </td>
                </tr>
            }
        </tbody>
    </table>

    @if (!string.IsNullOrEmpty(statusMessage))
    {
        <div class="usermanagement-status">@statusMessage</div>
    }

    <button class="btn btn-secondary" @onclick='() => Navigation.NavigateTo("/settings")' style="margin-top:12px;">@Loc["common.back"]</button>
</div>

@code {
    private List<UserEntity> users = new();
    private int currentUserId;
    private string statusMessage = string.Empty;

    protected override async Task OnInitializedAsync()
    {
        await LoadUsers();
    }

    private async Task LoadUsers()
    {
        users = await AuthService.GetAllUsersAsync();
        currentUserId = AuthService.CurrentUser?.Id ?? 0;
    }

    private async Task ActivateUser(int userId)
    {
        var ok = await AuthService.SetUserActiveAsync(userId, true);
        statusMessage = ok ? Loc["usermanagement.status.activated"] : Loc["common.error"];
        await LoadUsers();
    }

    private async Task DeactivateUser(int userId)
    {
        var ok = await AuthService.SetUserActiveAsync(userId, false);
        statusMessage = ok ? Loc["usermanagement.status.deactivated"] : Loc["common.error"];
        await LoadUsers();
    }

    private async Task DeleteUser(int userId)
    {
        var ok = await AuthService.DeleteUserAsync(userId);
        statusMessage = ok ? Loc["usermanagement.status.deleted"] : Loc["common.error"];
        await LoadUsers();
    }
}
