using Chronos.Core.Model;
namespace Chronos.Data.Sync;
/// Where a sequence stands between this PC and the server (BRIEF §12.7, §14.5).
public enum SyncStatus
{
/// Same originals and settings on both sides.
InSync,
LocalOnly,
RemoteOnly,
/// Only on the server; this PC keeps its proxies for editing.
ProxiesOnly,
/// This PC holds originals or settings the server lacks.
LocalAhead,
/// The server holds originals or settings this PC lacks.
ServerAhead,
/// Both sides changed the settings, title, tags, rating or notes since they last agreed: the user picks.
Conflict,
/// An upload or download job is queued, running or paused.
Transferring,
}
/// How the user settles a conflict (BRIEF §12.7).
public enum SyncResolution
{
KeepMine,
TakeServer,
/// The server's version wins; this PC's settings are kept next to the project as a dated copy.
SaveBoth,
}
/// What a download job fetches.
public enum SyncDownloadMode
{
/// Originals, hash-verified, into the local archive.
Originals,
/// Proxies (thumbnails where the server has none) into the proxy cache.
Proxies,
/// The server's renders the local copy lacks, into its exports folder.
Exports,
}
/// What an upload job sends.
public enum SyncUploadMode
{
/// Originals and settings (creating the server sequence when needed).
Originals,
/// This PC's renders the server lacks (BRIEF §14.5: renders are uploaded on request).
Exports,
}
///
/// One server sequence as this client last saw it, linked to its local copy when there is one. The base versions are
/// the settings counters both sides had when they last agreed; the diff versions are the frame counters and render
/// counts the missing counts were computed at, so originals and renders are compared again only when either side's change.
///
public sealed record SyncLink
{
/// Normalised server address ().
public required string Server { get; init; }
public required Guid RemoteId { get; init; }
public Guid? LocalId { get; init; }
// ---- The server's side, from its sequence listing ----
public required string Title { get; init; }
public int FrameCount { get; init; }
public long TotalBytes { get; init; }
public int RenderCount { get; init; }
public SequenceStatus RemoteStatus { get; init; }
public int RemoteSettingsVersion { get; init; }
public int RemoteFramesVersion { get; init; }
public DateTimeOffset RemoteUpdatedAt { get; init; }
// ---- Agreement and comparison state ----
public int? BaseLocalSettings { get; init; }
public int? BaseRemoteSettings { get; init; }
public int? DiffLocalFrames { get; init; }
public int? DiffRemoteFrames { get; init; }
public int? DiffLocalRenders { get; init; }
public int? DiffRemoteRenders { get; init; }
///
/// The server's update time the originals were compared at, kept while proxies it asks for may be made here (some
/// were counted, or the copy holds originals the server cannot decode and this PC can); null otherwise. The proxies
/// it asks for change without its frame counters moving (one arrives from a render node, its proxy policy changes),
/// and every such change leaves a new update time, so the originals are compared again when it moved.
///
public DateTimeOffset? DiffRemoteUpdatedAt { get; init; }
///
/// Local originals the server does not have, and proxies it asks for that this PC can make (it cannot decode those
/// originals): an upload sends both.
///
public int MissingRemote { get; init; }
/// Server originals this PC does not have.
public int MissingLocal { get; init; }
/// Server renders the local copy does not have, and their size.
public int ExportsToGet { get; init; }
public long ExportsToGetBytes { get; init; }
/// Renders of the local copy the server does not have (whose file is still on this PC), and their size.
public int ExportsToSend { get; init; }
public long ExportsToSendBytes { get; init; }
/// Settings versions at which the content was found to conflict; null when it does not.
public int? ConflictLocalSettings { get; init; }
public int? ConflictRemoteSettings { get; init; }
/// The server's side when the conflict was found: what the user is shown and what "Take server" applies.
public SyncPayload? ConflictRemote { get; init; }
/// Frames whose proxy (or thumbnail) is in this PC's proxy cache.
public int ProxiesCached { get; init; }
/// Server frames version the proxy cache was filled at.
public int? ProxiesFramesVersion { get; init; }
/// Why the last comparison of this sequence failed (it is compared again next time); null when it worked.
public string? Error { get; init; }
public DateTimeOffset? SyncedAt { get; init; }
public bool ServerHasProxies => (RemoteStatus & SequenceStatus.ProxiesBuilt) != 0;
///
/// The proxy cache was never completed or predates the server's last frame change (frames the server has no
/// proxy or thumbnail for stay missing until their frames change, so they are not fetched again and again).
///
public bool ProxiesStale => ProxiesFramesVersion != RemoteFramesVersion;
}
/// One row of the sync table: the local sequence, the server sequence, or both, and where they stand.
/// The upload or download job working on it, if any (the user's before background ones).
public sealed record SyncEntry(Sequence? Local, SyncLink? Link, SyncStatus Status, Job? Transfer = null)
{
public string Title => Local?.DisplayTitle ?? Link?.Title ?? string.Empty;
/// The server's id, or the local one for a sequence the server does not have.
public Guid Id => Link?.RemoteId ?? Local!.Id;
/// This PC holds originals of the sequence.
public bool HasLocalOriginals => Local is { FrameCount: > 0 };
}
/// The sync table after a refresh.
/// Time the refresh took (listing, manifest diffs, settings exchange).
public sealed record SyncSnapshot(string Server, IReadOnlyList Entries, DateTimeOffset RefreshedAt, TimeSpan Duration)
{
public long ServerBytes => Entries.Sum(e => e.Link?.TotalBytes ?? 0);
public int ServerSequences => Entries.Count(e => e.Link is not null);
}
/// Options a refresh applies.
/// Send and take settings changed on one side only (Server.html "Sync projects automatically").
/// Keep the proxy caches of server sequences this PC opened with proxies up to date.
/// The account may change server sequences; without it this PC's edits are never sent.
public sealed record SyncPolicy(bool AutoSyncSettings = true, bool KeepProxiesOffline = false, bool CanEdit = true);
///
/// A conflict could not be settled as the user chose because one side changed again after it was shown; the
/// sequence was compared again, so the user can choose on the fresh content.
///
public sealed class SyncConflictChangedException(string message) : InvalidOperationException(message);