// SPDX-FileCopyrightText: 2026 FocusFlow contributors
// SPDX-License-Identifier: AGPL-3.0-only

part of '../../persistence_memory.dart';

/// Private implementation type for `_Record` in this library.
/// It keeps supporting state and behavior scoped to the owning file so the public API stays focused on the scheduler concepts that callers use directly.
final class _Record<T> implements _IdentifiedRecord {
  /// Creates a `_Record` instance with the values required by its invariants.
  /// Callers should pass already-normalized scheduler data because validation and defaulting belong at the model or service boundary, not in arbitrary UI code.
  _Record({
    required this.id,
    required this.ownerId,
    required this.value,
    required this.revision,
    DateTime? createdAt,
    DateTime? updatedAt,
  })  : createdAt = createdAt ?? _epoch,
        updatedAt = updatedAt ?? _epoch;

  /// Stores the `id` value for this object.
  /// This field is part of the explicit state that downstream scheduling, persistence, or UI code reads when composing behavior.
  @override
  final String id;

  /// Stores the `ownerId` value for this object.
  /// This field is part of the explicit state that downstream scheduling, persistence, or UI code reads when composing behavior.
  final core.OwnerId ownerId;

  /// Stores the `value` value for this object.
  /// This field is part of the explicit state that downstream scheduling, persistence, or UI code reads when composing behavior.
  final T value;

  /// Stores the `revision` value for this object.
  /// This field is part of the explicit state that downstream scheduling, persistence, or UI code reads when composing behavior.
  final core.Revision revision;

  /// Stores the `createdAt` value for this object.
  /// This field is part of the explicit state that downstream scheduling, persistence, or UI code reads when composing behavior.
  final DateTime createdAt;

  /// Stores the `updatedAt` value for this object.
  /// This field is part of the explicit state that downstream scheduling, persistence, or UI code reads when composing behavior.
  final DateTime updatedAt;

  /// Returns a copy of this value with selected fields replaced.
  /// The scheduler uses copy-style updates to keep domain values immutable and make before-and-after behavior easier to test.
  _Record<T> copyWith({
    T? value,
    core.Revision? revision,
    DateTime? updatedAt,
  }) {
    return _Record<T>(
      id: id,
      ownerId: ownerId,
      value: value ?? this.value,
      revision: revision ?? this.revision,
      createdAt: createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
    );
  }
}
