using System.Text.Json.Serialization;
using Chronos.Core.Imaging;
using Chronos.Core.Naming;
namespace Chronos.Core.Import;
/// What to do with a photo that is already in the archive (same SHA-256).
public enum DuplicatePolicy
{
/// Leave it on the card; the archive copy is identical.
Skip,
/// Import it again into this session's sequence (its name gets a suffix if taken).
ImportAgain,
}
/// How the planner turns the photos of a source into sequences (BRIEF §12.4, §13).
public enum ImportGrouping
{
/// One session per camera body and file format, split where the pause between two photos exceeds the gap.
Sessions,
///
/// The whole source (a folder picked on this PC) is one sequence per file format: camera bodies and pauses are
/// ignored. RAW+JPEG of the same shots stays two sequences, so no frame is ever doubled.
///
WholeFolder,
}
/// Card import settings (BRIEF §12.4, §13); the defaults are the brief's.
public sealed record ImportOptions
{
public string FolderTemplate { get; init; } = TemplateDefaults.Folder;
public string FileTemplate { get; init; } = TemplateDefaults.File;
public TimeSpan SessionGap { get; init; } = TimeSpan.FromMinutes(45);
public int MinimumSessionFrames { get; init; } = 8;
public bool IncludeSubfolders { get; init; } = true;
public bool VerifyCopies { get; init; } = true;
public DuplicatePolicy Duplicates { get; init; } = DuplicatePolicy.Skip;
/// Sessions, or the whole source as one sequence per format (the session gap and minimum then do not apply).
public ImportGrouping Grouping { get; init; } = ImportGrouping.Sessions;
/// Card paths where the user split a session (a new session starts at that photo).
public IReadOnlySet SplitBefore { get; init; } = new HashSet();
/// Card paths where the user merged a session into the previous one.
public IReadOnlySet MergeAt { get; init; } = new HashSet();
/// Titles given to sessions, by session key; used as the sequence title and the {title} token.
public IReadOnlyDictionary Titles { get; init; } = new Dictionary();
}
public enum ImportAction
{
Copy,
/// Imported from this card before (same path, size and time).
SkipImported,
/// Already in the archive (same size and capture time, then the same SHA-256).
SkipInArchive,
}
/// One photo of the plan and its destination name.
public sealed record PlannedItem
{
/// Path below the source root, forward slashes.
public required string SourcePath { get; init; }
public long Size { get; init; }
public DateTime ModifiedUtc { get; init; }
public DateTime CapturedAt { get; init; }
public required string FileName { get; init; }
public ImportAction Action { get; init; }
/// The archive frame this photo duplicates (, or imported again).
public Guid? ExistingFrameId { get; init; }
/// SHA-256 computed while planning (duplicate check), if any.
public string? Sha256 { get; init; }
}
///
/// A session of the plan: one camera body and file format (a whole folder: one file format), one sequence in the
/// archive. Its camera is the one of its first photo.
///
public sealed record PlannedSession
{
/// 1-based, in order of start time across the import.
public int Number { get; init; }
/// Stable key of the session; re-planning the same photos yields the same key, so an interrupted import
/// resumes into the same sequence.
public required string SessionKey { get; init; }
public string? CameraMake { get; init; }
public string? CameraModel { get; init; }
public string? CameraSerial { get; init; }
/// Camera bodies among its photos: 1 for a session, possibly more for a whole folder.
public int Cameras { get; init; } = 1;
public ImageFileFormat Format { get; init; }
public DateTime Start { get; init; }
public DateTime End { get; init; }
/// Destination folder relative to the destination root, forward slashes.
public required string Folder { get; init; }
public string? Title { get; init; }
///
/// The title when the user types none: the source folder's name for a whole folder; null names the sequence
/// after its destination folder.
///
public string? DefaultTitle { get; init; }
public IReadOnlyList Items { get; init; } = [];
}
///
/// The exact result of an import before any byte is written (BRIEF §12.4 "Plan before touching disk"): sessions,
/// folders, file names, what is skipped and why, and the space it needs. Executing it copies exactly this.
///
public sealed record ImportPlan
{
public required Guid Id { get; init; }
public required string SourceRoot { get; init; }
public required string DestinationRoot { get; init; }
public DateTimeOffset Created { get; init; }
/// How the photos were grouped into .
public ImportGrouping Grouping { get; init; }
public IReadOnlyList Sessions { get; init; } = [];
public IReadOnlyList Problems { get; init; } = [];
/// Free space at the destination when planned; null when the volume does not report it.
public long? AvailableBytes { get; init; }
[JsonIgnore]
public IEnumerable Items => Sessions.SelectMany(s => s.Items);
[JsonIgnore]
public int FileCount => Sessions.Sum(s => s.Items.Count);
[JsonIgnore]
public int CopyCount => Items.Count(i => i.Action == ImportAction.Copy);
[JsonIgnore]
public long CopyBytes => Items.Where(i => i.Action == ImportAction.Copy).Sum(i => i.Size);
[JsonIgnore]
public bool HasEnoughSpace => AvailableBytes is not { } free || free >= CopyBytes;
}