using AttendanceHub.Sync.Domain;
using AttendanceHub.Sync.Upload;
namespace AttendanceHub.Sync.Persistence;
///
/// Durable local sync store (SQLite). Testable without COM.
/// Phase 3: no HTTP/ACK/backend cursor. Upload fields are reserved only.
///
public interface ISyncStore : IAsyncDisposable
{
Task InitializeAsync(CancellationToken cancellationToken = default);
Task UpsertPunchAsync(NormalizedPunch punch, CancellationToken cancellationToken = default);
Task UpsertBatchAsync(
IReadOnlyList punches,
CancellationToken cancellationToken = default);
Task GetWatermarkAsync(string deviceId, CancellationToken cancellationToken = default);
Task SaveWatermarkAsync(ReconciliationWatermark watermark, CancellationToken cancellationToken = default);
Task FindPunchAsync(PunchIdentity identity, CancellationToken cancellationToken = default);
Task> ListPunchesAsync(string deviceId, int limit = 100, CancellationToken cancellationToken = default);
Task GetStatsAsync(CancellationToken cancellationToken = default);
Task GetDeviceStatsAsync(string deviceId, CancellationToken cancellationToken = default);
// ---- Phase 4 upload outbox ----
Task> ClaimPendingBatchAsync(
string deviceId,
int batchSize,
TimeSpan sendingLease,
DateTimeOffset now,
CancellationToken cancellationToken = default);
Task RecoverStaleSendingAsync(TimeSpan sendingLease, DateTimeOffset now, CancellationToken cancellationToken = default);
Task ApplyAckAsync(
long localId,
RecordOutcome outcome,
string? reason,
string? serverReceiptId,
DateTimeOffset now,
RetryPolicy retryPolicy,
CancellationToken cancellationToken = default);
Task MoveToClientDlqAsync(
long localId,
string failureClass,
string reason,
DateTimeOffset now,
CancellationToken cancellationToken = default);
Task GetUploadStatsAsync(string? deviceId = null, CancellationToken cancellationToken = default);
Task SetUploadStateAsync(long localId, OutboxUploadState state, CancellationToken cancellationToken = default);
}
public sealed record UploadOutboxStats(
long Pending,
long Sending,
long Sent,
long Retry,
long Dlq,
long PermanentFailure,
DateTimeOffset? OldestPendingAt,
DateTimeOffset? LastSuccessAt);
/// Result of one ledger-retention pass (prune + optional VACUUM).
public sealed record LedgerPruneResult(
int PunchesDeleted,
int DlqDeleted,
bool Vacuumed,
long FreelistBytesBefore,
long DbBytesAfter);
public enum PunchUpsertKind
{
Inserted = 0,
Duplicate = 1,
Rejected = 2,
Expired = 3
}
public sealed record PunchUpsertResult(
PunchUpsertKind Kind,
PunchIdentity Identity,
string? Reason);
public sealed record BatchPersistResult(
int Attempted,
int Inserted,
int Duplicates,
int Rejected,
int Expired,
bool AllCommitted,
int BatchesCommitted,
int BatchesFailed,
string? Error,
IReadOnlyList InsertedRows,
IReadOnlyList RejectedRows)
{
public bool HasDurableRows => Inserted > 0;
}
public sealed record PersistedPunch(
long LocalId,
PunchIdentity Identity,
PunchSource Source,
int AttendanceState,
int VerifyMethod,
int WorkCode,
bool IsInvalid,
int SessionGeneration,
string SourceApi,
DateTimeOffset FirstSeenAt,
DateTimeOffset LastSeenAt,
int OccurrenceCount,
PunchTimeStatus TimeStatus,
// Reserved for Phase 4 upload — not used yet.
string UploadState,
int UploadAttempts,
string? LastUploadError)
{
public static PersistedPunch FromRow(NormalizedPunch p, long localId, string uploadState, int uploadAttempts, string? lastUploadError)
=> new(localId, p.Identity, p.Source, p.AttendanceState, p.VerifyMethod, p.WorkCode, p.IsInvalid,
p.SessionGeneration, p.SourceApi, p.FirstSeenAt, p.LastSeenAt, p.OccurrenceCount, p.TimeStatus,
uploadState, uploadAttempts, lastUploadError);
}
public sealed record StoreStats(
long PunchCount,
long RealtimeCount,
long ReconciliationCount,
long DuplicateCount,
long FutureHoldCount,
long RejectedCount,
long BatchesCommitted,
long BatchesFailed,
long BusyEvents,
long SqliteExceptions,
long DbSizeBytes,
DateTimeOffset? OldestPendingSeenAt,
int DeviceCount);
public sealed record DevicePersistStats(
string DeviceId,
long PunchCount,
long DuplicateCount,
long FutureHoldCount,
DateTimeOffset? LastPersistedPunchTime,
DateTimeOffset? OldestSeenAt,
TimeSpan? PersistenceLag,
int? LastSessionGeneration);