using System.Linq;
namespace OpenNest.Data;
///
/// Builds the shareable metadata from a live .
/// Counts are computed here (not carried on itself) so every caller
/// that saves to the nest server gets the same definition of "plate" and "part" counts.
///
public static class NestRecordFactory
{
/// Overlays authoritative current metadata on an archived nest before editing.
public static void ApplyMetadata(Nest nest, NestRecord record)
{
ArgumentNullException.ThrowIfNull(nest);
ArgumentNullException.ThrowIfNull(record);
nest.Name = record.Name;
nest.Customer = record.Customer;
nest.Notes = record.Comments;
nest.Status = record.Status;
nest.MadeBy = record.MadeBy;
nest.DateLastModified = record.DateModified;
nest.DateCreated = record.DateCreated;
nest.Material = NestMaterialSelection.Apply(nest.Material, record.Material);
nest.Thickness = record.Thickness;
}
///
/// Builds a record for uploading . is the
/// client-tracked id: for a first save (the server
/// assigns one), or the previously returned id to update an existing record.
/// is the size of the serialized .nest archive being
/// uploaded alongside this record; the server also recomputes it independently.
///
public static NestRecord FromNest(Nest nest, System.Guid id, long fileSize)
{
ArgumentNullException.ThrowIfNull(nest);
return new NestRecord
{
Id = id,
Name = nest.Name ?? "",
Customer = nest.Customer ?? "",
DateCreated = nest.DateCreated,
DateModified = nest.DateLastModified,
Material = nest.Material?.Name ?? "",
Thickness = nest.Thickness,
Status = nest.Status,
PlateCount = nest.Plates.Count,
PartCount = nest.Plates.Sum(CountNonCutoffParts),
// Nest has no separate "comments" field; Notes is the closest analog
// shown in the nest info dialog, so it round-trips as Comments here.
Comments = nest.Notes ?? "",
MadeBy = nest.MadeBy ?? "",
FileSize = fileSize,
};
}
private static int CountNonCutoffParts(Plate plate) =>
plate.Parts.Count(part => !part.BaseDrawing.IsCutOff);
}