namespace ERPCore.Domain.Entities;

/// <summary>
/// One output line of a run stage — a copy of a <see cref="StageOutput"/> with its
/// quantity scaled at creation, plus the live produced/scrapped/transferred figures.
/// Model: docs/30 Part C.
/// <para><b>Available to transfer is derived, never stored:</b>
/// <c>ProducedQty − ScrappedQty − TransferredQty</c>. Every transfer path checks it and
/// raises <c>422 TRANSFER_EXCEEDS_AVAILABLE</c> (FR-MFG-12).</para>
/// <para>Scrap cost is <b>absorbed</b> into the run cost pool as normal yield loss — no
/// write-off ledger entry is posted (FR-MFG-11).</para>
/// </summary>
public class RunStageOutput
{
    public int RunOutputId { get; set; }

    public int RunStageId { get; set; }
    public RunStage? RunStage { get; set; }

    /// <summary>Null on intermediate (WIP) outputs; set on the terminal output — the finished good.</summary>
    public int? ItemId { get; set; }
    public Item? Item { get; set; }

    public string Name { get; set; } = string.Empty;

    /// <summary>
    /// Display label for intermediate WIP; null on the terminal output, whose unit is the
    /// finished item's base UOM. Never converted — see <see cref="StageOutput.UomId"/>.
    /// </summary>
    public int? UomId { get; set; }
    public Uom? Uom { get; set; }

    /// <summary>
    /// Scaled at creation; per-run editable until the stage starts. Every quantity on an
    /// output is a pack count, so scrap is recorded in whole broken bottles rather than ml.
    /// </summary>
    public decimal PlannedQty { get; set; }

    /// <summary>Recorded at complete. A re-complete after a rework <b>overwrites</b> this, never adds to it.</summary>
    public decimal ProducedQty { get; set; }

    public decimal ScrappedQty { get; set; }

    /// <summary>Mandatory when <see cref="ScrappedQty"/> &gt; 0, context <c>Production</c> (FR-MFG-11).</summary>
    public int? ScrapReasonCodeId { get; set; }
    public ReasonCode? ScrapReason { get; set; }

    /// <summary>Total WIP handed to children so far, across approve and any later partial transfers.</summary>
    public decimal TransferredQty { get; set; }
}
