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

part of '../repository_values.dart';

/// Optimistic concurrency revision for mutable repository records.
class Revision implements Comparable<Revision> {
  /// Creates a `Revision` 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.
  const Revision(this.value) : assert(value >= 0);

  /// First revision assigned to a newly inserted record.
  static const initial = Revision(1);

  /// Numeric revision value.
  final int value;

  /// Return the next monotonically increasing revision.
  Revision next() => Revision(value + 1);

  /// Performs the `compareTo` behavior for this scheduler component.
  /// The method provides a named entry point for this operation so callers do not need to know the lower-level state or persistence steps involved.
  @override
  int compareTo(Revision other) => value.compareTo(other.value);

  /// Converts scheduler data for `toString` without changing the source object.
  /// Conversion helpers keep persistence and export shapes explicit so domain models do not depend on storage-specific details.
  @override
  String toString() => value.toString();

  /// Performs the `operator ==` behavior for this scheduler component.
  /// The method provides a named entry point for this operation so callers do not need to know the lower-level state or persistence steps involved.
  @override
  bool operator ==(Object other) {
    return other is Revision && other.value == value;
  }

  /// Returns the derived `hashCode` value for this object.
  /// Keeping this as an accessor gives callers a stable read surface while the owning type controls how the value is calculated.
  @override
  int get hashCode => value.hashCode;
}
