@using Indotalent.Features.Setting.SystemUser
@using Indotalent.Features.Setting.SystemUser.Cqrs
@using Indotalent.Infrastructure.Authorization.Identity
@using MudBlazor
@inject SystemUserService SystemUserService
@inject ISnackbar Snackbar

<MudPaper Elevation="0" Square="true" Class="pa-6 mb-3 d-flex align-center justify-space-between" Style="border: 1px solid #DCEBFA;">
    <div class="d-flex align-center gap-4">
        <MudIconButton Icon="@Icons.Material.Filled.ArrowBack" OnClick="() => OnCancel.InvokeAsync()" />
        <div>
            <MudText Typo="Typo.h5" Style="font-weight: 900; color: #1a1a1a;">Manage User Roles</MudText>
            <MudText Typo="Typo.body2" Style="color: #64748b;">Assign or revoke roles for: @Data.FullName (@Data.Email)</MudText>
        </div>
    </div>
</MudPaper>

<MudPaper Elevation="0" Outlined="true" Class="pa-8" Style="border-radius: 0px; border: 1px solid #DCEBFA;">
    <MudText Typo="Typo.button" Color="Color.Primary" Style="font-weight: 800;">Available System Roles</MudText>
    <MudDivider Class="mt-2 mb-6" />

    <MudGrid Spacing="3">
        @foreach (var role in ApplicationRoles.AllRoles)
        {
            <MudItem xs="12" sm="4" md="3">
                <MudPaper Elevation="0" Class="pa-4 d-flex align-center" Style="border: 1px solid #E2E8F0; background-color: #F8FAFC;">
                    <MudCheckBox T="bool"
                                 Value="@(_selectedRoles.Contains(role))"
                                 ValueChanged="@((bool val) => OnRoleToggled(role, val))"
                                 Color="Color.Primary"
                                 Dense="true" />
                    <MudStack Spacing="0">
                        <MudText Typo="Typo.body2" Style="font-weight: 700;">@role</MudText>
                        <MudText Typo="Typo.caption" Style="color: #64748b;">System Role</MudText>
                    </MudStack>
                </MudPaper>
            </MudItem>
        }
    </MudGrid>

    <div class="d-flex justify-end gap-2 mt-10">
        <MudButton OnClick="() => OnCancel.InvokeAsync()"
                   Variant="Variant.Outlined"
                   Disabled="_processing"
                   Style="border-radius: 4px; border: 1px solid #e0e0e0; text-transform: none; font-weight: 700;">
            Cancel
        </MudButton>
        <MudButton Color="Color.Primary"
                   Variant="Variant.Filled"
                   OnClick="Submit"
                   Disabled="_processing"
                   Style="border-radius: 4px; text-transform: none; font-weight: 700;">
            @if (_processing)
            {
                <MudProgressCircular Class="ms-n1" Size="Size.Small" Indeterminate="true" />
                <MudText Class="ms-2" Typo="Typo.button" Style="text-transform: none !important;">Saving Roles...</MudText>
            }
            else
            {
                <MudText>Update Role Assignments</MudText>
            }
        </MudButton>
    </div>
</MudPaper>

@code {
    [Parameter] public UpdateSystemUserRequest Data { get; set; } = new();
    [Parameter] public EventCallback OnCancel { get; set; }
    [Parameter] public EventCallback OnSuccess { get; set; }

    private List<string> _selectedRoles = new();
    private bool _processing = false;

    protected override void OnInitialized()
    {
        if (Data.UserRoles != null)
        {
            _selectedRoles = new List<string>(Data.UserRoles);
        }
    }

    private void OnRoleToggled(string role, bool isSelected)
    {
        if (isSelected)
        {
            if (!_selectedRoles.Contains(role)) _selectedRoles.Add(role);
        }
        else
        {
            _selectedRoles.Remove(role);
        }
    }

    private async Task Submit()
    {
        if (_selectedRoles.Count == 0)
        {
            Snackbar.Add("Please select at least one role", Severity.Warning);
            return;
        }

        _processing = true;
        try
        {
            var request = new ChangeRoleRequest
            {
                UserId = Data.Id ?? string.Empty,
                Roles = _selectedRoles
            };

            var success = await SystemUserService.ChangeRoleAsync(request);
            await Task.Delay(500);

            if (success)
            {
                Snackbar.Add("User roles updated successfully", Severity.Success);
                await OnSuccess.InvokeAsync();
            }
        }
        finally
        {
            _processing = false;
        }
    }
}