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

namespace DodoSSH.Client.Sync;

/// <summary>
/// The host keys this vault trusts, decrypted, with unpushed local changes laid over them.
/// </summary>
/// <remarks>
/// <para>
/// The fourth facade over the same generic repository, and it needed no new sync logic at all — which was the
/// point of the item-kind seam.
/// </para>
/// <para>
/// <b>Nothing here is on the SSH handshake path.</b> Listing decrypts every pin in the vault, and the
/// handshake asks about host key trust from inside a synchronous SSH.NET event where a decryption per lookup
/// would be I/O and AEAD work on the thread completing the key exchange. <c>VaultKnownHostStore</c> exists to
/// keep those apart: it reads through here when a vault opens and after each synchronisation pass, and answers
/// the handshake from an in-memory snapshot.
/// </para>
/// </remarks>
public sealed class KnownHostRepository(
    ItemStore items,
    OutboxStore outbox,
    VaultKeyring keyring,
    IActivityLogSink? activity = null)
{
    private readonly VaultItemRepository<KnownHostSecret> knownHosts =
        new(KnownHostKeyKind.Instance, items, outbox, keyring, activity);

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

    /// <inheritdoc cref="VaultItemRepository{TSecret}.CreateAsync" />
    public Task<Guid> CreateAsync(
        Guid vaultId,
        KnownHostSecret knownHost,
        CancellationToken cancellationToken) =>
        knownHosts.CreateAsync(vaultId, knownHost, cancellationToken);

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

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