using DodoSSH.Client.Domain;
using DodoSSH.Client.Storage;

namespace DodoSSH.Client.Sync;

/// <summary>
/// The groups in this vault, decrypted, with unpushed local changes laid over them.
/// </summary>
/// <remarks>
/// <para>
/// The fifth facade over the same generic repository, and like the fourth it needed no new sync logic at all.
/// </para>
/// <para>
/// <b>Deleting a group does not touch the hosts in it.</b> There is deliberately no <c>DeleteAsync</c>
/// overload that unfiles its members: one user action would become N host writes, N outbox rows and N chances
/// to merge against an edit nobody made, and the group's own tombstone can still lose a merge — by which time
/// the membership it was clearing is gone. Hosts left holding a dangling id fall under the ungrouped heading,
/// which is where the interface handles it. See <see cref="HostSecret.GroupId"/>.
/// </para>
/// </remarks>
public sealed class HostGroupRepository(
    ItemStore items,
    OutboxStore outbox,
    VaultKeyring keyring,
    IActivityLogSink? activity = null)
{
    private readonly VaultItemRepository<HostGroupSecret> groups =
        new(HostGroupKind.Instance, items, outbox, keyring, activity);

    /// <inheritdoc cref="VaultItemRepository{TSecret}.ListAsync" />
    public Task<ItemListing<HostGroupSecret>> ListAsync(
        Guid vaultId,
        CancellationToken cancellationToken) =>
        groups.ListAsync(vaultId, cancellationToken);

    /// <inheritdoc cref="VaultItemRepository{TSecret}.CreateAsync" />
    public Task<Guid> CreateAsync(
        Guid vaultId,
        HostGroupSecret group,
        CancellationToken cancellationToken) =>
        groups.CreateAsync(vaultId, group, cancellationToken);

    /// <inheritdoc cref="VaultItemRepository{TSecret}.UpdateAsync" />
    public Task UpdateAsync(
        Guid vaultId,
        Guid entityId,
        HostGroupSecret group,
        CancellationToken cancellationToken) =>
        groups.UpdateAsync(vaultId, entityId, group, cancellationToken);

    /// <inheritdoc cref="VaultItemRepository{TSecret}.DeleteAsync" />
    public Task DeleteAsync(Guid vaultId, Guid entityId, CancellationToken cancellationToken) =>
        groups.DeleteAsync(vaultId, entityId, cancellationToken);
}
