using ERPCore.Domain.Enums;

namespace ERPCore.Domain.Entities;

/// <summary>
/// A reusable production-line definition — the stage graph designed on the canvas
/// (FR-MFG-01). Never hard-deleted once referenced by a run; deactivated instead
/// (FR-MD-08 posture). Editing is locked while any run of it is InProgress
/// (FR-MFG-06, <c>409 TEMPLATE_IN_USE</c>) — edit-lock replaces versioning, which is
/// why runs copy display fields at creation. Mutable aggregate with a
/// <see cref="RowVersion"/> token. Model: docs/30 Part C.
/// </summary>
public class ProductionTemplate
{
    public int TemplateId { get; set; }
    public string Code { get; set; } = string.Empty;
    public string Name { get; set; } = string.Empty;
    public string? Description { get; set; }

    public EntityStatus Status { get; set; } = EntityStatus.Active;

    /// <summary>
    /// Canvas-only annotations (grouping boxes and divider lines) as a jsonb array, stored
    /// verbatim and never interpreted server-side.
    /// </summary>
    /// <remarks>
    /// Not in docs/30 Part C — added because the builder canvas already draws these and
    /// without somewhere to keep them a save would silently discard the user's layout notes.
    /// They carry no graph semantics: no ports, no edges, and the validator never sees them.
    /// Nullable so a template that has none stores nothing rather than an empty array.
    /// </remarks>
    public string? Annotations { get; set; }

    public int CreatedBy { get; set; }
    public User? Creator { get; set; }

    public DateTime CreatedAt { get; set; }
    public uint RowVersion { get; set; }

    public ICollection<TemplateStage> Stages { get; set; } = new List<TemplateStage>();
    public ICollection<StageEdge> Edges { get; set; } = new List<StageEdge>();
    public ICollection<ProductionRun> Runs { get; set; } = new List<ProductionRun>();
}
