using Chronos.App.Infrastructure;
using Chronos.App.Workspace;
using Chronos.Core.Localization;
using Chronos.Data.Archiving.Jobs;
using Chronos.Data.Housekeeping;
using Chronos.Data.Workspaces;
namespace Chronos.App.Jobs;
///
/// The desktop's clean-up step workspace-clean (phase 5c): removes the media of workspace projects in step with their
/// home, those not opened for Settings › Workspace › Clean after, then the least recently used while the projects take more
/// than Size limit (). Each project kept is logged with why. It needs no archive: the
/// workspace-cleanup schedule runs it whether one is open or not. The trash is never emptied here.
///
public sealed class WorkspaceCleanStep(Func workspace) : ICleanupStep
{
public const string StepName = "workspace-clean";
public string Name => StepName;
public async Task RunAsync(CleanupContext context, CleanupParameters parameters)
{
ArgumentNullException.ThrowIfNull(context);
if (workspace() is not { } host || await host.CleanAsync(cleanAfter: true, cancellationToken: context.Cancellation).ConfigureAwait(false) is not { } report)
return Loc.L("Jobs.Log.WorkspaceUnavailable");
foreach (var kept in report.Kept)
context.Job.Log(Loc.L("Jobs.Log.WorkspaceKept", Named(kept.SequenceTitle, kept.Name, kept.IsMain), Reason(kept.Reason)));
if (report.Cleaned.Count == 0 && report.Kept.Count == 0) return Loc.L("Jobs.Log.WorkspaceNothing", Formatting.Bytes(report.UsedBefore));
foreach (var cleaned in report.Cleaned.Where(c => c.Trashed > 0))
context.Job.Log(Loc.L("Archive.Log.WorkspaceOriginalsTrashed", Named(cleaned.SequenceTitle, cleaned.Name, cleaned.IsMain), Formatting.Count(cleaned.Trashed)));
return Loc.L("Jobs.Log.WorkspaceCleaned", Formatting.Count(report.Cleaned.Count), Formatting.Bytes(report.Freed), Formatting.Count(report.Kept.Count));
}
/// "open in Edit", "renders not sent", …: why a project was kept, for the job log.
public static string Reason(WorkspaceKeep reason) => Loc.L("Jobs.Log.WorkspaceSkip." + reason);
/// "Harbour sunset · Main": a project as the job log names it (its sequence, then its name).
public static string Named(string sequenceTitle, string name, bool isMain)
{
var project = WorkspaceText.ProjectName(name, isMain);
return sequenceTitle is { Length: > 0 } title ? title + Loc.Separator + project : project;
}
}
///
/// The desktop's clean-up step workspace-trash (phase 5c): reports what the workspace's trash holds (entries and
/// size); it never deletes, emptying it stays the user's Settings › Workspace › Trash › Empty….
///
public sealed class WorkspaceTrashStep(Func workspace) : ICleanupStep
{
public const string StepName = "workspace-trash";
public string Name => StepName;
public async Task RunAsync(CleanupContext context, CleanupParameters parameters)
{
ArgumentNullException.ThrowIfNull(context);
if (workspace() is not { IsEnabled: true } host) return Loc.L("Jobs.Log.WorkspaceUnavailable");
var plan = await host.PlanTrashAsync(context.Cancellation).ConfigureAwait(false);
return Loc.L("Jobs.Log.WorkspaceTrash", Formatting.Count(plan.Items.Count), Formatting.Bytes(plan.Bytes));
}
}