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

namespace DodoSSH.Client.Sync;

/// <summary>
/// The connections this vault has recorded, decrypted, with unpushed local entries laid over them.
/// </summary>
/// <remarks>
/// <para>
/// The seventh facade over the same generic repository, and the first whose <c>UpdateAsync</c> is missing on
/// purpose. A connection log entry is written once, at close, and never edited — see
/// <c>VaultConnectionLogEntry</c> for why that is what makes a synced log tractable at all — so an update
/// method here would be an invitation to break the property the whole design rests on.
/// </para>
/// <para>
/// <see cref="DeleteAsync"/> stays, because retention needs it. It is the only thing that deletes an entry.
/// </para>
/// </remarks>
public sealed class ConnectionLogRepository(ItemStore items, OutboxStore outbox, VaultKeyring keyring)
{
    private readonly VaultItemRepository<ConnectionLogSecret> entries =
        new(ConnectionLogEntryKind.Instance, items, outbox, keyring);

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

    /// <inheritdoc cref="VaultItemRepository{TSecret}.CreateAsync" />
    public Task<Guid> CreateAsync(
        Guid vaultId,
        ConnectionLogSecret entry,
        CancellationToken cancellationToken) =>
        entries.CreateAsync(vaultId, entry, cancellationToken);

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

/// <summary>
/// The keychain changes this vault has recorded, decrypted, with unpushed local entries laid over them.
/// </summary>
/// <inheritdoc cref="ConnectionLogRepository" path="/remarks" />
public sealed class ActivityLogRepository(ItemStore items, OutboxStore outbox, VaultKeyring keyring)
{
    private readonly VaultItemRepository<ActivityLogSecret> entries =
        new(ActivityLogEntryKind.Instance, items, outbox, keyring);

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

    /// <inheritdoc cref="VaultItemRepository{TSecret}.CreateAsync" />
    public Task<Guid> CreateAsync(
        Guid vaultId,
        ActivityLogSecret entry,
        CancellationToken cancellationToken) =>
        entries.CreateAsync(vaultId, entry, cancellationToken);

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