@namespace ZB.MOM.WW.Secrets.Ui
@implements IDisposable
@inject ISecretStore Store
@* Components/SecretsList.razor — metadata-only listing of secrets. Never renders a value or
   ciphertext. Add/rotate use the in-page SecretEditor; delete uses the in-page ConfirmDeleteModal;
   reveal is the policy-gated, audited RevealButton. The list reloads after any mutation. *@

<AuthorizeView Policy="@SecretsAuthorization.ManagePolicy">
    <div class="toolbar btn-row">
        <TechButton Variant="ButtonVariant.Primary" data-testid="add-secret" @onclick="ShowAdd">
            Add secret
        </TechButton>
    </div>
</AuthorizeView>

@if (_editorMode != EditorMode.None)
{
    <SecretEditor ExistingName="_rotateName"
                  OnSaved="OnMutatedAsync"
                  OnCancelled="CloseEditor" />
}

<TechCard Title="Secrets">
    @if (_secrets is null)
    {
        <p class="mono">Loading…</p>
    }
    else
    {
        <div class="table-wrap">
            <table class="data-table">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Description</th>
                        <th>Content type</th>
                        <th>KEK</th>
                        <th class="num">Revision</th>
                        <th>Updated (UTC)</th>
                        <th>Updated by</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @if (_secrets.Count == 0)
                    {
                        <tr>
                            <td class="empty-row" colspan="8">No secrets.</td>
                        </tr>
                    }
                    else
                    {
                        @foreach (SecretMetadata secret in _secrets)
                        {
                            <tr>
                                <td class="mono">@secret.Name.Value</td>
                                <td>@secret.Description</td>
                                <td>@secret.ContentType</td>
                                <td class="mono">@secret.KekId</td>
                                <td class="num">@secret.Revision</td>
                                <td class="mono">@secret.UpdatedUtc.UtcDateTime.ToString("u")</td>
                                <td>@secret.UpdatedBy</td>
                                <td class="row-actions">
                                    <AuthorizeView Policy="@SecretsAuthorization.RevealPolicy">
                                        <RevealButton Name="secret.Name" />
                                    </AuthorizeView>
                                    <AuthorizeView Policy="@SecretsAuthorization.ManagePolicy">
                                        <button type="button" class="btn btn-outline-secondary rotate-secret"
                                                data-secret-name="@secret.Name.Value"
                                                @onclick="() => ShowRotate(secret.Name)">
                                            Rotate
                                        </button>
                                        <button type="button" class="btn btn-danger delete-secret"
                                                data-secret-name="@secret.Name.Value"
                                                @onclick="() => ShowDelete(secret.Name)">
                                            Delete
                                        </button>
                                    </AuthorizeView>
                                </td>
                            </tr>
                        }
                    }
                </tbody>
            </table>
        </div>
    }
</TechCard>

<ConfirmDeleteModal Visible="_deleteTarget.HasValue"
                    Name="_deleteTarget.GetValueOrDefault()"
                    OnDeleted="OnMutatedAsync"
                    OnCancelled="CloseDelete" />

@code {
    private readonly CancellationTokenSource _cts = new();
    private IReadOnlyList<SecretMetadata>? _secrets;

    private EditorMode _editorMode = EditorMode.None;
    private SecretName? _rotateName;
    private SecretName? _deleteTarget;

    private enum EditorMode
    {
        None,
        Add,
        Rotate,
    }

    /// <inheritdoc />
    protected override async Task OnInitializedAsync() => await ReloadAsync();

    private async Task ReloadAsync()
        => _secrets = await Store.ListAsync(includeDeleted: false, _cts.Token);

    private void ShowAdd()
    {
        _rotateName = null;
        _editorMode = EditorMode.Add;
    }

    private void ShowRotate(SecretName name)
    {
        _rotateName = name;
        _editorMode = EditorMode.Rotate;
    }

    private void CloseEditor()
    {
        _editorMode = EditorMode.None;
        _rotateName = null;
    }

    private void ShowDelete(SecretName name) => _deleteTarget = name;

    private void CloseDelete() => _deleteTarget = null;

    private async Task OnMutatedAsync()
    {
        CloseEditor();
        CloseDelete();
        await ReloadAsync();
    }

    /// <inheritdoc />
    public void Dispose()
    {
        _cts.Cancel();
        _cts.Dispose();
    }
}
