/// <summary>
/// API representation of a digitization batch. Hides EF navigation properties
/// and exposes enum fields as DB string literals.
/// </summary>
public record BatchDetailResponse(
    Guid Id,
    string Status,
    string BatchType,
    string Track,
    BatchTypeFieldRequirements FieldRequirements,
    Guid? PatientId,
    string DocumentRef,
    string? DocumentUrl,
    bool EnableRetroactiveAlerts,
    Guid? EnteredByUserId,
    Guid? VerifiedByUserId,
    Guid? ApprovedByUserId,
    string? RejectionReason,
    DateTimeOffset? PromotedAt,
    Guid? PromotionEncounterId,
    Guid? SupersedesBatchId,
    bool ClinicianAttestation,
    bool IsCorrection,
    SupersessionInfo? Supersession,
    DateTimeOffset CreatedAt,
    DateTimeOffset UpdatedAt
)
{
    public static BatchDetailResponse FromEntity(
        DigitizationBatch batch,
        string? documentUrl = null,
        SupersessionInfo? supersession = null) =>
        new(
            batch.Id,
            batch.Status.ToDbString(),
            batch.BatchType.ToDbString(),
            batch.Track.ToDbString(),
            BatchTypeFieldRequirements.ForBatchType(batch.BatchType),
            batch.PatientId,
            batch.DocumentRef,
            documentUrl,
            batch.EnableRetroactiveAlerts,
            batch.EnteredByUserId,
            batch.VerifiedByUserId,
            batch.ApprovedByUserId,
            batch.RejectionReason,
            batch.PromotedAt,
            batch.PromotionEncounterId,
            batch.SupersedesBatchId,
            batch.ClinicianAttestation,
            batch.SupersedesBatchId.HasValue,
            supersession,
            batch.CreatedAt,
            batch.UpdatedAt
        );

    public static SupersessionInfo ToSupersessionInfo(
        DigitizationBatch supersededBatch,
        int originalObservationCount) =>
        new(
            supersededBatch.Id,
            supersededBatch.Status.ToDbString(),
            supersededBatch.PromotedAt ?? supersededBatch.UpdatedAt,
            originalObservationCount
        );
}
