using Chronos.App.Infrastructure;
using Chronos.App.Jobs;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Data.Archiving;
using Chronos.Data.Repositories;
using Chronos.Ui.Controls;
namespace Chronos.App.Archiving;
/// A group of identical photos: the copies and where they are; the first one counts as the original.
public sealed record DuplicateRow(string Title, string Detail, IReadOnlyList Places, Guid SequenceId);
/// What an orphan line is about, which decides its fix.
public enum OrphanKind
{
/// Photos in a sequence folder that its sidecar does not list: fix = index them.
UnlistedFiles,
/// Frames whose file is gone: fix = reconcile (flags them missing in sidecar and index).
MissingFiles,
/// An index row whose folder has no sidecar any more: fix = remove the row (after confirmation).
MissingSequence,
/// A folder with photos the index does not know yet: fix = scan.
NewFolder,
}
/// One line of the orphans report and the target of its fix.
public sealed record OrphanRow(OrphanKind Kind, string Title, string Detail, Guid? SequenceId);
/// A line of "Recent maintenance": date, operation and outcome.
public sealed record MaintenanceRow(Guid JobId, string DateText, string Text, string StateText, JobState State)
{
/// The outcome's glyph and colour, as on the Jobs screen (paired with the word, never colour alone).
public StatusKind StateKind => State switch
{
JobState.Running => StatusKind.Running,
JobState.Queued => StatusKind.Queued,
JobState.Paused => StatusKind.Paused,
JobState.Done => StatusKind.Done,
JobState.Failed => StatusKind.Failed,
_ => StatusKind.Offline,
};
///
/// The state first, as on the Jobs screen (a running row shows its percentage, not the word), then what it means; a finished
/// row has the state alone, since this list shows no run time for its explanation to speak of.
///
public string StateToolTip => State == JobState.Done
? Loc.L("Jobs.State.Done")
: string.Join(Loc.Separator, Loc.L("Jobs.State." + State), Loc.L("Tooltips.Jobs.State." + State));
}
///
/// The Maintenance view's reports (BRIEF §12.5): duplicates across sequences, orphans with their safe fixes, and the
/// recent archive jobs. Nothing here deletes a file; the only removal is of index rows whose sidecar is gone. Folders
/// and paths are kept left to right inside right-to-left text (BRIEF §16).
///
public static class MaintenanceReports
{
/// Kinds of jobs listed under "Recent maintenance".
public static readonly IReadOnlyList MaintenanceKinds = [JobKind.Scan, JobKind.Rebuild, JobKind.Verify, JobKind.Maintenance];
public static IReadOnlyList Duplicates(IReadOnlyList groups, IReadOnlyList locations)
{
var titles = locations.ToDictionary(l => l.Id, l => l.Title ?? l.Folder[(l.Folder.LastIndexOf('/') + 1)..]);
return
[
.. groups.Where(g => g.Frames.Count > 1).Select(g =>
{
var first = g.Frames[0];
var name = first.RelativePath[(first.RelativePath.LastIndexOf('/') + 1)..];
var places = g.Frames.Select(f => Loc.L("Archive.Duplicates.Place", titles.GetValueOrDefault(f.SequenceId, f.RelativePath),
Formatting.LeftToRight(f.RelativePath))).ToList();
return new DuplicateRow(name, Loc.Plural("Archive.Duplicates.Copies", g.Frames.Count), places, first.SequenceId);
}),
];
}
public static IReadOnlyList Orphans(OrphanReport report, IReadOnlyList locations)
{
var byFolder = locations.ToDictionary(l => l.Folder, StringComparer.Ordinal);
var rows = new List();
foreach (var s in report.Sequences)
{
byFolder.TryGetValue(s.Folder, out var location);
var title = location?.Title ?? Formatting.LeftToRight(s.Folder);
if (s.UnindexedFiles.Count > 0)
rows.Add(new OrphanRow(OrphanKind.UnlistedFiles, title, Loc.Plural("Archive.Orphans.Unlisted", s.UnindexedFiles.Count), location?.Id));
if (s.MissingFiles.Count > 0)
rows.Add(new OrphanRow(OrphanKind.MissingFiles, title, Loc.Plural("Archive.Orphans.Missing", s.MissingFiles.Count), location?.Id));
}
foreach (var missing in report.MissingSequences)
{
var folder = Formatting.LeftToRight(missing.Folder);
rows.Add(new OrphanRow(OrphanKind.MissingSequence, missing.Title ?? folder, Loc.L("Archive.Orphans.NoSidecar", folder), missing.Id));
}
foreach (var folder in report.UnindexedFolders)
rows.Add(new OrphanRow(OrphanKind.NewFolder, Formatting.LeftToRight(folder.Length == 0 ? "/" : folder), Loc.L("Archive.Orphans.NewFolder"), null));
return rows;
}
public static IReadOnlyList Recent(IEnumerable jobs, int count) =>
[
.. jobs.OrderByDescending(j => j.Finished ?? j.Started ?? j.Created).Take(count).Select(j =>
{
var when = j.Finished ?? j.Started ?? j.Created;
var text = string.IsNullOrWhiteSpace(j.TargetLabel) ? JobText.Kind(j.Kind) : Loc.L("Archive.Recent.Line", JobText.Kind(j.Kind), j.TargetLabel);
var state = j.State switch
{
JobState.Running when j.ProgressTotal > 0 => Formatting.Percent(j.ProgressDone / (double)j.ProgressTotal),
_ => Loc.L("Jobs.State." + j.State),
};
return new MaintenanceRow(j.Id, Formatting.Date(when), text, state, j.State);
}),
];
}