using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Data.Archiving;
using Chronos.Data.Projects;
using Chronos.Data.Sync;
namespace Chronos.Data.Copies;
/// The user's choice for one photo under Choose each (nothing preselected: ).
public enum PhotoChoice
{
Leave = 0,
Bring = 1,
KeepBoth = 2,
TakeHere = 3,
TakeThere = 4,
}
/// What a plan does with one photo.
public enum PhotoAction
{
Send = 0,
Fetch = 1,
SendVersion = 2,
FetchVersion = 3,
TrashHere = 4,
TrashThere = 5,
}
/// Why a plan cannot be applied (each maps to one Errors.Copies.* key).
public enum CopyBlock
{
NeedsNewerServer,
NoUploadRight,
NoFetchRight,
NoEditRight,
NoDeleteRight,
LinkTaken,
NoSpace,
Busy,
ArchiveOffline,
DetailsUnchosen,
FolderMoved,
UnsentWorkspace,
BothLinked,
TrashBlocked,
KeptUnchosen,
ChangedFile,
/// Photos sent into a server sequence the account neither owns nor may edit (the server refuses that, 403).
NoAddRight,
/// The other sequence of a merge holds photos a verify flagged changed or damaged: they would go to the trash unchecked.
DamagedPhotos,
}
/// The choices of an Integrate: photo policy, per-photo choices (by photo key), per-detail sides (by path), projects.
public sealed record IntegrateChoices(CopyPhotoPolicy Photos, IReadOnlyDictionary PerPhoto,
IReadOnlyDictionary Details, CopyProjects Projects);
/// One photo step of a plan: the photo's key, what is done, the frame it starts from, its name, size and SHA-256.
public sealed record PhotoStep(string Key, PhotoAction Action, Guid? FrameId, string FileName, long Bytes, string? Sha256);
/// A photo a trash step takes whose raw SHA-256 nothing else holds (no other frame of this index, nor the other side).
public sealed record LastCopy(string Key, CopySide Side, string FileName, long Bytes);
/// A project copied across: from which side, the source, the name it gets at the other home, its versions and head.
public sealed record ProjectStep(CopySide From, Guid SourceProjectId, string SourceName, string TargetName, int Versions, Guid SourceHeadId);
///
/// The dry run of an Integrate of a local sequence with a server sequence (design §5c). Payload is the one portable payload
/// both sides get; LocalPayload and RemotePayload are each side's as planned (the apply-time content check).
///
public sealed record IntegratePlan(Guid CopyId, string Server, Guid LocalId, Guid RemoteId, IntegrateChoices Choices,
IReadOnlyList Photos, IReadOnlyList LastCopies, int LeftOut, SyncPayload? Payload,
SyncPayload? LocalPayload, SyncPayload? RemotePayload, int DetailChanges, IReadOnlyList Projects,
long FetchBytes, long SendBytes, long FreeBytes, long? QuotaLeftBytes, IReadOnlyList Blocks,
int LocalFramesVersion, int LocalSettingsVersion, int RemoteFramesVersion, int RemoteSettingsVersion, string Fingerprint)
{
public bool CanApply => Blocks.Count == 0;
public bool Destructive => Photos.Any(p => p.Action is PhotoAction.TrashHere or PhotoAction.TrashThere);
/// New photos move by themselves afterwards only when the plan leaves nothing on one side only (design §6.1).
public bool AutoPhotos => LeftOut == 0;
/// The context the plan was made with (rights, names): the job plans again with it ().
public CopyPlanContext? Context { get; init; }
///
/// Photos whose server version stays for want of the right to trash it there (Delete, and owner or admin of the server
/// sequence): counted in ; the server's Main uses this PC's version of them.
///
public int StaysForDelete { get; init; }
/// Photos whose server version the plan trashes there or excludes from the server's Main: the server uses this PC's version.
public int ServerUsesOurs { get; init; }
}
/// The choices of a merge of two local copies (projects are always copied across: the other's home goes to the trash).
public sealed record MergeChoices(CopyPhotoPolicy Photos, IReadOnlyDictionary PerPhoto,
IReadOnlyDictionary Details);
/// A render of the dropped sequence that moves to the kept one (its row keeps its id).
public sealed record RenderStep(Guid RenderId, string FileName, long Bytes, string? Sha256);
/// The dry run of a merge of two local sequences into the kept one (design §5d).
public sealed record MergePlan(Guid CopyId, Guid KeepId, Guid DropId, MergeChoices Choices, IReadOnlyList Photos,
IReadOnlyList LastCopies, IReadOnlyList Renders, SyncPayload? Payload, int DetailChanges,
IReadOnlyList Projects, IReadOnlyList SessionKeys, IReadOnlyList SkippedSessionKeys,
IReadOnlyList Links, DeletePlan Trash, long CopyBytes, long FreeBytes, IReadOnlyList Blocks,
int KeepFramesVersion, int KeepSettingsVersion, int DropFramesVersion, int DropSettingsVersion, string Fingerprint)
{
public bool CanApply => Blocks.Count == 0;
/// by the names their servers are shown by.
public IReadOnlyList LinkNames { get; init; } = [];
/// The name of the server both sequences are linked to (); null otherwise.
public string? BothLinkedServer { get; init; }
/// Photos the kept sequence holds that the other deleted: they stay in the kept one ("Deleted in the other").
public int DeletedInOther { get; init; }
}
///
/// What the planners need to know of this PC and the signed-in account: names, rights (OwnsOrAdmin: the account is an admin
/// of the server, which owns every sequence there; owning one is decided per sequence from its owner and ),
/// whether the server keeps projects, the quota left, sequences with unsent workspace work, and every server's originals archive.
///
public sealed record CopyPlanContext(string PcName, string? ServerName, bool CanUpload, bool CanFetch, bool CanEdit, bool CanDelete,
bool OwnsOrAdmin, bool KeepsProjects, long? QuotaLeft, IReadOnlySet UnsentWorkspaceSequences,
IReadOnlySet ServerOriginalsArchives)
{
/// The signed-in account's id: it owns a server sequence whose owner it is (null: unknown, signed out).
public Guid? UserId { get; init; }
}
/// The server-side dry run of moving photos of a pair's server sequence to the server's trash (design §6.2).
public sealed record TrashTherePlan(Guid CopyId, string Server, Guid RemoteId, IReadOnlyList FrameIds, IReadOnlyList Keys, int Photos,
long Bytes, int FramesVersion, IReadOnlyList LastCopies);
/// The dry run of moving photos of a pair's local copy to its archive trash, with the ones no other sequence of this index holds.
public sealed record TrashHerePlan(FrameDeletePlan Plan, IReadOnlyList LastCopies);
///
/// The dry run of dropping one of the two versions one sequence keeps of a photo (owner Q3; a Union merge leaves them, and
/// its pair is gone): the version's frame goes to the sequence's archive trash, the other version ()
/// stays. Applied only while that other version is still there, usable and verified by content. :
/// the version itself when no other sequence of this index holds its content.
///
public sealed record OwnVersionDropPlan(FrameDeletePlan Plan, Guid PartnerFrameId, string PartnerFileName, string PartnerSha256,
IReadOnlyList LastCopies);
///
/// The dry run of dropping one version of a photo kept in two versions (design §4.1, Versions): the version
/// goes to the trash of both sides (this PC's copy and the server's, or both local copies of a merge),
/// the other version stays on both. Applied only while that other version is still held,
/// verified by content, on both sides.
///
/// The other local copy's dry run (a pair of two local copies); null for a server pair.
/// The server's frames of the version (a server pair), at .
public sealed record DropVersionPlan(Guid CopyId, string Key, string PartnerKey, FrameDeletePlan Here, FrameDeletePlan? Other, Guid? RemoteId,
IReadOnlyList RemoteFrameIds, int RemotePhotos, long RemoteBytes, int RemoteFramesVersion, IReadOnlyList LastCopies)
{
/// Every frame the drop moves, on both sides.
public int Photos => Here.Frames.Count + (Other?.Frames.Count ?? 0) + RemotePhotos;
}
///
/// The journal of one project copied to another home: the intended name, when it was asked, the copy once made, the version
/// the copy was made from, and the versions pushed so far (source version id to the copy's).
///
public sealed record TransplantStep(string Intent, DateTimeOffset Asked, Guid? TargetId, Guid? BaseId, int BaseNumber,
IReadOnlyList Moves);
/// A version of the source project and its copy at the other home.
public sealed record TransplantMove(Guid Source, Guid Copy);
/// Copies a project with its whole history from one home to another (ProjectTransplant).
public interface IProjectCopier
{
Task CopyAsync(IProjectHome from, SequenceProject source, IProjectHome to, string name,
TransplantStep? journal, Func save, CancellationToken ct);
}
/// Whether a sequence has workspace projects with unsent versions or unsaved work (a merge waits for them).
public interface IWorkspaceGuard
{
Task HasUnsentAsync(Guid sequenceId, CancellationToken ct);
}
/// The sequences changed since the plan was made: nothing was applied; the pair goes back to its previous state.
public sealed class StaleCopyPlanException : InvalidOperationException
{
public StaleCopyPlanException()
: base(Loc.L("Errors.Copies.StalePlan"))
{
}
public StaleCopyPlanException(string message)
: base(message)
{
}
public StaleCopyPlanException(string message, Exception inner)
: base(message, inner)
{
}
}
/// A sequence of the pair is already linked to another sequence of that server: nothing was changed.
public sealed class CopyLinkTakenException : InvalidOperationException
{
public CopyLinkTakenException()
: base(Loc.L("Errors.Copies.LinkTaken", string.Empty, string.Empty))
{
}
public CopyLinkTakenException(string message)
: base(message)
{
}
public CopyLinkTakenException(string message, Exception inner)
: base(message, inner)
{
}
/// The server key both sequences of a merge are linked to (); null otherwise.
public string? Server { get; private init; }
/// The refusal for already linked to another sequence of .
public static CopyLinkTakenException For(string title, string server) => new(Loc.L("Errors.Copies.LinkTaken", title, server));
///
/// The refusal of a merge whose two sequences are both linked to sequences of the server , worded
/// with (the caller resolves the key to the name the server is shown by; the key by default).
///
public static CopyLinkTakenException BothLinked(string key, string? name = null) =>
new(Loc.L("Errors.Copies.BothLinked", name ?? key)) { Server = key };
}