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

part of '../../../../application_layer.dart';

/// Private implementation type for `_UnitOfWorkSchedulingSnapshotRepository` 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.
class _UnitOfWorkSchedulingSnapshotRepository
    implements SchedulingSnapshotRepository {
  /// Creates a `_UnitOfWorkSchedulingSnapshotRepository` 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.
  _UnitOfWorkSchedulingSnapshotRepository(this._snapshotsById);

  /// Private state stored as `_snapshotsById` for this implementation.
  /// The field is intentionally scoped to this library so only the owning type can change the related scheduler or UI behavior.
  final Map<String, SchedulingStateSnapshot> _snapshotsById;

  /// Runs the `findById` operation and completes after its asynchronous work finishes.
  /// Callers should await the returned future so repository writes, UI refreshes, and failure handling happen in a predictable order.
  @override
  Future<SchedulingStateSnapshot?> findById(String id) async {
    return _snapshotsById[id];
  }

  /// Runs the `findInWindow` operation and completes after its asynchronous work finishes.
  /// Callers should await the returned future so repository writes, UI refreshes, and failure handling happen in a predictable order.
  @override
  Future<List<SchedulingStateSnapshot>> findInWindow(
    SchedulingWindow window,
  ) async {
    return List<SchedulingStateSnapshot>.unmodifiable(
      _snapshotsById.values.where(
        (snapshot) => snapshot.window.interval.overlaps(window.interval),
      ),
    );
  }

  /// Runs the `save` operation and completes after its asynchronous work finishes.
  /// Callers should await the returned future so repository writes, UI refreshes, and failure handling happen in a predictable order.
  @override
  Future<void> save(SchedulingStateSnapshot snapshot) async {
    _snapshotsById[snapshot.id] = snapshot;
  }
}
